Make type fields public

This commit is contained in:
Neemek 2025-03-18 20:32:44 +01:00
parent 954308b29f
commit 85b1631c84
4 changed files with 30 additions and 27 deletions

View file

@ -314,8 +314,8 @@ func (c *Compiler) Compile(tree Node) error {
} }
// check that arg type is as required // check that arg type is as required
if !f.in[i].Matches(sig) { if !f.In[i].Matches(sig) {
return c.error(fmt.Sprintf("argument #%d does not have expected type signature: got %s, requires %s", i, sig, f.in[i]), arg) return c.error(fmt.Sprintf("argument #%d does not have expected type signature: got %s, requires %s", i, sig, f.In[i]), arg)
} }
err = c.Compile(arg) err = c.Compile(arg)
@ -596,7 +596,7 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
return SignatureOf(v), nil return SignatureOf(v), nil
} }
return sig.(*ObjectSignature).members[n.property], nil return sig.(*ObjectSignature).Members[n.property], nil
default: default:
return nil, c.error(fmt.Sprintf("cannot access property from value of type %s", sig), n) return nil, c.error(fmt.Sprintf("cannot access property from value of type %s", sig), n)
@ -615,8 +615,8 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
f := sig.(*FunctionSignature) f := sig.(*FunctionSignature)
if len(n.args) != len(f.in) { if len(n.args) != len(f.In) {
return nil, c.error(fmt.Sprintf("bad argument count (expected %v, got %v)", len(f.in), len(n.args)), n) return nil, c.error(fmt.Sprintf("bad argument count (expected %v, got %v)", len(f.In), len(n.args)), n)
} }
// type check arguments // type check arguments
@ -626,12 +626,12 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
return nil, err return nil, err
} }
if !sig.Matches(f.in[i]) { if !sig.Matches(f.In[i]) {
return nil, c.error(fmt.Sprintf("argument #%d has wrong type signature. requires %s, got %s", i, f.in[i], sig), arg) return nil, c.error(fmt.Sprintf("argument #%d has wrong type signature. requires %s, got %s", i, f.In[i], sig), arg)
} }
} }
return f.out, nil return f.Out, nil
case FunctionNodeType: case FunctionNodeType:
n := tree.(*FunctionNode) n := tree.(*FunctionNode)

View file

@ -148,7 +148,7 @@ func (*BooleanSignature) String() string {
} }
type ListSignature struct { type ListSignature struct {
contents TypeSignature Contents TypeSignature
} }
func (*ListSignature) Type() Type { func (*ListSignature) Type() Type {
@ -160,15 +160,15 @@ func (s *ListSignature) Matches(other TypeSignature) bool {
return other.Matches(s) return other.Matches(s)
} }
return other.Type() == TypeAny || (other.Type() == TypeList && other.(*ListSignature).contents.Matches(s.contents)) return other.Type() == TypeAny || (other.Type() == TypeList && other.(*ListSignature).Contents.Matches(s.Contents))
} }
func (s *ListSignature) String() string { func (s *ListSignature) String() string {
return fmt.Sprintf("list[%s]", s.contents) return fmt.Sprintf("list[%s]", s.Contents)
} }
type ObjectSignature struct { type ObjectSignature struct {
members map[string]TypeSignature Members map[string]TypeSignature
} }
func (*ObjectSignature) Type() Type { func (*ObjectSignature) Type() Type {
@ -190,12 +190,12 @@ func (s *ObjectSignature) Matches(other TypeSignature) bool {
o := other.(*ObjectSignature) o := other.(*ObjectSignature)
if len(o.members) != len(s.members) { if len(o.Members) != len(s.Members) {
return false return false
} }
for name, member := range s.members { for name, member := range s.Members {
v, ok := o.members[name] v, ok := o.Members[name]
if !ok { if !ok {
return false return false
@ -214,8 +214,8 @@ func (s *ObjectSignature) String() string {
} }
type FunctionSignature struct { type FunctionSignature struct {
in []TypeSignature In []TypeSignature
out TypeSignature Out TypeSignature
} }
func (*FunctionSignature) Type() Type { func (*FunctionSignature) Type() Type {
@ -237,16 +237,16 @@ func (s *FunctionSignature) Matches(other TypeSignature) bool {
f := other.(*FunctionSignature) f := other.(*FunctionSignature)
if !s.out.Matches(f.out) { if !s.Out.Matches(f.Out) {
return false return false
} }
if len(f.in) != len(s.in) { if len(f.In) != len(s.In) {
return false return false
} }
for i, p := range s.in { for i, p := range s.In {
v := f.in[i] v := f.In[i]
if !p.Matches(v) { if !p.Matches(v) {
return false return false
} }
@ -260,7 +260,7 @@ func (s *FunctionSignature) String() string {
b.WriteString("func(") b.WriteString("func(")
for i, t := range s.in { for i, t := range s.In {
if i > 0 { if i > 0 {
b.WriteString(", ") b.WriteString(", ")
} }
@ -268,7 +268,7 @@ func (s *FunctionSignature) String() string {
} }
b.WriteString(") ") b.WriteString(") ")
b.WriteString(s.out.String()) b.WriteString(s.Out.String())
return b.String() return b.String()
} }

View file

@ -594,9 +594,9 @@ func (vm *VM) Next() bool {
vm.chunk = f.Chunk vm.chunk = f.Chunk
vm.ip = 0 vm.ip = 0
case *BuiltinFunctionValue: case *BuiltinFunctionValue:
args := make([]Value, len(f.Signature.in)) args := make([]Value, len(f.Signature.In))
for i := len(f.Signature.in) - 1; i >= 0; i-- { for i := len(f.Signature.In) - 1; i >= 0; i-- {
args[i] = vm.stack.Pop() args[i] = vm.stack.Pop()
} }

View file

@ -101,7 +101,10 @@ func run(_ js.Value, args []js.Value) interface{} {
// overwrite output // overwrite output
vm.SetGlobal("write", &core.BuiltinFunctionValue{ vm.SetGlobal("write", &core.BuiltinFunctionValue{
Name: "write", Name: "write",
Parameters: []string{"value"}, Signature: &core.FunctionSignature{
[]core.TypeSignature{&core.StringSignature{}},
&core.NilSignature{},
},
F: func(vm *core.VM, this core.Value, v map[string]core.Value) (core.Value, error) { F: func(vm *core.VM, this core.Value, v map[string]core.Value) (core.Value, error) {
log.Printf("Writing value: %s", v["value"].String()) log.Printf("Writing value: %s", v["value"].String())
outputHandler.Invoke(js.ValueOf(v["value"].String() + "\n")) outputHandler.Invoke(js.ValueOf(v["value"].String() + "\n"))