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
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)
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)
}
err = c.Compile(arg)
@ -596,7 +596,7 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
return SignatureOf(v), nil
}
return sig.(*ObjectSignature).members[n.property], nil
return sig.(*ObjectSignature).Members[n.property], nil
default:
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)
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)
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)
}
// type check arguments
@ -626,12 +626,12 @@ func (c *Compiler) deduceSignature(tree Node) (TypeSignature, error) {
return nil, err
}
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)
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 f.out, nil
return f.Out, nil
case FunctionNodeType:
n := tree.(*FunctionNode)

View file

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

View file

@ -594,9 +594,9 @@ func (vm *VM) Next() bool {
vm.chunk = f.Chunk
vm.ip = 0
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()
}

View file

@ -101,7 +101,10 @@ func run(_ js.Value, args []js.Value) interface{} {
// overwrite output
vm.SetGlobal("write", &core.BuiltinFunctionValue{
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) {
log.Printf("Writing value: %s", v["value"].String())
outputHandler.Invoke(js.ValueOf(v["value"].String() + "\n"))