-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbytecode.go
311 lines (281 loc) · 6.19 KB
/
bytecode.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
package dicescript
import (
"fmt"
"strconv"
)
type CodeType uint8
type ByteCode struct {
T CodeType
Value any
}
const (
typePushIntNumber CodeType = iota
typePushFloatNumber
typePushString
typePushArray
typePushDict
typePushRange
typePushComputed
typePushNull
typePushThis
typePushGlobal
typePushFunction
typePushLast
typePushDefaultExpr
typeLoadFormatString
typeLoadName
typeLoadNameWithDetail
typeLoadNameRaw // 如遇到computed,这个版本不取出其内容
typeStoreName
typeStoreNameGlobal
typeStoreNameLocal
typeInvoke
typeInvokeSelf
typeItemGet
typeItemSet
typeAttrGet
typeAttrSet
typeSliceGet
typeSliceSet
typeAdd // 注意,修改顺序时一定要顺带修改下面的数组
typeSubtract
typeMultiply
typeDivide
typeModulus
typeExponentiation
typeNullCoalescing
typeCompLT
typeCompLE
typeCompEQ
typeCompNE
typeCompGE
typeCompGT
typeBitwiseAnd
typeBitwiseOr
typeLogicAnd
typeLogicOr
typeNegation
typePositive
typeDiceInit
typeDiceSetTimes
typeDiceSetKeepLowNum
typeDiceSetKeepHighNum
typeDiceSetDropLowNum
typeDiceSetDropHighNum
typeDiceSetMin
typeDiceSetMax
typeDice
typeDiceCocPenalty
typeDiceCocBonus
typeDiceFate
typeDiceWod
typeWodSetInit // 重置参数
typeWodSetPool // 设置骰池(骰数)
typeWodSetPoints // 面数
typeWodSetThreshold // 阈值 >=
typeWodSetThresholdQ // 阈值 <=
typeDiceDC
typeDCSetInit
typeDCSetPool // 骰池
typeDCSetPoints // 面数
typeHalt
typeDetailMark
typePop
typePopN
typeNop
typeJmp
typeJe
typeJne
typeJeDup
typeReturn
typeFStringBlockPush // fstring标记 用于栈平衡
typeFStringBlockPop
typeBlockPush
typeBlockPop
typeStSetName
typeStModify
typeStX0
typeStX1
)
func (code *ByteCode) CodeString() string {
switch code.T {
case typePushIntNumber:
return "push.int " + strconv.FormatInt(int64(code.Value.(IntType)), 10)
case typePushFloatNumber:
return "push.flt " + strconv.FormatFloat(code.Value.(float64), 'f', 2, 64)
case typePushString:
return "push.str " + code.Value.(string)
case typePushRange:
return "push.range"
case typePushArray:
return "push.arr " + strconv.FormatInt(int64(code.Value.(IntType)), 10)
case typePushDict:
return "push.dict " + strconv.FormatInt(int64(code.Value.(IntType)), 10)
case typePushComputed:
computed, _ := code.Value.(*VMValue).ReadComputed()
return "push.computed " + computed.Expr
case typePushNull:
return "push.null"
case typePushThis:
return "push.this"
case typePushGlobal:
return "push.global"
case typePushFunction:
computed, _ := code.Value.(*VMValue).ReadFunctionData()
return "push.func " + computed.Name
case typeInvoke:
return "invoke " + strconv.FormatInt(int64(code.Value.(IntType)), 10)
case typeInvokeSelf:
return "invoke.self " + code.Value.(string)
case typeItemGet:
return "item.get"
case typeItemSet:
return "item.set"
case typeAttrSet:
return "attr.set " + code.Value.(string)
case typeAttrGet:
return "attr.get " + code.Value.(string)
case typeSliceGet:
return "slice.get"
case typeSliceSet:
return "slice.set"
case typeAdd:
return "add"
case typeSubtract:
return "sub"
case typeMultiply:
return "mul"
case typeDivide:
return "div"
case typeModulus:
return "mod"
case typeExponentiation:
return "pow"
case typeNullCoalescing:
return "nullCoalescing"
case typeLogicAnd:
return "and"
case typeLogicOr:
return "or"
case typeBitwiseAnd:
return "&"
case typeBitwiseOr:
return "|"
case typeNegation:
return "neg"
case typePositive:
return "pos"
case typeDiceInit:
return "dice.init"
case typeDiceSetTimes:
return "dice.setTimes"
case typeDiceSetKeepLowNum:
return "dice.setKeepLow"
case typeDiceSetKeepHighNum:
return "dice.setKeepHigh"
case typeDiceSetDropLowNum:
return "dice.setDropLow"
case typeDiceSetDropHighNum:
return "dice.setDropHigh"
case typeDiceSetMin:
return "dice.setMin"
case typeDiceSetMax:
return "dice.setMax"
case typeDice:
return "dice"
case typeDiceCocPenalty:
return "coc.penalty"
case typeDiceCocBonus:
return "coc.bonus"
case typeDiceFate:
return "dice.fate"
case typeWodSetInit:
return "wod.init"
case typeWodSetPool:
return "wod.pool"
case typeWodSetPoints:
return "wod.points"
case typeWodSetThreshold:
return "wod.threshold"
case typeWodSetThresholdQ:
return "wod.thresholdQ"
case typeDiceDC:
return "dice.dc"
case typeDCSetInit:
return "dc.setInit"
case typeDCSetPool:
return "dc.setPool"
case typeDCSetPoints:
return "dc.setPoints"
case typeDiceWod:
return "dice.wod"
case typeLoadName:
return "ld " + code.Value.(string)
case typeLoadNameWithDetail:
return "ld.d " + code.Value.(string)
case typeLoadNameRaw:
return "ld.raw " + code.Value.(string)
case typeLoadFormatString:
return fmt.Sprintf("ld.fs %d", code.Value)
case typeStoreName:
return fmt.Sprintf("store %s", code.Value)
case typeStoreNameGlobal:
return fmt.Sprintf("store.global %s", code.Value)
case typeStoreNameLocal:
return fmt.Sprintf("store.local %s", code.Value)
case typeHalt:
return "halt"
case typeDetailMark:
v := code.Value.(BufferSpan)
return fmt.Sprintf("mark.detail %d, %d", v.Begin, v.End)
case typeJmp:
return fmt.Sprintf("jmp %d", code.Value)
case typeJe:
return fmt.Sprintf("je %d", code.Value)
case typeJeDup:
return fmt.Sprintf("je.dup %d", code.Value)
case typeJne:
return fmt.Sprintf("jne %d", code.Value)
case typeCompLT:
return "comp.lt"
case typeCompLE:
return "comp.le"
case typeCompEQ:
return "comp.eq"
case typeCompNE:
return "comp.ne"
case typeCompGE:
return "comp.ge"
case typeCompGT:
return "comp.gt"
case typePushLast:
return "push.last"
case typePushDefaultExpr:
return "push.def_expr"
case typePop:
return "pop"
case typePopN:
return fmt.Sprintf("popn %d", code.Value)
case typeNop:
return "nop"
case typeReturn:
return "ret"
case typeBlockPush:
return "block.push"
case typeBlockPop:
return "block.pop"
case typeFStringBlockPush:
return "fstr.block.push"
case typeFStringBlockPop:
return "fstr.block.pop"
case typeStSetName:
return "st.set"
case typeStModify:
return fmt.Sprintf("st.mod %s", code.Value)
case typeStX0:
return "st.x0"
case typeStX1:
return "st.x1"
}
return ""
}