2016-02-26 17:22:50 +00:00
|
|
|
package hil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-09-30 19:56:40 +00:00
|
|
|
"errors"
|
2016-02-26 17:22:50 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hil/ast"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EvalConfig is the configuration for evaluating.
|
|
|
|
type EvalConfig struct {
|
|
|
|
// GlobalScope is the global scope of execution for evaluation.
|
|
|
|
GlobalScope *ast.BasicScope
|
|
|
|
|
|
|
|
// SemanticChecks is a list of additional semantic checks that will be run
|
|
|
|
// on the tree prior to evaluating it. The type checker, identifier checker,
|
|
|
|
// etc. will be run before these automatically.
|
|
|
|
SemanticChecks []SemanticChecker
|
|
|
|
}
|
|
|
|
|
|
|
|
// SemanticChecker is the type that must be implemented to do a
|
|
|
|
// semantic check on an AST tree. This will be called with the root node.
|
|
|
|
type SemanticChecker func(ast.Node) error
|
|
|
|
|
2016-08-09 23:53:09 +00:00
|
|
|
// EvaluationResult is a struct returned from the hil.Eval function,
|
|
|
|
// representing the result of an interpolation. Results are returned in their
|
|
|
|
// "natural" Go structure rather than in terms of the HIL AST. For the types
|
|
|
|
// currently implemented, this means that the Value field can be interpreted as
|
|
|
|
// the following Go types:
|
|
|
|
// TypeInvalid: undefined
|
|
|
|
// TypeString: string
|
|
|
|
// TypeList: []interface{}
|
|
|
|
// TypeMap: map[string]interface{}
|
2020-09-30 19:56:40 +00:00
|
|
|
// TypBool: bool
|
2016-08-09 23:53:09 +00:00
|
|
|
type EvaluationResult struct {
|
|
|
|
Type EvalType
|
|
|
|
Value interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidResult is a structure representing the result of a HIL interpolation
|
|
|
|
// which has invalid syntax, missing variables, or some other type of error.
|
|
|
|
// The error is described out of band in the accompanying error return value.
|
|
|
|
var InvalidResult = EvaluationResult{Type: TypeInvalid, Value: nil}
|
|
|
|
|
2020-09-30 19:56:40 +00:00
|
|
|
// errExitUnknown is an internal error that when returned means the result
|
|
|
|
// is an unknown value. We use this for early exit.
|
|
|
|
var errExitUnknown = errors.New("unknown value")
|
|
|
|
|
2016-08-09 23:53:09 +00:00
|
|
|
func Eval(root ast.Node, config *EvalConfig) (EvaluationResult, error) {
|
|
|
|
output, outputType, err := internalEval(root, config)
|
|
|
|
if err != nil {
|
|
|
|
return InvalidResult, err
|
|
|
|
}
|
|
|
|
|
2020-09-30 19:56:40 +00:00
|
|
|
// If the result contains any nested unknowns then the result as a whole
|
|
|
|
// is unknown, so that callers only have to deal with "entirely known"
|
|
|
|
// or "entirely unknown" as outcomes.
|
|
|
|
if ast.IsUnknown(ast.Variable{Type: outputType, Value: output}) {
|
|
|
|
outputType = ast.TypeUnknown
|
|
|
|
output = UnknownValue
|
|
|
|
}
|
|
|
|
|
2016-08-09 23:53:09 +00:00
|
|
|
switch outputType {
|
|
|
|
case ast.TypeList:
|
|
|
|
val, err := VariableToInterface(ast.Variable{
|
|
|
|
Type: ast.TypeList,
|
|
|
|
Value: output,
|
|
|
|
})
|
|
|
|
return EvaluationResult{
|
|
|
|
Type: TypeList,
|
|
|
|
Value: val,
|
|
|
|
}, err
|
|
|
|
case ast.TypeMap:
|
|
|
|
val, err := VariableToInterface(ast.Variable{
|
|
|
|
Type: ast.TypeMap,
|
|
|
|
Value: output,
|
|
|
|
})
|
|
|
|
return EvaluationResult{
|
2020-09-30 19:56:40 +00:00
|
|
|
Type: TypeMap,
|
2016-08-09 23:53:09 +00:00
|
|
|
Value: val,
|
|
|
|
}, err
|
|
|
|
case ast.TypeString:
|
|
|
|
return EvaluationResult{
|
|
|
|
Type: TypeString,
|
|
|
|
Value: output,
|
|
|
|
}, nil
|
2020-09-30 19:56:40 +00:00
|
|
|
case ast.TypeBool:
|
|
|
|
return EvaluationResult{
|
|
|
|
Type: TypeBool,
|
|
|
|
Value: output,
|
|
|
|
}, nil
|
|
|
|
case ast.TypeUnknown:
|
|
|
|
return EvaluationResult{
|
|
|
|
Type: TypeUnknown,
|
|
|
|
Value: UnknownValue,
|
|
|
|
}, nil
|
2016-08-09 23:53:09 +00:00
|
|
|
default:
|
|
|
|
return InvalidResult, fmt.Errorf("unknown type %s as interpolation output", outputType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 17:22:50 +00:00
|
|
|
// Eval evaluates the given AST tree and returns its output value, the type
|
|
|
|
// of the output, and any error that occurred.
|
2016-08-09 23:53:09 +00:00
|
|
|
func internalEval(root ast.Node, config *EvalConfig) (interface{}, ast.Type, error) {
|
2016-02-26 17:22:50 +00:00
|
|
|
// Copy the scope so we can add our builtins
|
|
|
|
if config == nil {
|
|
|
|
config = new(EvalConfig)
|
|
|
|
}
|
|
|
|
scope := registerBuiltins(config.GlobalScope)
|
|
|
|
implicitMap := map[ast.Type]map[ast.Type]string{
|
|
|
|
ast.TypeFloat: {
|
|
|
|
ast.TypeInt: "__builtin_FloatToInt",
|
|
|
|
ast.TypeString: "__builtin_FloatToString",
|
|
|
|
},
|
|
|
|
ast.TypeInt: {
|
|
|
|
ast.TypeFloat: "__builtin_IntToFloat",
|
|
|
|
ast.TypeString: "__builtin_IntToString",
|
|
|
|
},
|
|
|
|
ast.TypeString: {
|
2016-08-09 23:53:09 +00:00
|
|
|
ast.TypeInt: "__builtin_StringToInt",
|
|
|
|
ast.TypeFloat: "__builtin_StringToFloat",
|
2020-09-30 19:56:40 +00:00
|
|
|
ast.TypeBool: "__builtin_StringToBool",
|
|
|
|
},
|
|
|
|
ast.TypeBool: {
|
|
|
|
ast.TypeString: "__builtin_BoolToString",
|
2016-02-26 17:22:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build our own semantic checks that we always run
|
|
|
|
tv := &TypeCheck{Scope: scope, Implicit: implicitMap}
|
|
|
|
ic := &IdentifierCheck{Scope: scope}
|
|
|
|
|
|
|
|
// Build up the semantic checks for execution
|
|
|
|
checks := make(
|
|
|
|
[]SemanticChecker,
|
|
|
|
len(config.SemanticChecks),
|
|
|
|
len(config.SemanticChecks)+2)
|
|
|
|
copy(checks, config.SemanticChecks)
|
|
|
|
checks = append(checks, ic.Visit)
|
|
|
|
checks = append(checks, tv.Visit)
|
|
|
|
|
|
|
|
// Run the semantic checks
|
|
|
|
for _, check := range checks {
|
|
|
|
if err := check(root); err != nil {
|
|
|
|
return nil, ast.TypeInvalid, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
v := &evalVisitor{Scope: scope}
|
|
|
|
return v.Visit(root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EvalNode is the interface that must be implemented by any ast.Node
|
|
|
|
// to support evaluation. This will be called in visitor pattern order.
|
|
|
|
// The result of each call to Eval is automatically pushed onto the
|
|
|
|
// stack as a LiteralNode. Pop elements off the stack to get child
|
|
|
|
// values.
|
|
|
|
type EvalNode interface {
|
|
|
|
Eval(ast.Scope, *ast.Stack) (interface{}, ast.Type, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type evalVisitor struct {
|
|
|
|
Scope ast.Scope
|
|
|
|
Stack ast.Stack
|
|
|
|
|
|
|
|
err error
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *evalVisitor) Visit(root ast.Node) (interface{}, ast.Type, error) {
|
|
|
|
// Run the actual visitor pattern
|
|
|
|
root.Accept(v.visit)
|
|
|
|
|
|
|
|
// Get our result and clear out everything else
|
|
|
|
var result *ast.LiteralNode
|
|
|
|
if v.Stack.Len() > 0 {
|
|
|
|
result = v.Stack.Pop().(*ast.LiteralNode)
|
|
|
|
} else {
|
|
|
|
result = new(ast.LiteralNode)
|
|
|
|
}
|
|
|
|
resultErr := v.err
|
2020-09-30 19:56:40 +00:00
|
|
|
if resultErr == errExitUnknown {
|
|
|
|
// This means the return value is unknown and we used the error
|
|
|
|
// as an early exit mechanism. Reset since the value on the stack
|
|
|
|
// should be the unknown value.
|
|
|
|
resultErr = nil
|
|
|
|
}
|
2016-02-26 17:22:50 +00:00
|
|
|
|
|
|
|
// Clear everything else so we aren't just dangling
|
|
|
|
v.Stack.Reset()
|
|
|
|
v.err = nil
|
|
|
|
|
|
|
|
t, err := result.Type(v.Scope)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ast.TypeInvalid, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.Value, t, resultErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *evalVisitor) visit(raw ast.Node) ast.Node {
|
|
|
|
if v.err != nil {
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
|
|
|
|
en, err := evalNode(raw)
|
|
|
|
if err != nil {
|
|
|
|
v.err = err
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
|
|
|
|
out, outType, err := en.Eval(v.Scope, &v.Stack)
|
|
|
|
if err != nil {
|
|
|
|
v.err = err
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Stack.Push(&ast.LiteralNode{
|
|
|
|
Value: out,
|
|
|
|
Typex: outType,
|
|
|
|
})
|
2020-09-30 19:56:40 +00:00
|
|
|
|
|
|
|
if outType == ast.TypeUnknown {
|
|
|
|
// Halt immediately
|
|
|
|
v.err = errExitUnknown
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
|
2016-02-26 17:22:50 +00:00
|
|
|
return raw
|
|
|
|
}
|
|
|
|
|
|
|
|
// evalNode is a private function that returns an EvalNode for built-in
|
|
|
|
// types as well as any other EvalNode implementations.
|
|
|
|
func evalNode(raw ast.Node) (EvalNode, error) {
|
|
|
|
switch n := raw.(type) {
|
|
|
|
case *ast.Index:
|
|
|
|
return &evalIndex{n}, nil
|
|
|
|
case *ast.Call:
|
|
|
|
return &evalCall{n}, nil
|
2020-09-30 19:56:40 +00:00
|
|
|
case *ast.Conditional:
|
|
|
|
return &evalConditional{n}, nil
|
2016-08-09 23:53:09 +00:00
|
|
|
case *ast.Output:
|
|
|
|
return &evalOutput{n}, nil
|
2016-02-26 17:22:50 +00:00
|
|
|
case *ast.LiteralNode:
|
|
|
|
return &evalLiteralNode{n}, nil
|
|
|
|
case *ast.VariableAccess:
|
|
|
|
return &evalVariableAccess{n}, nil
|
|
|
|
default:
|
|
|
|
en, ok := n.(EvalNode)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("node doesn't support evaluation: %#v", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return en, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type evalCall struct{ *ast.Call }
|
|
|
|
|
|
|
|
func (v *evalCall) Eval(s ast.Scope, stack *ast.Stack) (interface{}, ast.Type, error) {
|
|
|
|
// Look up the function in the map
|
|
|
|
function, ok := s.LookupFunc(v.Func)
|
|
|
|
if !ok {
|
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf(
|
|
|
|
"unknown function called: %s", v.Func)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The arguments are on the stack in reverse order, so pop them off.
|
|
|
|
args := make([]interface{}, len(v.Args))
|
|
|
|
for i, _ := range v.Args {
|
|
|
|
node := stack.Pop().(*ast.LiteralNode)
|
2020-09-30 19:56:40 +00:00
|
|
|
if node.IsUnknown() {
|
|
|
|
// If any arguments are unknown then the result is automatically unknown
|
|
|
|
return UnknownValue, ast.TypeUnknown, nil
|
|
|
|
}
|
2016-02-26 17:22:50 +00:00
|
|
|
args[len(v.Args)-1-i] = node.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the function
|
|
|
|
result, err := function.Callback(args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf("%s: %s", v.Func, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, function.ReturnType, nil
|
|
|
|
}
|
|
|
|
|
2020-09-30 19:56:40 +00:00
|
|
|
type evalConditional struct{ *ast.Conditional }
|
2016-02-26 17:22:50 +00:00
|
|
|
|
2020-09-30 19:56:40 +00:00
|
|
|
func (v *evalConditional) Eval(s ast.Scope, stack *ast.Stack) (interface{}, ast.Type, error) {
|
|
|
|
// On the stack we have literal nodes representing the resulting values
|
|
|
|
// of the condition, true and false expressions, but they are in reverse
|
|
|
|
// order.
|
|
|
|
falseLit := stack.Pop().(*ast.LiteralNode)
|
|
|
|
trueLit := stack.Pop().(*ast.LiteralNode)
|
|
|
|
condLit := stack.Pop().(*ast.LiteralNode)
|
2016-02-26 17:22:50 +00:00
|
|
|
|
2020-09-30 19:56:40 +00:00
|
|
|
if condLit.IsUnknown() {
|
|
|
|
// If our conditional is unknown then our result is also unknown
|
|
|
|
return UnknownValue, ast.TypeUnknown, nil
|
2016-02-26 17:22:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:56:40 +00:00
|
|
|
if condLit.Value.(bool) {
|
|
|
|
return trueLit.Value, trueLit.Typex, nil
|
|
|
|
} else {
|
|
|
|
return falseLit.Value, trueLit.Typex, nil
|
2016-02-26 17:22:50 +00:00
|
|
|
}
|
2020-09-30 19:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type evalIndex struct{ *ast.Index }
|
|
|
|
|
|
|
|
func (v *evalIndex) Eval(scope ast.Scope, stack *ast.Stack) (interface{}, ast.Type, error) {
|
|
|
|
key := stack.Pop().(*ast.LiteralNode)
|
|
|
|
target := stack.Pop().(*ast.LiteralNode)
|
2016-08-09 23:53:09 +00:00
|
|
|
|
|
|
|
variableName := v.Index.Target.(*ast.VariableAccess).Name
|
|
|
|
|
2020-09-30 19:56:40 +00:00
|
|
|
if key.IsUnknown() {
|
|
|
|
// If our key is unknown then our result is also unknown
|
|
|
|
return UnknownValue, ast.TypeUnknown, nil
|
|
|
|
}
|
2016-08-09 23:53:09 +00:00
|
|
|
|
2020-09-30 19:56:40 +00:00
|
|
|
// For target, we'll accept collections containing unknown values but
|
|
|
|
// we still need to catch when the collection itself is unknown, shallowly.
|
|
|
|
if target.Typex == ast.TypeUnknown {
|
|
|
|
return UnknownValue, ast.TypeUnknown, nil
|
|
|
|
}
|
2016-08-09 23:53:09 +00:00
|
|
|
|
2020-09-30 19:56:40 +00:00
|
|
|
switch target.Typex {
|
|
|
|
case ast.TypeList:
|
|
|
|
return v.evalListIndex(variableName, target.Value, key.Value)
|
|
|
|
case ast.TypeMap:
|
|
|
|
return v.evalMapIndex(variableName, target.Value, key.Value)
|
2016-08-09 23:53:09 +00:00
|
|
|
default:
|
2020-09-30 19:56:40 +00:00
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf(
|
|
|
|
"target %q for indexing must be ast.TypeList or ast.TypeMap, is %s",
|
|
|
|
variableName, target.Typex)
|
2016-02-26 17:22:50 +00:00
|
|
|
}
|
2016-08-09 23:53:09 +00:00
|
|
|
}
|
2016-02-26 17:22:50 +00:00
|
|
|
|
2016-08-09 23:53:09 +00:00
|
|
|
func (v *evalIndex) evalListIndex(variableName string, target interface{}, key interface{}) (interface{}, ast.Type, error) {
|
|
|
|
// We assume type checking was already done and we can assume that target
|
|
|
|
// is a list and key is an int
|
2016-02-26 17:22:50 +00:00
|
|
|
list, ok := target.([]ast.Variable)
|
|
|
|
if !ok {
|
2020-09-30 19:56:40 +00:00
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf(
|
|
|
|
"cannot cast target to []Variable, is: %T", target)
|
2016-02-26 17:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyInt, ok := key.(int)
|
|
|
|
if !ok {
|
2020-09-30 19:56:40 +00:00
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf(
|
|
|
|
"cannot cast key to int, is: %T", key)
|
2016-02-26 17:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf("list is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyInt < 0 || len(list) < keyInt+1 {
|
2020-09-30 19:56:40 +00:00
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf(
|
|
|
|
"index %d out of range for list %s (max %d)",
|
|
|
|
keyInt, variableName, len(list))
|
2016-02-26 17:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
returnVal := list[keyInt].Value
|
|
|
|
returnType := list[keyInt].Type
|
|
|
|
return returnVal, returnType, nil
|
|
|
|
}
|
|
|
|
|
2016-08-09 23:53:09 +00:00
|
|
|
func (v *evalIndex) evalMapIndex(variableName string, target interface{}, key interface{}) (interface{}, ast.Type, error) {
|
|
|
|
// We assume type checking was already done and we can assume that target
|
|
|
|
// is a map and key is a string
|
|
|
|
vmap, ok := target.(map[string]ast.Variable)
|
|
|
|
if !ok {
|
2020-09-30 19:56:40 +00:00
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf(
|
|
|
|
"cannot cast target to map[string]Variable, is: %T", target)
|
2016-08-09 23:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyString, ok := key.(string)
|
|
|
|
if !ok {
|
2020-09-30 19:56:40 +00:00
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf(
|
|
|
|
"cannot cast key to string, is: %T", key)
|
2016-08-09 23:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(vmap) == 0 {
|
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf("map is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := vmap[keyString]
|
|
|
|
if !ok {
|
2020-09-30 19:56:40 +00:00
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf(
|
|
|
|
"key %q does not exist in map %s", keyString, variableName)
|
2016-08-09 23:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return value.Value, value.Type, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type evalOutput struct{ *ast.Output }
|
2016-02-26 17:22:50 +00:00
|
|
|
|
2016-08-09 23:53:09 +00:00
|
|
|
func (v *evalOutput) Eval(s ast.Scope, stack *ast.Stack) (interface{}, ast.Type, error) {
|
2016-02-26 17:22:50 +00:00
|
|
|
// The expressions should all be on the stack in reverse
|
|
|
|
// order. So pop them off, reverse their order, and concatenate.
|
|
|
|
nodes := make([]*ast.LiteralNode, 0, len(v.Exprs))
|
2020-09-30 19:56:40 +00:00
|
|
|
haveUnknown := false
|
2016-02-26 17:22:50 +00:00
|
|
|
for range v.Exprs {
|
2020-09-30 19:56:40 +00:00
|
|
|
n := stack.Pop().(*ast.LiteralNode)
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
|
|
|
|
// If we have any unknowns then the whole result is unknown
|
|
|
|
// (we must deal with this first, because the type checker can
|
|
|
|
// skip type conversions in the presence of unknowns, and thus
|
|
|
|
// any of our other nodes may be incorrectly typed.)
|
|
|
|
if n.IsUnknown() {
|
|
|
|
haveUnknown = true
|
|
|
|
}
|
2016-02-26 17:22:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:56:40 +00:00
|
|
|
if haveUnknown {
|
|
|
|
return UnknownValue, ast.TypeUnknown, nil
|
2016-08-09 23:53:09 +00:00
|
|
|
}
|
2020-09-30 19:56:40 +00:00
|
|
|
|
|
|
|
// Special case the single list and map
|
|
|
|
if len(nodes) == 1 {
|
|
|
|
switch t := nodes[0].Typex; t {
|
|
|
|
case ast.TypeList:
|
|
|
|
fallthrough
|
|
|
|
case ast.TypeMap:
|
|
|
|
fallthrough
|
|
|
|
case ast.TypeUnknown:
|
|
|
|
return nodes[0].Value, t, nil
|
|
|
|
}
|
2016-08-09 23:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise concatenate the strings
|
2016-02-26 17:22:50 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
for i := len(nodes) - 1; i >= 0; i-- {
|
2020-09-30 19:56:40 +00:00
|
|
|
if nodes[i].Typex != ast.TypeString {
|
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf(
|
|
|
|
"invalid output with %s value at index %d: %#v",
|
|
|
|
nodes[i].Typex,
|
|
|
|
i,
|
|
|
|
nodes[i].Value,
|
|
|
|
)
|
|
|
|
}
|
2016-02-26 17:22:50 +00:00
|
|
|
buf.WriteString(nodes[i].Value.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), ast.TypeString, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type evalLiteralNode struct{ *ast.LiteralNode }
|
|
|
|
|
|
|
|
func (v *evalLiteralNode) Eval(ast.Scope, *ast.Stack) (interface{}, ast.Type, error) {
|
|
|
|
return v.Value, v.Typex, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type evalVariableAccess struct{ *ast.VariableAccess }
|
|
|
|
|
|
|
|
func (v *evalVariableAccess) Eval(scope ast.Scope, _ *ast.Stack) (interface{}, ast.Type, error) {
|
|
|
|
// Look up the variable in the map
|
|
|
|
variable, ok := scope.LookupVar(v.Name)
|
|
|
|
if !ok {
|
|
|
|
return nil, ast.TypeInvalid, fmt.Errorf(
|
|
|
|
"unknown variable accessed: %s", v.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return variable.Value, variable.Type, nil
|
|
|
|
}
|