@@ -3,19 +3,52 @@ package main
33import (
44 "fmt"
55 "math/rand"
6+ "runtime/debug"
67
78 "github.com/antonmedv/expr"
89 "github.com/antonmedv/expr/ast"
910 "github.com/antonmedv/expr/builtin"
10- "github.com/antonmedv/expr/test/playground"
1111)
1212
13+ var env = map [string ]interface {}{
14+ "a" : 1 ,
15+ "b" : 2 ,
16+ "f" : 0.5 ,
17+ "ok" : true ,
18+ "s" : "abc" ,
19+ "arr" : []int {1 , 2 , 3 },
20+ "obj" : map [string ]interface {}{
21+ "a" : 1 ,
22+ "b" : 2 ,
23+ "obj" : map [string ]interface {}{
24+ "a" : 1 ,
25+ "b" : 2 ,
26+ "obj" : map [string ]int {
27+ "a" : 1 ,
28+ "b" : 2 ,
29+ },
30+ },
31+ "fn" : func (a int ) int { return a + 1 },
32+ "head" : func (xs ... interface {}) interface {} { return xs [0 ] },
33+ },
34+ "add" : func (a , b int ) int { return a + b },
35+ "div" : func (a , b int ) int { return a / b },
36+ }
37+
38+ var names []string
39+
40+ func init () {
41+ for name := range env {
42+ names = append (names , name )
43+ }
44+ }
45+
1346func main () {
1447 var code string
1548 defer func () {
1649 if r := recover (); r != nil {
1750 fmt .Printf ("==========================\n %s\n ==========================\n " , code )
18- panic ( r )
51+ debug . PrintStack ( )
1952 }
2053 }()
2154
@@ -32,11 +65,11 @@ func main() {
3265 {10 , 5 },
3366 })).String ()
3467
35- program , err := expr .Compile (code , expr .Env (playground. Blog {} ))
68+ program , err := expr .Compile (code , expr .Env (env ))
3669 if err != nil {
3770 continue
3871 }
39- _ , err = expr .Run (program , playground . ExampleData () )
72+ _ , err = expr .Run (program , env )
4073 if err != nil {
4174 continue
4275 }
@@ -98,7 +131,7 @@ func integerNode(depth int) ast.Node {
98131
99132func stringNode (depth int ) ast.Node {
100133 corpus := []string {
101- "Go " , "JavaScript " , " " ,
134+ "a " , "b " , "c " ,
102135 }
103136 return & ast.StringNode {
104137 Value : corpus [rand .Intn (len (corpus ))],
@@ -112,40 +145,16 @@ func booleanNode(depth int) ast.Node {
112145}
113146
114147func identifierNode (depth int ) ast.Node {
115- cases := []string {
116- "Posts" ,
117- "Authors" ,
118- "TotalViews" ,
119- }
120-
121148 return & ast.IdentifierNode {
122- Value : cases [rand .Intn (len (cases ))],
149+ Value : names [rand .Intn (len (names ))],
123150 }
124151}
125152
126153func memberNode (depth int ) ast.Node {
127154 cases := []string {
128- "Birthday" ,
129- "Biography" ,
130- "Website" ,
131- "ID" ,
132- "FirstName" ,
133- "LastName" ,
134- "Email" ,
135- "Profile" ,
136- "ID" ,
137- "Title" ,
138- "Content" ,
139- "PublishDate" ,
140- "Author" ,
141- "Comments" ,
142- "Tags" ,
143- "Likes" ,
144- "ID" ,
145- "AuthorName" ,
146- "Content" ,
147- "CommentDate" ,
148- "Upvotes" ,
155+ "a" ,
156+ "b" ,
157+ "obj" ,
149158 }
150159
151160 return & ast.MemberNode {
@@ -205,17 +214,8 @@ func binaryNode(depth int) ast.Node {
205214
206215func methodNode (depth int ) ast.Node {
207216 cases := []string {
208- "Age" ,
209- "FullName" ,
210- "IsAdmin" ,
211- "Published" ,
212- "After" ,
213- "Before" ,
214- "Compare" ,
215- "Equal" ,
216- "IsZero" ,
217- "Upvoted" ,
218- "AuthorEmail" ,
217+ "fn" ,
218+ "head" ,
219219 }
220220
221221 return & ast.MemberNode {
@@ -227,17 +227,8 @@ func methodNode(depth int) ast.Node {
227227
228228func funcNode (depth int ) ast.Node {
229229 cases := []string {
230- "RecentPosts" ,
231- "PopularPosts" ,
232- "TotalUpvotes" ,
233- "TotalComments" ,
234- "Add" ,
235- "Sub" ,
236- "Title" ,
237- "HasTag" ,
238- "IsAdmin" ,
239- "IsZero" ,
240- "WithID" ,
230+ "add" ,
231+ "div" ,
241232 }
242233
243234 return & ast.IdentifierNode {
0 commit comments