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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
package script
import (
"go/ast"
"reflect"
)
// compiles expression or statement
func (w *World) compile(n ast.Node) Expr {
switch n := n.(type) {
case ast.Stmt:
return w.compileStmt(n)
case ast.Expr:
return w.compileExpr(n)
default:
panic(err(n.Pos(), "not allowed"))
}
}
// compiles a statement
func (w *World) compileStmt(st ast.Stmt) Expr {
switch st := st.(type) {
default:
panic(err(st.Pos(), "not allowed:", typ(st)))
case *ast.EmptyStmt:
return &emptyStmt{}
case *ast.AssignStmt:
return w.compileAssignStmt(st)
case *ast.ExprStmt:
return w.compileExpr(st.X)
case *ast.IfStmt:
return w.compileIfStmt(st)
case *ast.ForStmt:
return w.compileForStmt(st)
case *ast.IncDecStmt:
return w.compileIncDecStmt(st)
case *ast.BlockStmt:
w.EnterScope()
defer w.ExitScope()
return w.compileBlockStmt_noScope(st)
}
}
// embed to get Type() that returns nil
type void struct{}
func (v *void) Type() reflect.Type { return nil }
func (v *void) Fix() Expr { panic(invalid_closure) }
type emptyStmt struct{ void }
func (*emptyStmt) Child() []Expr { return nil }
func (*emptyStmt) Eval() interface{} { return nil }
const invalid_closure = "illegal statement in closure"
|