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
|
package script
// Exec compiles and executes the source statements.
func (w *World) Exec(src string) error {
code, err := w.Compile(src)
if err != nil {
return err
}
code.Eval()
return nil
}
// Exec with panic on error.
func (w *World) MustExec(src string) {
code := w.MustCompile(src)
code.Eval()
}
// Eval with panic on error.
func (w *World) MustEval(src string) interface{} {
Expr := w.MustCompileExpr(src)
return Expr.Eval()
}
// Eval compiles and evaluates src, which must be an expression, and returns the result(s). E.g.:
//
// world.Eval("1+1") // returns 2, nil
func (w *World) Eval(src string) (ret interface{}, err error) {
Expr, err := w.CompileExpr(src)
if err != nil {
return nil, err
}
return Expr.Eval(), nil
}
|