|
@@ -18,10 +18,10 @@ func (handler *Handler) Call(vm *VM, arguments ...Value) []Value {
|
|
type Callable interface {
|
|
type Callable interface {
|
|
|
|
|
|
Call(vm *VM, arguments ...Value) []Value
|
|
Call(vm *VM, arguments ...Value) []Value
|
|
-
|
|
+
|
|
Position() *Position
|
|
Position() *Position
|
|
-
|
|
+
|
|
-
|
|
+ Signature() Signature
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -34,26 +34,32 @@ type Helper interface {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-type CallableValue struct {
|
|
+type BasicCallable struct {
|
|
|
|
|
|
Name string
|
|
Name string
|
|
|
|
|
|
HelpText string
|
|
HelpText string
|
|
|
|
+
|
|
|
|
+ signature Signature
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (val BasicCallable) Signature() Signature {
|
|
|
|
+ return val.signature
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-func (val CallableValue) Help() string {
|
|
+func (val BasicCallable) Help() string {
|
|
return val.HelpText
|
|
return val.HelpText
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-func (val * CallableValue) SetHelp(help string) string {
|
|
+func (val * BasicCallable) SetHelp(help string) string {
|
|
val.HelpText = help
|
|
val.HelpText = help
|
|
return val.HelpText
|
|
return val.HelpText
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-func (val CallableValue) HelperName() string {
|
|
+func (val BasicCallable) HelperName() string {
|
|
return val.Name
|
|
return val.Name
|
|
}
|
|
}
|
|
|
|
|
|
@@ -68,32 +74,33 @@ func ClearHelp(helper Helper, extra string) string {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-func (val CallableValue) String() string {
|
|
+func (val BasicCallable) String() string {
|
|
return val.Name
|
|
return val.Name
|
|
}
|
|
}
|
|
|
|
|
|
-func (val CallableValue) Type() TypeValue {
|
|
+func (val BasicCallable) Type() TypeValue {
|
|
return TypeValue("Callable")
|
|
return TypeValue("Callable")
|
|
}
|
|
}
|
|
|
|
|
|
-func (from CallableValue) Convert(to interface{}) error {
|
|
+func (from BasicCallable) Convert(to interface{}) error {
|
|
return NewErrorValuef("Cannot convert the callable value %v to %v", from, to)
|
|
return NewErrorValuef("Cannot convert the callable value %v to %v", from, to)
|
|
}
|
|
}
|
|
|
|
|
|
-func (val *CallableValue) Call(vm *VM, arguments ...Value) []Value {
|
|
+func (val *BasicCallable) Call(vm *VM, arguments ...Value) []Value {
|
|
panic("Not implemented")
|
|
panic("Not implemented")
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
type BuiltinValue struct {
|
|
type BuiltinValue struct {
|
|
- CallableValue
|
|
+ BasicCallable
|
|
Handler
|
|
Handler
|
|
}
|
|
}
|
|
|
|
|
|
-func NewCallableValue(name string) CallableValue {
|
|
+func NewBasicCallable(name string) BasicCallable {
|
|
- return CallableValue{name, ""}
|
|
+ return BasicCallable{name, "", NoSignature()}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
func NewBuiltinValue(name string, handler Handler) *BuiltinValue {
|
|
func NewBuiltinValue(name string, handler Handler) *BuiltinValue {
|
|
result := &BuiltinValue{}
|
|
result := &BuiltinValue{}
|
|
result.Name = name
|
|
result.Name = name
|
|
@@ -103,7 +110,7 @@ func NewBuiltinValue(name string, handler Handler) *BuiltinValue {
|
|
|
|
|
|
|
|
|
|
type BlockValue struct {
|
|
type BlockValue struct {
|
|
- CallableValue
|
|
+ BasicCallable
|
|
Ast *Ast
|
|
Ast *Ast
|
|
}
|
|
}
|
|
|
|
|
|
@@ -137,22 +144,14 @@ func (defined * DefinedValue) Position() *Position {
|
|
return defined.Body.Position()
|
|
return defined.Body.Position()
|
|
}
|
|
}
|
|
|
|
|
|
-func (cv CallableValue) Position() *Position {
|
|
+func (cv BasicCallable) Position() *Position {
|
|
pos := Position{cv.Name, 1, 1}
|
|
pos := Position{cv.Name, 1, 1}
|
|
return &pos
|
|
return &pos
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-type Parameter struct {
|
|
|
|
- Name WordValue
|
|
|
|
- Type TypeValue
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
|
|
type DefinedValue struct {
|
|
type DefinedValue struct {
|
|
- CallableValue
|
|
+ BasicCallable
|
|
Body *BlockValue
|
|
Body *BlockValue
|
|
Parameters []*Parameter
|
|
Parameters []*Parameter
|
|
}
|
|
}
|
|
@@ -186,7 +185,7 @@ func (defined *DefinedValue) Call(vm *VM, arguments ...Value) []Value {
|
|
}
|
|
}
|
|
|
|
|
|
func (defined *DefinedValue) Help() string {
|
|
func (defined *DefinedValue) Help() string {
|
|
- help := defined.CallableValue.Help()
|
|
+ help := defined.BasicCallable.Help()
|
|
extra := "["
|
|
extra := "["
|
|
for _, parameter := range defined.Parameters {
|
|
for _, parameter := range defined.Parameters {
|
|
extra = fmt.Sprintf("%s %s %s", extra, parameter.Name, parameter.Type)
|
|
extra = fmt.Sprintf("%s %s %s", extra, parameter.Name, parameter.Type)
|
|
@@ -196,70 +195,6 @@ func (defined *DefinedValue) Help() string {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
- Amount of types that will be considered inside a signature.
|
|
|
|
- Limited mosty to allow hashability, that is, Signature is a map key.
|
|
|
|
-*/
|
|
|
|
-const TypesInSignature = 32
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-type Signature struct {
|
|
|
|
- Types [TypesInSignature]TypeValue
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (s Signature) String() string {
|
|
|
|
- res := "["
|
|
|
|
- sep := ""
|
|
|
|
- for _, typ := range s.Types {
|
|
|
|
- if typ != ZeroTypeValue {
|
|
|
|
- res = res + sep + typ.String()
|
|
|
|
- sep = " "
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- res = res + "]"
|
|
|
|
- return res
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func NewSignature(types ... TypeValue) Signature {
|
|
|
|
- signature := Signature{}
|
|
|
|
- for i := 0 ; i < len(types) && i < len(signature.Types) ; i ++ {
|
|
|
|
- signature.Types[i] = types[i]
|
|
|
|
- }
|
|
|
|
- return signature
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func CalculateSignature(arguments ...Value) Signature {
|
|
|
|
- signature := Signature{}
|
|
|
|
- for i := 0; i < len(signature.Types); i++ {
|
|
|
|
- if i < len(arguments) {
|
|
|
|
- signature.Types[i] = arguments[i].Type()
|
|
|
|
- } else {
|
|
|
|
- signature.Types[i] = AnyTypeValue
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return signature
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (tv TypeValue) IsMatch(other TypeValue) bool {
|
|
|
|
- if tv == AnyTypeValue || other == AnyTypeValue {
|
|
|
|
- return true
|
|
|
|
- }
|
|
|
|
- if tv == ZeroTypeValue || other == ZeroTypeValue {
|
|
|
|
- return true
|
|
|
|
- }
|
|
|
|
- return tv == other
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (signature Signature) IsMatch(other Signature) bool {
|
|
|
|
- for i, kind := range signature.Types {
|
|
|
|
- t1 := kind
|
|
|
|
- t2 := other.Types[i]
|
|
|
|
- if !t1.IsMatch(t2) {
|
|
|
|
- return false
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return true
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
|
|
type Overload struct {
|
|
type Overload struct {
|
|
@@ -272,7 +207,7 @@ the types of the arguments, in particular the first one. The individual
|
|
callable functions are the overloads
|
|
callable functions are the overloads
|
|
*/
|
|
*/
|
|
type CoverValue struct {
|
|
type CoverValue struct {
|
|
- CallableValue
|
|
+ BasicCallable
|
|
Overloads map[Signature]Overload
|
|
Overloads map[Signature]Overload
|
|
}
|
|
}
|
|
|
|
|
|
@@ -294,7 +229,7 @@ func NewCoverValue(name string) *CoverValue {
|
|
|
|
|
|
|
|
|
|
func (cover *CoverValue) Help() string {
|
|
func (cover *CoverValue) Help() string {
|
|
- help := cover.CallableValue.Help()
|
|
+ help := cover.BasicCallable.Help()
|
|
extra := "\n"
|
|
extra := "\n"
|
|
for signature, overload := range cover.Overloads {
|
|
for signature, overload := range cover.Overloads {
|
|
|
|
|