@@ -3,19 +3,52 @@ package main
3
3
import (
4
4
"fmt"
5
5
"math/rand"
6
+ "runtime/debug"
6
7
7
8
"github.com/antonmedv/expr"
8
9
"github.com/antonmedv/expr/ast"
9
10
"github.com/antonmedv/expr/builtin"
10
- "github.com/antonmedv/expr/test/playground"
11
11
)
12
12
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
+
13
46
func main () {
14
47
var code string
15
48
defer func () {
16
49
if r := recover (); r != nil {
17
50
fmt .Printf ("==========================\n %s\n ==========================\n " , code )
18
- panic ( r )
51
+ debug . PrintStack ( )
19
52
}
20
53
}()
21
54
@@ -32,11 +65,11 @@ func main() {
32
65
{10 , 5 },
33
66
})).String ()
34
67
35
- program , err := expr .Compile (code , expr .Env (playground. Blog {} ))
68
+ program , err := expr .Compile (code , expr .Env (env ))
36
69
if err != nil {
37
70
continue
38
71
}
39
- _ , err = expr .Run (program , playground . ExampleData () )
72
+ _ , err = expr .Run (program , env )
40
73
if err != nil {
41
74
continue
42
75
}
@@ -98,7 +131,7 @@ func integerNode(depth int) ast.Node {
98
131
99
132
func stringNode (depth int ) ast.Node {
100
133
corpus := []string {
101
- "Go " , "JavaScript " , " " ,
134
+ "a " , "b " , "c " ,
102
135
}
103
136
return & ast.StringNode {
104
137
Value : corpus [rand .Intn (len (corpus ))],
@@ -112,40 +145,16 @@ func booleanNode(depth int) ast.Node {
112
145
}
113
146
114
147
func identifierNode (depth int ) ast.Node {
115
- cases := []string {
116
- "Posts" ,
117
- "Authors" ,
118
- "TotalViews" ,
119
- }
120
-
121
148
return & ast.IdentifierNode {
122
- Value : cases [rand .Intn (len (cases ))],
149
+ Value : names [rand .Intn (len (names ))],
123
150
}
124
151
}
125
152
126
153
func memberNode (depth int ) ast.Node {
127
154
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" ,
149
158
}
150
159
151
160
return & ast.MemberNode {
@@ -205,17 +214,8 @@ func binaryNode(depth int) ast.Node {
205
214
206
215
func methodNode (depth int ) ast.Node {
207
216
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" ,
219
219
}
220
220
221
221
return & ast.MemberNode {
@@ -227,17 +227,8 @@ func methodNode(depth int) ast.Node {
227
227
228
228
func funcNode (depth int ) ast.Node {
229
229
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" ,
241
232
}
242
233
243
234
return & ast.IdentifierNode {
0 commit comments