|
@@ -73,6 +73,15 @@ func ClearHelp(helper Helper, extra string) string {
|
|
return helper.SetHelp("")
|
|
return helper.SetHelp("")
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (bc * BasicCallable) Takes(arguments ...TypeValue) *BasicCallable {
|
|
|
|
+ bc.signature.SetParameters(arguments...)
|
|
|
|
+ return bc
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (bc * BasicCallable) Returns(arguments ...TypeValue) {
|
|
|
|
+ bc.signature.SetReturns(arguments...)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
|
|
func (val BasicCallable) String() string {
|
|
func (val BasicCallable) String() string {
|
|
return val.Name
|
|
return val.Name
|
|
@@ -153,24 +162,23 @@ func (cv BasicCallable) Position() *Position {
|
|
type DefinedValue struct {
|
|
type DefinedValue struct {
|
|
BasicCallable
|
|
BasicCallable
|
|
Body *BlockValue
|
|
Body *BlockValue
|
|
- Signature *Signature
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-func NewDefinedValue(name string, signature *Signature, body *BlockValue) *DefinedValue {
|
|
|
|
|
|
+func NewDefinedValue(name string, signature Signature, body *BlockValue) *DefinedValue {
|
|
result := &DefinedValue{}
|
|
result := &DefinedValue{}
|
|
result.Name = name
|
|
result.Name = name
|
|
result.Body = body
|
|
result.Body = body
|
|
- result.Signature = signature
|
|
|
|
|
|
+ result.signature = signature
|
|
return result
|
|
return result
|
|
}
|
|
}
|
|
|
|
|
|
func (defined *DefinedValue) Call(vm *VM, arguments ...Value) []Value {
|
|
func (defined *DefinedValue) Call(vm *VM, arguments ...Value) []Value {
|
|
- vm.Trace("Call defined value: %v %v %v", defined, defined.Signature, arguments)
|
|
|
|
|
|
+ vm.Trace("Call defined value: %v %v %v", defined, defined.signature, arguments)
|
|
for i , arg := range arguments {
|
|
for i , arg := range arguments {
|
|
- if i >= len(defined.Signature.Parameters) {
|
|
|
|
|
|
+ if i >= len(defined.signature.Parameters) {
|
|
break
|
|
break
|
|
}
|
|
}
|
|
- param := defined.Signature.Parameters[i]
|
|
|
|
|
|
+ param := defined.signature.Parameters[i]
|
|
expectedType := param.Type
|
|
expectedType := param.Type
|
|
if !expectedType.IsMatch(arg.Type()) {
|
|
if !expectedType.IsMatch(arg.Type()) {
|
|
return Fail(NewErrorValuef("Argument %d type mismatch: %s<->%s", i, expectedType, arg.Type()))
|
|
return Fail(NewErrorValuef("Argument %d type mismatch: %s<->%s", i, expectedType, arg.Type()))
|
|
@@ -187,14 +195,15 @@ func (defined *DefinedValue) Call(vm *VM, arguments ...Value) []Value {
|
|
func (defined *DefinedValue) Help() string {
|
|
func (defined *DefinedValue) Help() string {
|
|
help := defined.BasicCallable.Help()
|
|
help := defined.BasicCallable.Help()
|
|
extra := "["
|
|
extra := "["
|
|
- for _, parameter := range defined.Signature.Parameters {
|
|
|
|
|
|
+ for _, parameter := range defined.signature.Parameters {
|
|
extra = fmt.Sprintf("%s %s %s", extra, parameter.Name, parameter.Type)
|
|
extra = fmt.Sprintf("%s %s %s", extra, parameter.Name, parameter.Type)
|
|
}
|
|
}
|
|
extra = extra + "]:"
|
|
extra = extra + "]:"
|
|
return extra + help
|
|
return extra + help
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+// Assert that DefinedValue is callable.
|
|
|
|
+var _ Callable = &DefinedValue{}
|
|
|
|
|
|
/* An overload is an overloaded value that can be called. */
|
|
/* An overload is an overloaded value that can be called. */
|
|
type Overload struct {
|
|
type Overload struct {
|
|
@@ -553,6 +562,7 @@ func (builtin * BuiltinValue) Call(vm *VM, arguments ...Value) []Value {
|
|
return handler.Call(vm, arguments...)
|
|
return handler.Call(vm, arguments...)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/*
|
|
/*
|
|
func (vm *VM) CallDefined(ast *Ast, arguments ...Value) []Value {
|
|
func (vm *VM) CallDefined(ast *Ast, arguments ...Value) []Value {
|
|
arr := vm.RunChildren(*ast, arguments...)
|
|
arr := vm.RunChildren(*ast, arguments...)
|
|
@@ -629,7 +639,7 @@ func (vm * VM) ScopeUp(level int) *Scope {
|
|
return scope
|
|
return scope
|
|
}
|
|
}
|
|
|
|
|
|
-// RegisterUp registers in klevel scopes up from the current scope,
|
|
|
|
|
|
+// RegisterUp registers in level scopes up from the current scope,
|
|
// or at toplevel if the level is greater than the total depth
|
|
// or at toplevel if the level is greater than the total depth
|
|
func (vm *VM) RegisterUp(name string, value Value, level int) Value {
|
|
func (vm *VM) RegisterUp(name string, value Value, level int) Value {
|
|
scope := vm.ScopeUp(level)
|
|
scope := vm.ScopeUp(level)
|
|
@@ -646,18 +656,20 @@ func (vm *VM) RegisterCover(name string, level int) *CoverValue {
|
|
return value
|
|
return value
|
|
}
|
|
}
|
|
|
|
|
|
-func (vm *VM) RegisterBuiltin(name string, handler Handler) Value {
|
|
|
|
|
|
+func (vm *VM) RegisterBuiltin(name string, handler Handler) *BuiltinValue {
|
|
value := NewBuiltinValue(name, handler)
|
|
value := NewBuiltinValue(name, handler)
|
|
- return vm.Register(name, value)
|
|
|
|
|
|
+ vm.Register(name, value)
|
|
|
|
+ return value
|
|
}
|
|
}
|
|
|
|
|
|
-func (vm *VM) RegisterDefined(name string, signature *Signature, block *BlockValue, level int) Value {
|
|
|
|
|
|
+func (vm *VM) RegisterDefined(name string, signature Signature, block *BlockValue, level int) *DefinedValue {
|
|
value := NewDefinedValue(name, signature, block)
|
|
value := NewDefinedValue(name, signature, block)
|
|
- return vm.RegisterUp(name, value, level)
|
|
|
|
|
|
+ vm.RegisterUp(name, value, level)
|
|
|
|
+ return value
|
|
}
|
|
}
|
|
|
|
|
|
// RegisterBuiltinWithHelp
|
|
// RegisterBuiltinWithHelp
|
|
-func (vm *VM) RegisterBuiltinWithHelp(name string, handler Handler, help string) Value {
|
|
|
|
|
|
+func (vm *VM) RegisterBuiltinWithHelp(name string, handler Handler, help string) *BuiltinValue {
|
|
res := vm.RegisterBuiltin(name, handler)
|
|
res := vm.RegisterBuiltin(name, handler)
|
|
vm.SetHelp(name, help)
|
|
vm.SetHelp(name, help)
|
|
return res
|
|
return res
|