1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
package script
import (
"go/ast"
"reflect"
)
// an expression can be evaluated
type Expr interface {
Eval() interface{} // evaluate and return result (nil for void)
Type() reflect.Type // return type, nil for void
Child() []Expr
Fix() Expr // replace all variables by their current value, except for the time "t".
}
// compiles an expression
func (w *World) compileExpr(e ast.Expr) Expr {
switch e := e.(type) {
default:
panic(err(e.Pos(), "not allowed:", typ(e)))
case *ast.Ident:
return w.resolve(e.Pos(), e.Name)
case *ast.BasicLit:
return w.compileBasicLit(e)
case *ast.BinaryExpr:
return w.compileBinaryExpr(e)
case *ast.UnaryExpr:
return w.compileUnaryExpr(e)
case *ast.CallExpr:
return w.compileCallExpr(e)
case *ast.ParenExpr:
return w.compileExpr(e.X)
case *ast.IndexExpr:
return w.compileIndexExpr(e)
}
}
|