-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
324 lines (261 loc) · 7.54 KB
/
expr.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import "fmt"
type Visitor interface {
visitBinaryExpr(expr *BinaryExpr) string
visitUnaryExpr(expr *UnaryExpr) string
visitLiteralExpr(expr *LiteralExpr) string
visitGroupingExpr(expr *GroupingExpr) string
visitVarExpr(expr *VarExpr) string
visitAssignExpr(expr *AssignExpr) string
visitLogicalExpr(expr *LogicalExpr) string
visitCallExpr(expr *CallExpr) string
visitGetExpr(expr *GetExpr) string
visitSetExpr(expr *SetExpr) string
visitThisExpr(expr *ThisExpr) string
visitSuperExpr(expr *SuperExpr) string
}
type EvalVisitor interface {
visitBinaryExpr(expr *BinaryExpr) (interface{}, error)
visitUnaryExpr(expr *UnaryExpr) (interface{}, error)
visitLiteralExpr(expr *LiteralExpr) (interface{}, error)
visitGroupingExpr(expr *GroupingExpr) (interface{}, error)
visitVarExpr(expr *VarExpr) (interface{}, error)
visitAssignExpr(expr *AssignExpr) (interface{}, error)
visitLogicalExpr(expr *LogicalExpr) (interface{}, error)
visitCallExpr(expr *CallExpr) (interface{}, error)
visitGetExpr(expr *GetExpr) (interface{}, error)
visitSetExpr(expr *SetExpr) (interface{}, error)
visitThisExpr(expr *ThisExpr) (interface{}, error)
visitSuperExpr(expr *SuperExpr) (interface{}, error)
}
type Expr interface {
acceptStringVisitor(visitor Visitor) string
acceptEvalVisitor(visitor EvalVisitor) (interface{}, error)
}
type BinaryExpr struct {
left, right Expr
operator token
}
func newBinaryExpr(left, right Expr, operator token) *BinaryExpr {
return &BinaryExpr{
left: left,
right: right,
operator: operator,
}
}
func (expr *BinaryExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitBinaryExpr(expr)
}
func (expr *BinaryExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitBinaryExpr(expr)
}
func (expr *BinaryExpr) String() string {
return fmt.Sprintf("binary expr, left:%s operand:%s right:%s", expr.left, expr.operator, expr.right)
}
type UnaryExpr struct {
right Expr
operator token
}
func newUnaryExpr(right Expr, operator token) *UnaryExpr {
return &UnaryExpr{
right: right,
operator: operator,
}
}
func (expr *UnaryExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitUnaryExpr(expr)
}
func (expr *UnaryExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitUnaryExpr(expr)
}
func (expr *UnaryExpr) String() string {
return fmt.Sprintf("unary expr: operand:%s right:%s", expr.operator, expr.right)
}
type LiteralExpr struct {
value interface{}
}
func newLiteralExpr(value interface{}) *LiteralExpr {
return &LiteralExpr{
value: value,
}
}
func (expr *LiteralExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitLiteralExpr(expr)
}
func (expr *LiteralExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitLiteralExpr(expr)
}
func (expr *LiteralExpr) String() string {
return fmt.Sprintf("literal expr, value:%v", expr.value)
}
type GroupingExpr struct {
expression Expr
}
func newGroupingExpr(expr Expr) *GroupingExpr {
return &GroupingExpr{
expression: expr,
}
}
func (expr *GroupingExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitGroupingExpr(expr)
}
func (expr *GroupingExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitGroupingExpr(expr)
}
func (expr *GroupingExpr) String() string {
return fmt.Sprintf("group expr, expression:%s )", expr.expression)
}
type VarExpr struct {
name token
}
func newVarExpr(name token) *VarExpr {
return &VarExpr{
name: name,
}
}
func (expr *VarExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitVarExpr(expr)
}
func (expr *VarExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitVarExpr(expr)
}
func (expr VarExpr) String() string {
return fmt.Sprintf("var expr, var:%s", expr.name)
}
type AssignExpr struct {
name token
expr Expr
}
func newAssignExpr(name token, value Expr) *AssignExpr {
return &AssignExpr{
name: name,
expr: value,
}
}
func (expr *AssignExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitAssignExpr(expr)
}
func (expr *AssignExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitAssignExpr(expr)
}
func (expr *AssignExpr) String() string {
return fmt.Sprintf("assign expr, name:%s = expr:%s", expr.name, expr.expr)
}
type LogicalExpr struct {
operator token
left Expr
right Expr
}
func newLogicalExpr(operator token, left, right Expr) *LogicalExpr {
return &LogicalExpr{
operator: operator,
left: left,
right: right,
}
}
func (expr *LogicalExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitLogicalExpr(expr)
}
func (expr *LogicalExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitLogicalExpr(expr)
}
func (expr *LogicalExpr) String() string {
return fmt.Sprintf("logical expr, left:%s operator:%s right:%s", expr.left, expr.operator, expr.right)
}
type CallExpr struct {
callee Expr
paren token
args []Expr
}
func newCallExpr(callee Expr, paren token, args []Expr) *CallExpr {
return &CallExpr{
callee: callee,
paren: paren,
args: args,
}
}
func (expr *CallExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitCallExpr(expr)
}
func (expr *CallExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitCallExpr(expr)
}
func (expr *CallExpr) String() string {
return fmt.Sprintf("call expr, callee: %s args:%s", expr.callee, expr.args)
}
type GetExpr struct {
object Expr
name token
}
func newGetExpr(object Expr, name token) *GetExpr {
return &GetExpr{
object: object,
name: name,
}
}
func (expr *GetExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitGetExpr(expr)
}
func (expr *GetExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitGetExpr(expr)
}
func (expr *GetExpr) String() string {
return fmt.Sprintf("get expr, object: %s name:%s", expr.object, expr.name)
}
type SetExpr struct {
object Expr
name token
value Expr
}
func newSetExpr(object Expr, name token, value Expr) *SetExpr {
return &SetExpr{
object: object,
name: name,
value: value,
}
}
func (expr *SetExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitSetExpr(expr)
}
func (expr *SetExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitSetExpr(expr)
}
func (expr *SetExpr) String() string {
return fmt.Sprintf("set expr, object: %s name:%s value:%s", expr.object, expr.name, expr.value)
}
type ThisExpr struct {
keyword token
}
func newThisExpr(keyword token) *ThisExpr {
return &ThisExpr{
keyword: keyword,
}
}
func (expr *ThisExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitThisExpr(expr)
}
func (expr *ThisExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitThisExpr(expr)
}
func (expr *ThisExpr) String() string {
return fmt.Sprintf("this expr, keyword: %s", expr.keyword)
}
type SuperExpr struct {
keyword token
method token
}
func newSuperExpr(keyword token, method token) *SuperExpr {
return &SuperExpr{
keyword: keyword,
method: method,
}
}
func (expr *SuperExpr) acceptStringVisitor(visitor Visitor) string {
return visitor.visitSuperExpr(expr)
}
func (expr *SuperExpr) acceptEvalVisitor(visitor EvalVisitor) (interface{}, error) {
return visitor.visitSuperExpr(expr)
}
func (expr *SuperExpr) String() string {
return fmt.Sprintf("super expr, keyword: %s, method: %s", expr.keyword, expr.method)
}