-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransformer.go
363 lines (308 loc) · 7.68 KB
/
transformer.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package spiker
import (
"fmt"
"strconv"
)
// Transform token list to AST tree
func Transform(tokList []*Token) (nodeList []AstNode, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
}()
for _, token := range tokList {
nodeList = append(nodeList, transNode(token))
}
return
}
// Transform token to AST node
func transNode(token *Token) AstNode {
switch token.sym {
// Variable
case SymbolIdent:
return &NodeVariable{
Ast: Ast{raw: token},
Value: token.value,
}
// Assignment
case SymbolAssign, SymbolAssignAdd, SymbolAssignSub, SymbolAssignMul, SymbolAssignDiv, SymbolAssignMod:
if token.children[0].sym != SymbolIdent {
panic(fmt.Sprintf("EXPECTED IDENT, BUT GOT: %v", token.children[0].sym))
}
// Function declare
if len(token.children) >= 2 && token.children[1].sym == SymbolFuncDeclare {
return transFuncDef(token)
}
// Assign
return &NodeAssignOp{
Ast: Ast{raw: token},
Var: NodeVariable{
Ast: Ast{raw: token},
Value: token.children[0].value,
},
Op: token.sym,
Expr: transNode(token.children[1]),
}
// Minus(-)
case SymbolSub:
if len(token.children) > 1 {
return &NodeBinaryOp{
Ast: Ast{raw: token},
Left: transNode(token.children[0]),
Op: token.sym,
Right: transNode(token.children[1]),
}
} else if len(token.children) == 1 {
return &NodeUnaryOp{
Ast: Ast{raw: token},
Op: token.sym,
Right: transNode(token.children[0]),
}
}
// Unary
case SymbolLogicNot, SymbolNot:
return &NodeUnaryOp{
Ast: Ast{raw: token},
Op: token.sym,
Right: transNode(token.children[0]),
}
// Binary
case SymbolAdd, SymbolMul, SymbolDiv, SymbolMod, SymbolPow, // +, -, *, /, %, **
SymbolSHL, SymbolSHR, // >>, <<
SymbolAnd, SymbolOr, SymbolXor, SymbolLogicAnd, SymbolLogicOr, // &, |, ^, &&, ||
SymbolEQL, SymbolNEQ, SymbolGTR, SymbolGTE, SymbolLSS, SymbolLTE, // ==, !=, >, >=, <, <=
SymbolIn: // in
return &NodeBinaryOp{
Ast: Ast{raw: token},
Left: transNode(token.children[0]),
Op: token.sym,
Right: transNode(token.children[1]),
}
// Number
case SymbolNumber:
num, _ := strconv.ParseFloat(token.value, 64)
return &NodeNumber{
Ast: Ast{raw: token},
Value: num,
}
// String
case SymbolString:
return &NodeString{
Ast: Ast{raw: token},
Value: token.value,
}
// True
case SymbolTrue:
return &NodeBool{
Ast: Ast{raw: token},
Value: true,
}
// False
case SymbolFalse:
return &NodeBool{
Ast: Ast{raw: token},
Value: false,
}
// [A,B,...]
case SymbolArray:
arr := &NodeList{
Ast: Ast{raw: token},
List: make([]AstNode, 0),
}
for _, subNode := range token.children {
arr.List = append(arr.List, transNode(subNode))
}
return arr
// [A:AA,BB,C:CC,...]
case SymbolMap:
var index float64
dict := &NodeMap{
Ast: Ast{raw: token},
Map: make(map[AstNode]AstNode),
}
for _, subNode := range token.children {
var key AstNode
item := transNode(subNode)
if subNode.key != nil {
key = item
item = transNode(subNode.key)
} else {
key = &NodeNumber{
Ast: Ast{raw: nil},
Value: index,
}
index++
}
dict.Map[key] = item
}
return dict
// var[i]
case SymbolLbrack:
idx := &NodeVarIndex{
Ast: Ast{raw: token},
Var: transNode(token.children[0]),
Index: transNode(token.children[1]),
}
return idx
// If
case SymbolIf:
return transIfStmt(token)
// (
case SymbolLparen:
// function call
if len(token.children) > 0 && token.children[0].sym == SymbolIdent {
fc := &NodeFuncCallOp{
Ast: Ast{raw: token},
Name: NodeVariable{
Ast: Ast{raw: token.children[0]},
Value: token.children[0].value,
},
Params: make([]AstNode, 0),
}
for _, pt := range token.children[1:] {
fc.Params = append(fc.Params, transNode(pt))
}
return fc
}
// while
case SymbolWhile:
if len(token.children) < 2 {
panic("Missing judgment expression for while")
}
nws := &NodeWhile{
Ast: Ast{raw: token},
Expr: transNode(token.children[0]),
Body: make([]AstNode, 0),
}
if token.children[1].sym == SymbolLbrace {
for _, stmt := range token.children[1].children {
nws.Body = append(nws.Body, transNode(stmt))
}
}
return nws
// continue
case SymbolContinue:
return &NodeContinue{
Ast{raw: token},
}
// break
case SymbolBreak:
return &NodeBreak{
Ast{raw: token},
}
// return
case SymbolReturn:
nr := &NodeReturn{
Ast: Ast{raw: token},
}
if len(token.children) > 0 {
for _, v := range token.children {
nr.Tuples = append(nr.Tuples, transNode(v))
}
}
return nr
}
return nil
}
// transform token to FuncDef statement
func transFuncDef(token *Token) *NodeFuncDef {
fnd := &NodeFuncDef{
Ast: Ast{raw: token},
Name: NodeVariable{
Ast: Ast{raw: token.children[0]},
Value: token.children[0].value,
},
Params: make([]NodeParam, 0),
Body: make([]AstNode, 0),
}
tokFnd := token.children[1]
if len(tokFnd.children) < 2 {
panic("FUNC DECLARE EXPECTED PARAMS AND BODY")
}
// params
switch tokFnd.children[0].sym {
case SymbolIdent: // single parameter
fnd.Params = append(fnd.Params, NodeParam{
Ast: Ast{raw: tokFnd.children[0]},
Name: NodeVariable{
Ast: Ast{raw: tokFnd.children[0]},
Value: tokFnd.children[0].value,
},
})
case SymbolTuple: // multi parameters, use tuple
if len(tokFnd.children[0].children) > 0 {
for _, v := range tokFnd.children[0].children {
fnd.Params = append(fnd.Params, NodeParam{
Ast: Ast{raw: v},
Name: NodeVariable{
Ast: Ast{raw: v},
Value: v.value,
},
})
}
}
}
// body
if tokFnd.children[1].sym == SymbolLbrace {
for _, v := range tokFnd.children[1].children {
fnd.Body = append(fnd.Body, transNode(v))
}
if len(tokFnd.children[1].children) == 1 {
fnd.SingleStmt = isSingleFuncStmt(tokFnd.children[1].children[0])
}
} else {
fnd.SingleStmt = true
fnd.Body = append(fnd.Body, transNode(tokFnd.children[1]))
}
return fnd
}
// transform token to IF statement
func transIfStmt(token *Token) *NodeIf {
// if
ifStmt := &NodeIf{
Ast: Ast{raw: token},
Expr: transNode(token.children[0]),
Body: make([]AstNode, 0),
}
// if ... { ... <body> ... }
if token.children[1] != nil && token.children[1].sym == SymbolLbrace {
for _, ifBodyNode := range token.children[1].children {
ifStmt.Body = append(ifStmt.Body, transNode(ifBodyNode))
}
}
if len(token.children) < 3 {
return ifStmt
}
// if ... <else if> ...
if token.children[2] != nil && token.children[2].sym == SymbolIf {
ifStmt.ElseIf = transIfStmt(token.children[2])
}
// if ... <else> ...
if token.children[2] != nil && token.children[2].sym == SymbolLbrace {
ifStmt.Else = make([]AstNode, 0)
for _, elseBodyNode := range token.children[2].children {
ifStmt.Else = append(ifStmt.Else, transNode(elseBodyNode))
}
}
return ifStmt
}
// is a func call statement
func isFuncCall(token *Token) bool {
return token.sym == SymbolLparen && len(token.children) > 0 && token.children[0].sym == SymbolIdent
}
// is a single statement of function declare
func isSingleFuncStmt(token *Token) bool {
switch token.sym {
case SymbolIdent, SymbolLogicNot, SymbolNot:
return true
case SymbolAdd, SymbolSub, SymbolMul, SymbolDiv, SymbolMod, SymbolPow, // +, -, *, /, %, **
SymbolSHL, SymbolSHR, // >>, <<
SymbolAnd, SymbolOr, SymbolXor, SymbolLogicAnd, SymbolLogicOr, // &, |, ^, &&, ||
SymbolEQL, SymbolNEQ, SymbolGTR, SymbolGTE, SymbolLSS, SymbolLTE, // ==, !=, >, >=, <, <=
SymbolIn: // in
return true
case SymbolLbrack, SymbolMap, SymbolArray, SymbolNumber, SymbolString, SymbolTrue, SymbolFalse:
return true
}
return isFuncCall(token)
}