8 // Scope is the interface used to look up variables and functions while
9 // evaluating. How these functions/variables are defined are up to the caller.
10 type Scope interface {
11 LookupFunc(string) (Function, bool)
12 LookupVar(string) (Variable, bool)
15 // Variable is a variable value for execution given as input to the engine.
16 // It records the value of a variables along with their type.
17 type Variable struct {
22 // NewVariable creates a new Variable for the given value. This will
23 // attempt to infer the correct type. If it can't, an error will be returned.
24 func NewVariable(v interface{}) (result Variable, err error) {
25 switch v := reflect.ValueOf(v); v.Kind() {
27 result.Type = TypeString
29 err = fmt.Errorf("Unknown type: %s", v.Kind())
36 // String implements Stringer on Variable, displaying the type and value
38 func (v Variable) String() string {
39 return fmt.Sprintf("{Variable (%s): %+v}", v.Type, v.Value)
42 // Function defines a function that can be executed by the engine.
43 // The type checker will validate that the proper types will be called
45 type Function struct {
46 // ArgTypes is the list of types in argument order. These are the
47 // required arguments.
49 // ReturnType is the type of the returned value. The Callback MUST
54 // Variadic, if true, says that this function is variadic, meaning
55 // it takes a variable number of arguments. In this case, the
56 // VariadicType must be set.
60 // Callback is the function called for a function. The argument
61 // types are guaranteed to match the spec above by the type checker.
62 // The length of the args is strictly == len(ArgTypes) unless Varidiac
63 // is true, in which case its >= len(ArgTypes).
64 Callback func([]interface{}) (interface{}, error)
67 // BasicScope is a simple scope that looks up variables and functions
69 type BasicScope struct {
70 FuncMap map[string]Function
71 VarMap map[string]Variable
74 func (s *BasicScope) LookupFunc(n string) (Function, bool) {
76 return Function{}, false
83 func (s *BasicScope) LookupVar(n string) (Variable, bool) {
85 return Variable{}, false