-
Notifications
You must be signed in to change notification settings - Fork 6
/
parser.go
378 lines (318 loc) · 8.79 KB
/
parser.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
368
369
370
371
372
373
374
375
376
377
378
package dicescript
import (
"errors"
"strconv"
)
type ParserData struct {
code []ByteCode
codeIndex int
Config RollConfig
flagsStack []RollConfig
counterStack []IntType // f-string 嵌套计数,在解析时中起作用
varnameStack []string // 另一个解析用栈
jmpStack []IntType
breakStack []IntType // break,用时创建
continueStack []IntType // continue用,用时创建
loopInfo []struct {
continueIndex int
breakIndex int
}
loopLayer int // 当前loop层数
codeStack []struct {
code []ByteCode
index int
textPos int
}
}
type BufferSpan struct {
Begin IntType
End IntType
Ret *VMValue
Text string
Expr string // 如果存在Expr,用来替代等号左边的内容,不存在的话用Begin和End从原文提取
Tag string // 来源标记
}
func (e *ParserData) LoopBegin() {
e.loopLayer += 1
e.loopInfo = append(e.loopInfo, struct {
continueIndex int
breakIndex int
}{continueIndex: len(e.continueStack), breakIndex: len(e.breakStack)})
}
func (e *ParserData) LoopEnd() {
e.loopLayer -= 1
info := e.loopInfo[len(e.loopInfo)-1]
e.continueStack = e.continueStack[:info.continueIndex]
e.breakStack = e.breakStack[:info.breakIndex]
e.loopInfo = e.loopInfo[:len(e.loopInfo)-1]
}
func (e *ParserData) checkStackOverflow() bool {
if e.codeIndex >= len(e.code) {
need := len(e.code) * 2
if need <= 8192 {
newCode := make([]ByteCode, need)
copy(newCode, e.code)
e.code = newCode
} else {
// e.Error = errors.New("E1:指令虚拟机栈溢出,请不要发送过长的指令")
return true
}
}
return false
}
func (e *ParserData) WriteCode(T CodeType, value any) {
if e.checkStackOverflow() {
return
}
c := &e.code[e.codeIndex]
c.T = T
c.Value = value
e.codeIndex += 1
}
func (p *ParserData) AddDiceDetail(begin IntType, end IntType) {
p.WriteCode(typeDetailMark, BufferSpan{Begin: begin, End: end})
}
func (e *ParserData) AddOp(operator CodeType) {
var val interface{} = nil
if operator == typeJne || operator == typeJmp {
val = IntType(0)
}
e.WriteCode(operator, val)
}
func (e *ParserData) AddLoadName(value string) {
e.WriteCode(typeLoadName, value)
}
func (e *ParserData) PushIntNumber(value string) {
val, _ := strconv.ParseInt(value, 10, 64)
e.WriteCode(typePushIntNumber, IntType(val))
}
func (e *ParserData) PushStr(value string) {
e.WriteCode(typePushString, value)
}
func (e *ParserData) PushArray(value IntType) {
e.WriteCode(typePushArray, value)
}
func (e *ParserData) PushDict(value IntType) {
e.WriteCode(typePushDict, value)
}
func (e *ParserData) PushNull() {
e.WriteCode(typePushNull, nil)
}
func (e *ParserData) PushThis() {
e.WriteCode(typePushThis, nil)
}
func (e *ParserData) PushGlobal() {
e.WriteCode(typePushGlobal, nil)
}
func (e *ParserData) AddFormatString(num IntType) {
// e.PushStr(value)
e.WriteCode(typeLoadFormatString, num) // num
}
func (e *ParserData) PushFloatNumber(value string) {
val, _ := strconv.ParseFloat(value, 64)
e.WriteCode(typePushFloatNumber, float64(val))
}
func (e *ParserData) AddStName() {
e.WriteCode(typeStSetName, nil)
}
type StInfo struct {
Op string
Text string
}
func (e *ParserData) AddStModify(op string, text string) {
e.WriteCode(typeStModify, StInfo{op, text})
}
func (e *ParserData) AddStore(text string) {
e.WriteCode(typeStoreName, text)
}
func (e *ParserData) AddStoreGlobal(text string) {
e.WriteCode(typeStoreNameGlobal, text)
}
func (e *ParserData) AddStoreLocal(text string) {
e.WriteCode(typeStoreNameLocal, text)
}
func (e *ParserData) NamePush(test string) {
e.varnameStack = append(e.varnameStack, test)
}
func (e *ParserData) NamePop() string {
last := len(e.varnameStack) - 1
val := e.varnameStack[last]
e.varnameStack = e.varnameStack[:last]
return val
}
func (e *ParserData) OffsetPush() {
e.jmpStack = append(e.jmpStack, IntType(e.codeIndex)-1)
}
func (p *ParserData) ContinuePush() error {
if p.loopLayer > 0 {
if p.continueStack == nil {
p.continueStack = []IntType{}
}
p.AddOp(typeJmp)
p.continueStack = append(p.continueStack, IntType(p.codeIndex)-1)
} else {
return errors.New("循环外不能放置continue")
}
return nil
}
func (p *ParserData) ContinueSet(offsetB int) {
if p.continueStack != nil {
info := p.loopInfo[len(p.loopInfo)-1]
for _, codeIndex := range p.continueStack[info.continueIndex:] {
lastB := len(p.jmpStack) - 1 - offsetB
jmpIndex := p.jmpStack[lastB]
// 试出来的,这个是对的,那么也许while那个是错的??还是说因为while最后多push了一个jmp呢?
p.code[codeIndex].Value = -(IntType(codeIndex) - jmpIndex)
}
}
}
func (p *ParserData) BreakSet() {
if p.breakStack != nil {
info := p.loopInfo[len(p.loopInfo)-1]
for _, codeIndex := range p.breakStack[info.breakIndex:] {
p.code[codeIndex].Value = IntType(p.codeIndex) - codeIndex - 1
}
}
}
func (p *ParserData) BreakPush() error {
if p.loopLayer > 0 {
if p.breakStack == nil {
p.breakStack = []IntType{}
}
p.AddOp(typeJmp)
p.breakStack = append(p.breakStack, IntType(p.codeIndex)-1)
return nil
} else {
return errors.New("循环外不能放置break")
}
}
func (e *ParserData) OffsetPopAndSet() {
last := len(e.jmpStack) - 1
codeIndex := e.jmpStack[last]
e.jmpStack = e.jmpStack[:last]
e.code[codeIndex].Value = IntType(IntType(e.codeIndex) - codeIndex - 1)
// fmt.Println("XXXX", e.Code[codeIndex], "|", e.Top, codeIndex)
}
func (e *ParserData) OffsetPopN(num int) {
last := len(e.jmpStack) - num
e.jmpStack = e.jmpStack[:last]
}
func (e *ParserData) OffsetJmpSetX(offsetA int, offsetB int, rev bool) {
lastA := len(e.jmpStack) - 1 - offsetA
lastB := len(e.jmpStack) - 1 - offsetB
codeIndex := e.jmpStack[lastA]
jmpIndex := e.jmpStack[lastB]
if rev {
e.code[codeIndex].Value = -(IntType(e.codeIndex) - jmpIndex - 1)
} else {
e.code[codeIndex].Value = IntType(e.codeIndex) - jmpIndex - 1
}
}
func (e *ParserData) CounterPush() {
e.counterStack = append(e.counterStack, 0)
}
func (e *ParserData) CounterAdd(offset IntType) {
last := len(e.counterStack) - 1
if last != -1 {
e.counterStack[last] += offset
}
}
func (e *ParserData) CounterPop() IntType {
last := len(e.counterStack) - 1
num := e.counterStack[last]
e.counterStack = e.counterStack[:last]
return num
}
func (e *ParserData) FlagsPush() {
e.flagsStack = append(e.flagsStack, e.Config)
}
func (e *ParserData) FlagsPop() {
last := len(e.flagsStack) - 1
e.Config = e.flagsStack[last]
e.flagsStack = e.flagsStack[:last]
}
func (e *ParserData) AddInvokeMethod(name string, paramsNum IntType) {
e.WriteCode(typePushIntNumber, paramsNum)
e.WriteCode(typeInvokeSelf, name)
}
func (e *ParserData) AddInvoke(paramsNum IntType) {
// e.WriteCode(typePushIntNumber, paramsNum)
e.WriteCode(typeInvoke, paramsNum)
}
func fixCodeByOffset(code []ByteCode, offset int) {
for index, i := range code {
switch i.T {
case typeDetailMark:
v := i.Value.(BufferSpan)
v.Begin -= IntType(offset)
v.End -= IntType(offset)
code[index].Value = v
}
}
}
func (p *ParserData) AddStoreComputed(name string, text string) {
code, length, offset := p.CodePop()
fixCodeByOffset(code, offset)
val := NewComputedValRaw(&ComputedData{
Expr: text,
code: code,
codeIndex: length,
})
p.WriteCode(typePushComputed, val)
p.WriteCode(typeStoreName, name)
}
func (p *ParserData) AddStoreComputedOnStack(text string) {
code, length, offset := p.CodePop()
fixCodeByOffset(code, offset)
val := NewComputedValRaw(&ComputedData{
Expr: text,
code: code,
codeIndex: length,
})
p.WriteCode(typePushComputed, val)
}
func (p *ParserData) AddStoreFunction(name string, paramsReversed []string, text string) {
code, length, offset := p.CodePop()
fixCodeByOffset(code, offset)
// 翻转一次
for i, j := 0, len(paramsReversed)-1; i < j; i, j = i+1, j-1 {
paramsReversed[i], paramsReversed[j] = paramsReversed[j], paramsReversed[i]
}
val := NewFunctionValRaw(&FunctionData{
Expr: text,
Name: name,
Params: paramsReversed,
code: code,
codeIndex: length,
})
p.WriteCode(typePushFunction, val)
if name != "" {
p.WriteCode(typeStoreName, name)
}
}
func (p *ParserData) AddAttrSet(objName string, attr string, isRaw bool) {
if isRaw {
p.WriteCode(typeLoadNameRaw, objName)
} else {
p.WriteCode(typeLoadName, objName)
}
p.WriteCode(typeAttrSet, attr)
}
func (p *ParserData) CodePush(textPos int) {
p.codeStack = append(p.codeStack, struct {
code []ByteCode
index int
textPos int
}{code: p.code, index: p.codeIndex, textPos: textPos})
p.code = make([]ByteCode, 256)
p.codeIndex = 0
}
func (p *ParserData) CodePop() ([]ByteCode, int, int) {
lastCode, lastIndex := p.code, p.codeIndex
last := len(p.codeStack) - 1
info := p.codeStack[last]
p.codeStack = p.codeStack[:last]
p.code = info.code
p.codeIndex = info.index
return lastCode, lastIndex, info.textPos
}