-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexample_eval_test.go
More file actions
59 lines (51 loc) · 1.87 KB
/
Copy pathexample_eval_test.go
File metadata and controls
59 lines (51 loc) · 1.87 KB
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
56
57
58
package eval_test
import (
"fmt"
"reflect"
"go/parser"
"github.com/0xfaded/eval"
)
const constant1 = "A constant"
//makeEnv creates an environment to use in eval
func makeEnv() *eval.SimpleEnv {
env := eval.MakeSimpleEnv()
env.Consts["constant1"] = reflect.ValueOf(constant1)
var1 := 1
env.Vars["var1"] = reflect.ValueOf(&var1)
env.Funcs["add"] = reflect.ValueOf(func(a, b int) int { return a + b })
fmtPkg := eval.MakeSimpleEnv()
fmtPkg.Funcs["Sprintf"] = reflect.ValueOf(fmt.Sprintf)
env.Pkgs["fmt"] = fmtPkg
return env
}
func ExampleSimple() {
result, panik, compileErrs := eval.Eval(`append([]int{1,2}[:1], map[int]int{1:2}[1])[1]`)
_ = panik
_ = compileErrs
fmt.Printf("%+v\n", result[0].Interface())
}
func ExampleWithEnvironment() {
env := makeEnv() // Create evaluation environment
result, panik, compileErrs := eval.EvalEnv(`fmt.Sprintf("%s %d", constant1, add(var1, 1) + 1)`, env)
_ = panik
_ = compileErrs
fmt.Printf("%+v\n", result[0].Interface())
}
// ExpectResult check the evaluation of a string with an expected result.
// More importantly though, does the steps to evaluate a string:
// 1. Parse expression using parser.ParseExpr (go/parser)
// 2. Type check expression using evalCheckExpr (0xfaded/eval)
// 3. run eval.EvalExpr (0xfaded/eval)
func ExampleFullApi() {
expr := `fmt.Sprintf("%s %d", constant1, add(var1, 1) + 1)`
env := makeEnv() // Create evaluation environment
if e, err := parser.ParseExpr(expr); err != nil {
fmt.Printf("Failed to parse expression '%s' (%v)\n", expr, err)
} else if cexpr, errs := eval.CheckExpr(e, env); len(errs) != 0 {
fmt.Printf("Error checking expression '%s' (%v)\n", expr, errs)
} else if results, err := eval.EvalExpr(cexpr, env); err != nil {
fmt.Printf("Panic evaluating expression '%s' (%v)\n", expr, err)
} else {
fmt.Printf("Expression '%s' yielded '%+v'\n", expr, results[0].Interface())
}
}