-
Notifications
You must be signed in to change notification settings - Fork 43
/
assign.go
367 lines (309 loc) · 8.03 KB
/
assign.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
364
365
366
367
package yakast
import (
"strings"
yak "github.com/yaklang/yaklang/common/yak/antlr4yak/parser"
"github.com/yaklang/yaklang/common/yak/antlr4yak/yakvm"
)
func (y *YakCompiler) VisitAssignExpressionStmt(raw yak.IAssignExpressionStmtContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.AssignExpressionStmtContext)
if i == nil {
return nil
}
recoverRange := y.SetRange(i.BaseParserRuleContext)
defer recoverRange()
if i.AssignExpression() != nil {
y.VisitAssignExpression(i.AssignExpression())
}
return nil
}
func (y *YakCompiler) VisitAssignExpression(raw yak.IAssignExpressionContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.AssignExpressionContext)
if i == nil {
return nil
}
recoverRange := y.SetRange(i.BaseParserRuleContext)
defer recoverRange()
// assign eq / colon assign eq
if op, op2 := i.AssignEq(), i.ColonAssignEq(); op != nil || op2 != nil {
// 赋值语句,应该先 visit 右值,再 visit 左值
recoverFormatBufferFunc := y.switchFormatBuffer()
y.VisitExpressionList(i.ExpressionList())
buf := recoverFormatBufferFunc()
forceAssign := op2 != nil
y.VisitLeftExpressionList(forceAssign, i.LeftExpressionList())
if op != nil {
y.writeStringWithWhitespace(op.GetText())
} else {
y.writeStringWithWhitespace(op2.GetText())
}
y.writeString(buf)
y.pushOperator(yakvm.OpAssign)
return nil
}
if i.PlusPlus() != nil { // ++
y.VisitLeftExpression(false, i.LeftExpression())
y.writeString("++")
y.pushOperator(yakvm.OpPlusPlus)
return nil
} else if i.SubSub() != nil { // --
y.VisitLeftExpression(false, i.LeftExpression())
y.writeString("--")
y.pushOperator(yakvm.OpMinusMinus)
return nil
}
if op := i.InplaceAssignOperator(); op != nil {
recoverFormatBufferFunc := y.switchFormatBuffer()
y.VisitExpression(i.Expression())
buf := recoverFormatBufferFunc()
y.VisitLeftExpression(false, i.LeftExpression())
y.writeStringWithWhitespace(op.GetText())
y.writeString(buf)
switch op.GetText() {
case "+=":
y.pushOperator(yakvm.OpPlusEq)
case "-=":
y.pushOperator(yakvm.OpMinusEq)
case "*=":
y.pushOperator(yakvm.OpMulEq)
case "/=":
y.pushOperator(yakvm.OpDivEq)
case "%=":
y.pushOperator(yakvm.OpModEq)
case "<<=":
y.pushOperator(yakvm.OpShlEq)
case ">>=":
y.pushOperator(yakvm.OpShrEq)
case "&=":
y.pushOperator(yakvm.OpAndEq)
case "|=":
y.pushOperator(yakvm.OpOrEq)
case "^=":
y.pushOperator(yakvm.OpXorEq)
case "&^=":
y.pushOperator(yakvm.OpAndNotEq)
default:
y.panicCompilerError(notImplemented, op.GetText())
}
}
return nil
}
func (y *YakCompiler) VisitLeftExpressionList(forceNewSymbol bool, raw yak.ILeftExpressionListContext) int {
if y == nil || raw == nil {
return -1
}
i, _ := raw.(*yak.LeftExpressionListContext)
if i == nil {
return -1
}
recoverRange := y.SetRange(i.BaseParserRuleContext)
defer recoverRange()
allExpr := i.AllLeftExpression()
lenOfAllExpr := len(allExpr)
for index, le := range allExpr {
y.VisitLeftExpression(forceNewSymbol, le)
if index < lenOfAllExpr-1 {
y.writeString(", ")
}
}
if allExpr != nil {
y.pushListWithLen(len(allExpr))
}
return len(allExpr)
}
func (y *YakCompiler) VisitLeftExpression(forceNewSymbol bool, raw yak.ILeftExpressionContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.LeftExpressionContext)
if i == nil {
return nil
}
recoverRange := y.SetRange(i.BaseParserRuleContext)
defer recoverRange()
if id := i.Identifier(); id != nil {
// 左值中的 identifier 要创建符号
idName := id.GetText()
y.writeString(idName)
if forceNewSymbol {
id, err := y.currentSymtbl.NewSymbolWithReturn(idName)
if err != nil {
y.panicCompilerError(forceCreateSymbolFailed, idName)
}
y.pushLeftRef(id)
return nil
}
var sym, ok = y.currentSymtbl.GetSymbolByVariableName(idName)
if !ok {
var err error
sym, err = y.currentSymtbl.NewSymbolWithReturn(idName)
if err != nil {
y.panicCompilerError(autoCreateSymbolFailed, idName)
}
}
y.pushLeftRef(sym)
return nil
}
if e := i.Expression(); e != nil {
y.VisitExpression(e)
/*
a[1] = 1
push 1
list 1
push slice
push index
list 2 // 组合 slice[index] 放入栈
list 1
assign
*/
if s := i.LeftSliceCall(); s != nil {
y.VisitLeftSliceCall(s)
// 这里复用 list 的操作,由 assign 判断后续的值
// list 2 可以把上述两个值组合成一个可以被赋值的操作
// 一定小心,不要乱改
y.pushListWithLen(2)
return nil
}
if m := i.LeftMemberCall(); m != nil {
y.VisitLeftMemberCall(m)
y.pushListWithLen(2)
return nil
}
return nil
}
return nil
}
func (y *YakCompiler) VisitLeftMemberCall(raw yak.ILeftMemberCallContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.LeftMemberCallContext)
if i == nil {
return nil
}
recoverRange := y.SetRange(i.BaseParserRuleContext)
defer recoverRange()
y.writeString(".")
if i := i.Identifier(); i != nil {
idName := i.GetText()
y.writeString(idName)
y.pushString(idName, idName)
return nil
}
if i := i.IdentifierWithDollar(); i != nil {
varName := i.GetText()
y.writeString(varName)
for strings.HasPrefix(varName, "$") {
varName = varName[1:]
}
symbolId, ok := y.currentSymtbl.GetSymbolByVariableName(varName)
if !ok {
y.panicCompilerError(notFoundDollarVariable, varName)
}
y.pushRef(symbolId)
return nil
}
y.panicCompilerError(bugMembercall)
return nil
}
func (y *YakCompiler) VisitLeftSliceCall(raw yak.ILeftSliceCallContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.LeftSliceCallContext)
if i == nil {
return nil
}
recoverRange := y.SetRange(i.BaseParserRuleContext)
defer recoverRange()
y.writeString("[")
y.VisitExpression(i.Expression())
y.writeString("]")
return nil
}
func (y *YakCompiler) VisitDeclareVariableExpression(raw yak.IDeclareVariableExpressionContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.DeclareVariableExpressionContext)
if i == nil {
return nil
}
if s := i.DeclareVariableOnly(); s != nil {
y.VisitDeclareVariableOnly(s)
return nil
}
if s := i.DeclareAndAssignExpression(); s != nil {
y.VisitDeclareAndAssignExpression(s)
return nil
}
return nil
}
func (y *YakCompiler) VisitDeclareAndAssignExpression(raw yak.IDeclareAndAssignExpressionContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.DeclareAndAssignExpressionContext)
if i == nil {
return nil
}
y.writeString("var ")
// 赋值语句,应该先 visit 右值,再 visit 左值
recoverFormat := y.switchFormatBuffer()
y.VisitExpressionList(i.ExpressionList())
buf := recoverFormat()
y.VisitLeftExpressionList(true, i.LeftExpressionList())
y.writeString(" = ")
y.writeString(buf)
y.pushOperator(yakvm.OpAssign)
return nil
}
func (y *YakCompiler) VisitDeclareVariableOnly(raw yak.IDeclareVariableOnlyContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.DeclareVariableOnlyContext)
if i == nil {
return nil
}
var count = len(i.AllIdentifier())
for i := 0; i < count; i++ {
y.pushUndefined()
}
y.pushListWithLen(count)
// format
y.writeString("var ")
for index, idCtx := range i.AllIdentifier() {
id, err := y.currentSymtbl.NewSymbolWithReturn(idCtx.GetText())
if err != nil {
y.panicCompilerError(CreateSymbolError, err.Error())
}
y.pushLeftRef(id)
// format variables
y.writeString(idCtx.GetText())
if index != count-1 {
y.writeString(", ")
}
}
y.pushListWithLen(count)
y.pushOperator(yakvm.OpAssign)
return nil
}
func (y *YakCompiler) VisitDeclareVariableExpressionStmt(raw yak.IDeclareVariableExpressionStmtContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.DeclareVariableExpressionStmtContext)
if i == nil {
return nil
}
recoverRange := y.SetRange(i.BaseParserRuleContext)
defer recoverRange()
y.VisitDeclareVariableExpression(i.DeclareVariableExpression())
return nil
}