-
Notifications
You must be signed in to change notification settings - Fork 45
/
new_switch.go
283 lines (243 loc) · 6.43 KB
/
new_switch.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
package yakast
import (
"fmt"
"strings"
uuid "github.com/google/uuid"
yak "github.com/yaklang/yaklang/common/yak/antlr4yak/parser"
"github.com/yaklang/yaklang/common/yak/antlr4yak/yakvm"
)
/*
cfg:
// switch case, just jump
if a == 1 {
jump body1
break
} else if a==2{
jump body3
fallthought
}else {
jump body-default
}
// body list
body1 {
...
break end-switch (current break counter) // break
...
}
jump end-switch // body1:jump-to-end
body2 {
...
break body-default:start-scope (current break counter) // fallthought
...
}
jump end-switch
body-default {
// body-default:start-scope
...
}
jump end-switch
// end-swich
*/
func (y *YakCompiler) VisitSwitchStmt(raw yak.ISwitchStmtContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.SwitchStmtContext)
if i == nil {
return nil
}
recoverRange := y.SetRange(i.BaseParserRuleContext)
defer recoverRange()
y.writeString("switch ")
var (
defaultCodeAddress int
switchExprIsEmpty bool
jmp2Default *yakvm.Code
)
recoverSymtbl := y.SwitchSymbolTableInNewScope("switch", uuid.New().String())
defer recoverSymtbl()
startIndex := y.GetNextCodeIndex()
y.enterSwitchContext(startIndex)
symbolName := fmt.Sprintf("$switch:%v$", y.GetNextCodeIndex())
expressionResultID, err := y.currentSymtbl.NewSymbolWithReturn(symbolName)
if err != nil {
y.panicCompilerError(CreateSymbolError, symbolName)
}
// 将表达式的值放入符号中
if e := i.Expression(); e != nil {
y.VisitExpression(i.Expression())
y.pushListWithLen(1)
// 设置左值,这个左值是一个新建的符号!
y.pushLeftRef(expressionResultID)
y.pushListWithLen(1)
// 为右值创建一个符号,这个符号为 rightExpressionSymbol
y.pushOperator(yakvm.OpAssign)
y.writeString(" {")
} else {
//y.pushUndefined()
switchExprIsEmpty = true
y.writeString(" {")
}
y.writeNewLine()
allcases := i.AllCase()
lenOfAllCases := len(allcases)
jmpToCase := make([]*yakvm.Code, 0, lenOfAllCases)
jmpToEnd := make([]*yakvm.Code, 0, lenOfAllCases)
_fallthrough := make([]*yakvm.Code, 0)
caseAddress := make([]int, 0, lenOfAllCases)
conditionbuf := make([]string, 0, lenOfAllCases)
// save jump to case with index
pushJumpTrue := func(index int) {
jmpt := y.pushJmpIfTrue()
jmpt.Unary = index
jmpToCase = append(jmpToCase, jmpt)
}
// build check list
for index := range allcases {
recoverFormatBufferFunc := y.switchFormatBuffer()
y.writeString("case ")
if exprs, ok := i.ExpressionList(index).(*yak.ExpressionListContext); ok {
if len(exprs.AllExpression()) == 1 {
// only one expression
y.VisitExpression(exprs.AllExpression()[0])
if !switchExprIsEmpty {
y.pushRef(expressionResultID)
y.pushOperator(yakvm.OpEq)
}
pushJumpTrue(index)
} else {
// multiple expression
for i, e := range exprs.AllExpression() {
y.VisitExpression(e)
if !switchExprIsEmpty {
y.pushRef(expressionResultID)
y.pushOperator(yakvm.OpEq)
}
pushJumpTrue(index)
if i < len(exprs.AllExpression())-1 {
y.writeString(", ")
}
}
}
}
y.writeString(":")
y.writeNewLine()
buf := recoverFormatBufferFunc()
conditionbuf = append(conditionbuf, buf)
}
jmp2Default = y.pushJmp()
// build body list
for index := range allcases {
// save case body address
stmtAddress := y.GetNextCodeIndex()
caseAddress = append(caseAddress, stmtAddress)
// new scope for body
recoverSymtbl = y.SwitchSymbolTableInNewScope("case", uuid.New().String())
y.incIndent()
recoverFormatBufferFunc := y.switchFormatBuffer()
if stmt, ok := i.StatementList(index).(*yak.StatementListContext); ok {
allStatement := stmt.AllStatement()
lenOfAllStatement := len(allStatement)
for i, istmt := range allStatement {
if istmt == nil {
continue
}
stmt := istmt.(*yak.StatementContext)
// 忽略开头的empty
if i == 0 && stmt.Empty() != nil {
continue
}
y.writeIndent()
if s := stmt.FallthroughStmt(); s != nil {
if y.NowInSwitch() {
y.writeString("fallthrough")
y.writeEOS(stmt.Eos())
// save in _fallthrough
b := y.pushBreak()
b.Unary = index
_fallthrough = append(_fallthrough, b)
continue
}
y.panicCompilerError(fallthroughError)
}
newline := y.VisitStatement(istmt.(*yak.StatementContext))
if i < lenOfAllStatement-1 && newline {
y.writeNewLine()
}
}
}
buf := recoverFormatBufferFunc()
buf = strings.Trim(buf, "\n")
y.writeString(conditionbuf[index] + buf)
y.decIndent()
y.writeNewLine()
// end scope
recoverSymtbl()
// jump to switch end, when body finish
jmpToEnd = append(jmpToEnd, y.pushJmp())
}
// default
if i.Default() != nil {
defaultCodeAddress = y.GetNextCodeIndex()
// default body scope
recoverSymtbl = y.SwitchSymbolTableInNewScope("default", uuid.New().String())
y.writeString("default:")
y.writeNewLine()
y.incIndent()
recoverFormatBufferFunc := y.switchFormatBuffer()
stmts := i.StatementList(len(allcases)).(*yak.StatementListContext)
// y.VisitStatementList(stmts)
allStatement := stmts.AllStatement()
lenOfAllStatement := len(allStatement)
for i, istmt := range allStatement {
if istmt == nil {
continue
}
stmt := istmt.(*yak.StatementContext)
// 忽略开头的empty
if i == 0 && stmt.Empty() != nil {
continue
}
y.writeIndent()
newline := y.VisitStatement(istmt.(*yak.StatementContext))
if i < lenOfAllStatement-1 && newline {
y.writeNewLine()
}
}
buf := recoverFormatBufferFunc()
buf = strings.Trim(buf, "\n")
y.writeString(buf)
y.decIndent()
y.writeNewLine()
// end scope
recoverSymtbl()
}
// handler jump case
for _, jmp := range jmpToCase {
jmp.Unary = caseAddress[jmp.Unary]
}
endCodewithScopeEnd := y.GetNextCodeIndex()
if defaultCodeAddress == 0 {
defaultCodeAddress = endCodewithScopeEnd
}
jmp2Default.Unary = defaultCodeAddress
// handler fallthough
// 设置fallthrough跳转到下个statementlist的位置
for _, b := range _fallthrough {
// 最后一个的fallthrough应该跳转到default
if b.Unary == lenOfAllCases-1 {
b.Unary = defaultCodeAddress
} else {
b.Unary = caseAddress[b.Unary+1]
}
b.Op2 = yakvm.NewIntValue(2)
}
// 设置跳转到switch结尾的位置
for _, jmp := range jmpToEnd {
jmp.Unary = endCodewithScopeEnd
}
// handler break
y.exitSwitchContext(endCodewithScopeEnd)
y.writeString("}")
return nil
}