-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrollvm.go
1104 lines (986 loc) · 27.6 KB
/
rollvm.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2022 fy <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package dicescript
import (
"bytes"
"errors"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode"
)
func NewVM() *Context {
// 创建parser
p := &Context{}
p.Init()
return p
}
// RunExpr 注: 最后不一定叫这个名字,这个函数作用是,即使当前vm被占用,也能执行语句,是为了指令hack而服务的
func (ctx *Context) RunExpr(value string, useUpCtxLocal bool) (*VMValue, error) {
val := NewFunctionValRaw(&FunctionData{
Expr: value,
Name: "",
Params: nil,
code: nil,
codeIndex: 0,
})
oldErr := ctx.Error // 注意,这一储存在并发状态下可能并不准确
v := val.FuncInvokeRaw(ctx, nil, useUpCtxLocal)
curErr := ctx.Error
ctx.Error = oldErr // 这是临时方案,本质上不应对当前ctx的状态做出改变
return v, curErr
}
// GetErrorText 主要用于js,因为ctx.Error是数组,在js那边不被当做正常的异常对象,所以会报错
func (ctx *Context) GetErrorText() string {
if ctx.Error != nil {
return ctx.Error.Error()
}
return ""
}
func (ctx *Context) GetParsedOffset() int {
return ctx.parser.pt.offset
}
func (ctx *Context) Parse(value string) error {
// 检测是否正在执行,正在执行则使用新的上下文
if ctx.IsRunning {
return errors.New("正在执行中,无法执行新的语句")
}
p := newParser("", []byte(value))
ctx.parser = p
d := p.cur.data
// p.debug = true
// 初始化指令栈,默认指令长度512条,会自动增长
d.code = make([]ByteCode, 512)
d.codeIndex = 0
d.Config = ctx.Config
ctx.Error = nil
ctx.NumOpCount = 0
ctx.detailCache = ""
// 开始解析,编译字节码
if ctx.Config.ParseExprLimit != 0 {
p.maxExprCnt = ctx.Config.ParseExprLimit
}
_, err := p.parse(nil)
if err != nil {
ctx.Error = err
return err
}
ctx.code = p.cur.data.code
ctx.codeIndex = p.cur.data.codeIndex
return nil
}
// IsCalculateExists 只有表达式被解析后,才能被调用,暂不考虑存在invoke指令的情况
func (ctx *Context) IsCalculateExists() bool {
for _, i := range ctx.code {
switch i.T {
case typeDice, typeDiceDC, typeDiceWod, typeDiceFate, typeDiceCocBonus, typeDiceCocPenalty:
return true
case typeAdd, typeSubtract, typeMultiply, typeDivide, typeModulus, typeExponentiation:
return true
case typeInvoke, typeInvokeSelf:
return true
}
}
return false
}
func (ctx *Context) RunAfterParsed() error {
ctx.IsComputedLoaded = false
// 以下为eval
ctx.evaluate()
if ctx.Error != nil {
return ctx.Error
}
// 获取结果
if ctx.top != 0 {
ctx.Ret = &ctx.stack[ctx.top-1]
} else {
ctx.Ret = NewNullVal()
}
// 给出VM解析完句子后的剩余文本
offset := ctx.parser.pt.offset
matched := strings.TrimRightFunc(string(ctx.parser.data[:offset]), func(r rune) bool {
return unicode.IsSpace(r)
})
ctx.Matched = matched
ctx.RestInput = string(ctx.parser.data[len(matched):])
return nil
}
// Run 执行给定语句
func (ctx *Context) Run(value string) error {
if err := ctx.Parse(value); err != nil {
return err
}
return ctx.RunAfterParsed()
}
type spanByBegin []BufferSpan
func (a spanByBegin) Len() int { return len(a) }
func (a spanByBegin) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a spanByBegin) Less(i, j int) bool { return a[i].Begin < a[j].Begin }
type spanByEnd []BufferSpan
func (a spanByEnd) Len() int { return len(a) }
func (a spanByEnd) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a spanByEnd) Less(i, j int) bool { return a[i].End < a[j].End }
// getE5 := func() error {
// return errors.New("E5: 超出单指令允许算力,不予计算")
// }
/*
计算过程标注
基础格式1: 算式=结果
基础格式2: 算式=多个过程=结果
过程: 值[小算式=值=文本标注, 子过程] - 即 span.Ret[expr=span.Ret=span.Text, subDetails]
多个过程: 过程的集合,如 a + b,如果过程只有一个,那么多个过程与过程相同
文本标注区分: 目前骰点式有文本标注,如2d1,1d2,f2,a10等,加减乘除等算式没有,计算类型和变量读取有,函数调用有
补丁: 过程进行调整,取消"=值"这部分输出
分为三种情况再进行细分
1. 直接相等,一级,单项,无子句
d10=10[d10=10=10]=10 首先进行一次省略,即文本标注如果等于值,那么=10=10 可以省略为=10
基于d10=10[d10=10]=10二次省略,如果单项过程的文本标注=值,进行省略,那么 d10=10[d10]=10
基于d10=10[d10]=10三次省略,如果只有一项过程,且没有子项,且大算式=小算式,过程可全部省略
最终: d10=10
疑点: 2d10=12[2d10=12=2+10]=12 这种三项都不满足,但是2d10=12=2+10是否过于繁琐?
2.一级,单项,有子句
(2d1)d1=2[(2d1)d1=2=1+1,2d1=2]=2
(2d1+((2d1)d1)d1)d1=4[(2d1+((2d1)d1)d1)d1=4=1+1+1+1,2d1=2,2d1=2,(2d1)d1=2,((2d1)d1)d1=2]=4
一下不知道怎么弄了
3.组合算式
种类1和种类2使用加减乘除等符号相连
也包括 [] {} 等数据组合形式
值得注意的是,目前 [2d1,2]kl 这种形式,2d1和2都属于一级,未来可能会修改。
同理还有 [2d1,2].kl() 这个与上面等价,只是写法不同
*/
func (ctx *Context) makeDetailStr(details []BufferSpan) string {
offset := ctx.parser.pt.offset
if ctx.Config.CustomMakeDetailFunc != nil {
return ctx.Config.CustomMakeDetailFunc(ctx, details, ctx.parser.data, offset)
}
detailResult := ctx.parser.data[:offset]
curPoint := IntType(-1) // nolint
lastEnd := IntType(-1) // nolint
type Group struct {
begin IntType
end IntType
tag string
spans []BufferSpan
val *VMValue
}
var m []Group
for _, i := range details {
// fmt.Println("?", i, lastEnd)
if i.Begin > lastEnd {
curPoint = i.Begin
m = append(m, Group{begin: curPoint, end: i.End, tag: i.Tag, spans: []BufferSpan{i}, val: i.Ret})
} else {
m[len(m)-1].spans = append(m[len(m)-1].spans, i)
if i.End > m[len(m)-1].end {
m[len(m)-1].end = i.End
}
}
if i.End > lastEnd {
lastEnd = i.End
}
}
for i := len(m) - 1; i >= 0; i-- {
buf := bytes.Buffer{}
writeBuf := func(p []byte) {
buf.Write(p)
}
writeBufStr := func(s string) {
buf.WriteString(s)
}
item := m[i]
size := len(item.spans)
sort.Sort(spanByEnd(item.spans))
last := item.spans[size-1]
subDetailsText := ""
if size > 1 {
// 次级结果,如 (10d3)d5 中,此处为10d3的结果
// 例如 (10d3)d5=63[(10d3)d5=...,10d3=19]
for j := 0; j < len(item.spans)-1; j++ {
span := item.spans[j]
subDetailsText += "," + string(detailResult[span.Begin:span.End]) + "=" + span.Ret.ToString()
}
}
exprText := last.Expr
baseExprText := string(detailResult[item.begin:item.end])
if last.Expr == "" {
exprText = baseExprText
}
writeBuf(detailResult[:item.begin])
// 主体结果部分,如 (10d3)d5=63[(10d3)d5=2+2+2+5+2+5+5+4+1+3+4+1+4+5+4+3+4+5+2,10d3=19]
partRet := last.Ret.ToString()
detail := "[" + exprText
if last.Text != "" && partRet != last.Text { // 规则1.1
detail += "=" + last.Text
}
switch item.tag {
case "load.computed":
detail += "=" + partRet
}
detail += subDetailsText + "]"
if len(m) == 1 && detail == "["+baseExprText+"]" {
detail = "" // 规则1.3
}
if len(detail) > 400 {
detail = "[略]"
}
writeBufStr(partRet + detail)
writeBuf(detailResult[item.end:])
detailResult = buf.Bytes()
}
detailStr := string(detailResult)
if detailStr == ctx.Ret.ToString() {
detailStr = "" // 如果detail和结果值完全一致,那么将其置空
}
return detailStr
}
func (ctx *Context) evaluate() {
ctx.top = 0
ctx.stack = make([]VMValue, 1000)
ctx.IsRunning = true
stack := ctx.stack
defer func() {
ctx.IsRunning = false // 如果程序崩掉,不过halt
}()
e := ctx
// ctx := &e.Context
var details []BufferSpan
numOpCountAdd := func(count IntType) bool {
e.NumOpCount += count
if ctx.Config.OpCountLimit > 0 && e.NumOpCount > ctx.Config.OpCountLimit {
ctx.Error = errors.New("允许算力上限")
return true
}
return false
}
diceStateIndex := -1
var diceStates []struct {
times IntType // 次数,如 2d10,times为2
isKeepLH IntType // 为1对应取低个数kl,为2对应取高个数kh,3为丢弃低个数dl,4为丢弃高个数dh
lowNum IntType
highNum IntType
min *IntType
max *IntType
}
diceInit := func() {
diceStateIndex += 1
data := struct {
times IntType // 次数,如 2d10,times为2
isKeepLH IntType // 为1对应取低个数,为2对应取高个数
lowNum IntType
highNum IntType
min *IntType
max *IntType
}{
times: 1,
}
if diceStateIndex >= len(diceStates) {
diceStates = append(diceStates, data)
} else {
// 其实我不太清楚这样是否对效率有提升。。
diceStates[diceStateIndex] = data
}
}
var wodState struct {
pool IntType
points IntType
threshold IntType
isGE bool
}
wodInit := func() {
wodState.pool = 1
wodState.points = 10 // 面数,默认d10
wodState.threshold = 8 // 成功线,默认9
wodState.isGE = true
}
var dcState struct {
pool IntType
points IntType
}
dcInit := func() {
dcState.pool = 1 // 骰数,默认1
dcState.points = 10 // 面数,默认d10
}
solveDetail := func() {
if !ctx.forceSolveDetail && ctx.subThreadDepth != 0 {
return
}
sort.Sort(spanByBegin(details))
ctx.DetailSpans = details
}
var lastPop *VMValue
stackPop := func() *VMValue {
v := &e.stack[e.top-1]
e.top -= 1
lastPop = v
return v
}
stackPop2 := func() (*VMValue, *VMValue) {
v2, v1 := stackPop(), stackPop()
lastPop = v1
return v1, v2
}
stackPopN := func(num IntType) []*VMValue {
var data []*VMValue
for i := IntType(0); i < num; i++ {
data = append(data, stackPop().Clone()) // 复制一遍规避栈问题
}
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
data[i], data[j] = data[j], data[i]
}
if num >= 1 {
lastPop = data[0]
}
return data
}
stackPush := func(v *VMValue) {
e.stack[e.top] = *v
e.top += 1
}
getRollMode := func() int {
if ctx.Config.DiceMinMode {
return -1
}
if ctx.Config.DiceMaxMode {
return 1
}
return 0
}
var fstrBlockStack [20]int
var fstrBlockIndex int
var blockStack [20]int // TODO: 如果在while循环中return会使得 blockIndex+1,用完之后就不能用了
var blockIndex int
startTime := time.Now().UnixMilli()
for opIndex := 0; opIndex < e.codeIndex; opIndex += 1 {
numOpCountAdd(1)
if ctx.Error == nil && e.top == len(stack) {
ctx.Error = errors.New("执行栈到达溢出线")
}
if ctx.Error != nil {
return
}
code := e.code[opIndex]
cIndex := fmt.Sprintf("%d/%d", opIndex+1, e.codeIndex)
if ctx.Config.PrintBytecode {
var subThread string
if ctx.subThreadDepth != 0 {
subThread = fmt.Sprintf(" S%d", ctx.subThreadDepth)
}
fmt.Printf("!!! %-20s %s %dms%s\n", code.CodeString(), cIndex, time.Now().UnixMilli()-startTime, subThread)
}
switch code.T {
case typePushIntNumber:
stack[e.top].TypeId = VMTypeInt
stack[e.top].Value = code.Value
e.top++
case typePushFloatNumber:
stack[e.top].TypeId = VMTypeFloat
stack[e.top].Value = code.Value
e.top++
case typePushString:
s := code.Value.(string)
stack[e.top].TypeId = VMTypeString
stack[e.top].Value = s
e.top++
case typePushArray:
num := code.Value.(IntType)
stackPush(NewArrayVal(stackPopN(num)...))
case typePushDict:
num := code.Value.(IntType)
items := stackPopN(num * 2)
dict, err := NewDictValWithArray(items...)
if err != nil {
e.Error = err
return
}
stackPush(dict.V())
case typePushComputed, typePushFunction:
val := code.Value.(*VMValue)
stackPush(val)
case typePushNull:
stackPush(NewNullVal())
case typePushThis:
stackPush(vmValueNewLocal())
// case typePushGlobal:
// stackPush(vmValueNewGlobal())
case typePushRange:
a, b := stackPop2()
_a, ok1 := a.ReadInt()
_b, ok2 := b.ReadInt()
if !(ok1 && ok2) {
ctx.Error = errors.New("左右两个区间必须都是数字类型")
return
}
step := IntType(1)
length := _b - _a
if length < 0 {
step = -1
length = -length
}
length += 1
if length > 512 {
ctx.Error = errors.New("不能一次性创建过长的数组")
return
}
arr := make([]*VMValue, length)
index := 0
for i := _a; ; i += step {
arr[index] = NewIntVal(i)
index++
if i == _b {
break
}
}
stackPush(NewArrayVal(arr...))
case typePushLast:
if lastPop == nil {
ctx.Error = errors.New("非法调用指令 push.last")
return
}
stackPush(lastPop)
case typePushDefaultExpr:
// 创建一个函数对象,然后调用它
if ctx.Config.DefaultDiceSideExpr != "" {
var val *VMValue
// 检查缓存
if ctx.Config.defaultDiceSideExprCacheFunc != nil {
fd, ok := ctx.Config.defaultDiceSideExprCacheFunc.ReadFunctionData()
if ok {
if fd.Expr == ctx.Config.DefaultDiceSideExpr {
val = ctx.Config.defaultDiceSideExprCacheFunc
}
}
}
if val == nil {
val = NewFunctionValRaw(&FunctionData{
Expr: ctx.Config.DefaultDiceSideExpr,
Name: "",
Params: nil,
code: nil,
codeIndex: 0,
})
ctx.Config.defaultDiceSideExprCacheFunc = val
}
v := val.FuncInvoke(ctx, nil)
if ctx.Error != nil {
return
}
stackPush(v)
} else {
stackPush(NewIntVal(100))
}
d := &details[len(details)-1]
dText := string(ctx.parser.data[d.Begin:d.End])
if !regexp.MustCompile("[dD][优優劣][势勢]").MatchString(dText) {
s := &diceStates[diceStateIndex]
if s.times > 1 {
d.Expr = fmt.Sprintf("%dD%s", s.times, stack[e.top-1].ToString())
} else {
d.Expr = fmt.Sprintf("D%s", stack[e.top-1].ToString())
}
switch s.isKeepLH {
case 1:
d.Expr += fmt.Sprintf("kl%d", s.lowNum)
case 2:
d.Expr += fmt.Sprintf("kh%d", s.highNum)
case 3:
d.Expr += fmt.Sprintf("dl%d", s.lowNum)
case 4:
d.Expr += fmt.Sprintf("dh%d", s.highNum)
}
if s.min != nil {
d.Expr += fmt.Sprintf("min%d", *s.min)
}
if s.max != nil {
d.Expr += fmt.Sprintf("max%d", *s.max)
}
}
case typeLogicAnd:
a, b := stackPop2()
if !a.AsBool() {
stackPush(a)
} else {
stackPush(b)
}
case typeInvoke:
paramsNum := code.Value.(IntType)
arr := stackPopN(paramsNum)
funcObj := stackPop()
if funcObj.TypeId == VMTypeFunction {
ret := funcObj.FuncInvoke(ctx, arr)
if ctx.Error != nil {
return
}
stackPush(ret)
} else if funcObj.TypeId == VMTypeNativeFunction {
ret := funcObj.FuncInvokeNative(ctx, arr)
if ctx.Error != nil {
return
}
stackPush(ret)
} else {
ctx.Error = fmt.Errorf("类型错误: [%s]无法被调用,必须是一个函数", funcObj.ToString())
}
case typeItemGet:
itemIndex := stackPop()
obj := stackPop()
ret := obj.ItemGet(ctx, itemIndex)
if ctx.Error != nil {
return
}
if ret == nil {
ret = NewNullVal()
}
stackPush(ret)
case typeItemSet:
val := stackPop() // 右值
itemIndex := stackPop() // 下标
obj := stackPop() // 数组 / 对象
obj.ItemSet(ctx, itemIndex, val.Clone())
if ctx.Error != nil {
return
}
case typeAttrSet:
attrVal, obj := stackPop2()
attrName := code.Value.(string)
ret := obj.AttrSet(ctx, attrName, attrVal.Clone())
if ctx.Error == nil && ret == nil {
ctx.Error = errors.New("不支持的类型:当前变量无法用.来设置属性")
}
if ctx.Error != nil {
return
}
case typeAttrGet:
obj := stackPop()
attrName := code.Value.(string)
ret := obj.AttrGet(ctx, attrName)
if ctx.Error != nil {
return
}
if ret == nil {
ctx.Error = errors.New("不支持的类型:当前变量无法用.来取属性")
return
}
stackPush(ret)
case typeSliceGet:
step := stackPop() // step
if step.TypeId != VMTypeNull {
ctx.Error = errors.New("尚不支持分片步长")
return
}
a, b := stackPop2()
obj := stackPop()
ret := obj.GetSliceEx(ctx, a, b)
if ctx.Error != nil {
return
}
stackPush(ret)
case typeSliceSet:
val := stackPop()
step := stackPop() // step
if step.TypeId != VMTypeNull {
ctx.Error = errors.New("尚不支持分片步长")
return
}
a, b := stackPop2()
obj := stackPop()
obj.SetSliceEx(ctx, a, b, val)
if ctx.Error != nil {
return
}
case typeReturn:
solveDetail()
ctx.IsRunning = false
return
case typeHalt:
solveDetail()
ctx.IsRunning = false
return
case typeLoadFormatString:
num := int(code.Value.(IntType))
outStr := ""
for index := 0; index < num; index++ {
var val VMValue
if e.top-num+index < 0 {
e.Error = errors.New("E3:无效的表达式")
return
} else {
val = stack[e.top-num+index]
}
outStr += val.ToString()
}
e.top -= num
stack[e.top].TypeId = VMTypeString
stack[e.top].Value = outStr
e.top++
case typeLoadName, typeLoadNameRaw, typeLoadNameWithDetail:
name := code.Value.(string)
var val *VMValue
withDetail := typeLoadNameWithDetail == code.T
if withDetail {
detail := &details[len(details)-1]
detail.Tag = "load"
detail.Text = ""
val = ctx.LoadNameWithDetail(name, true, true, detail)
} else {
val = ctx.LoadName(name, true, true)
}
if ctx.Error != nil {
return
}
// 计算真实结果
isRaw := typeLoadNameRaw == code.T
doCompute := func(val *VMValue) *VMValue {
if !isRaw && val.TypeId == VMTypeComputedValue {
if withDetail {
detail := &details[len(details)-1]
val = val.ComputedExecute(ctx, detail)
} else {
val = val.ComputedExecute(ctx, &BufferSpan{})
}
if ctx.Error != nil {
return nil
}
}
// 追加计算结果到detail
if withDetail {
detail := &details[len(details)-1]
detail.Ret = val
}
return val
}
if ctx.Config.HookFuncValueLoadOverwrite != nil {
if len(details) > 0 {
oldRet := details[len(details)-1].Ret
val = ctx.Config.HookFuncValueLoadOverwrite(ctx, name, val, doCompute, &details[len(details)-1])
if oldRet == details[len(details)-1].Ret {
// 如果ret发生变化才修改,顺便修改detail中的结果为最终结果
details[len(details)-1].Ret = val
}
} else {
val = ctx.Config.HookFuncValueLoadOverwrite(ctx, name, val, doCompute, &BufferSpan{})
}
} else {
val = doCompute(val)
}
if ctx.Error != nil {
return
}
stackPush(val)
case typeStoreName:
v := e.stack[e.top-1].Clone()
name := code.Value.(string)
ctx.StoreName(name, v, true)
if ctx.Error != nil {
return
}
case typeJe, typeJeDup:
v := stackPop()
if v.AsBool() {
opIndex += int(code.Value.(IntType))
if code.T == typeJeDup {
stackPush(v)
}
}
case typeJne:
t := stackPop()
if !t.AsBool() {
opIndex += int(code.Value.(IntType))
}
case typeJmp:
opIndex += int(code.Value.(IntType))
case typePop:
stackPop()
case typePopN:
stackPopN(code.Value.(IntType))
case typeAdd, typeSubtract, typeMultiply, typeDivide, typeModulus, typeExponentiation, typeNullCoalescing,
typeCompLT, typeCompLE, typeCompEQ, typeCompNE, typeCompGE, typeCompGT,
typeBitwiseAnd, typeBitwiseOr:
// 所有二元运算符
v1, v2 := stackPop2()
opFunc := binOperator[code.T-typeAdd]
ret := opFunc(v1, ctx, v2)
if ctx.Error == nil && ret == nil {
// TODO: 整理所有错误类型
opErr := fmt.Sprintf("这两种类型无法使用 %s 算符连接: %s, %s", code.CodeString(), v1.GetTypeName(), v2.GetTypeName())
ctx.Error = errors.New(opErr)
}
if ctx.Error != nil {
return
}
stackPush(ret)
case typePositive, typeNegation:
v := stackPop()
var ret *VMValue
if code.T == typePositive {
ret = v.OpPositive()
} else {
ret = v.OpNegation()
}
if ret == nil {
// TODO: 整理所有错误类型
opErr := fmt.Sprintf("此类型无法使用一元算符 %s: %s", code.CodeString(), v.GetTypeName())
ctx.Error = errors.New(opErr)
}
if ctx.Error != nil {
return
}
stackPush(ret)
case typeDiceInit:
diceInit()
case typeDiceSetTimes:
v := stackPop()
times, ok := v.ReadInt()
if !ok || times <= 0 {
ctx.Error = errors.New("骰点次数不为正整数")
return
}
diceStates[diceStateIndex].times = times
case typeDiceSetKeepLowNum:
v := stackPop()
diceStates[diceStateIndex].isKeepLH = 1
diceStates[diceStateIndex].lowNum, _ = v.ReadInt()
case typeDiceSetKeepHighNum:
v := stackPop()
diceStates[diceStateIndex].isKeepLH = 2
diceStates[diceStateIndex].highNum, _ = v.ReadInt()
case typeDiceSetDropLowNum:
v := stackPop()
diceStates[diceStateIndex].isKeepLH = 3
diceStates[diceStateIndex].lowNum, _ = v.ReadInt()
case typeDiceSetDropHighNum:
v := stackPop()
diceStates[diceStateIndex].isKeepLH = 4
diceStates[diceStateIndex].highNum, _ = v.ReadInt()
case typeDiceSetMin:
v := stackPop()
i, _ := v.ReadInt()
diceStates[diceStateIndex].min = &i
case typeDiceSetMax:
v := stackPop()
i, _ := v.ReadInt()
diceStates[diceStateIndex].max = &i
case typeDetailMark:
span := code.Value.(BufferSpan)
details = append(details, span)
case typeDice:
diceState := diceStates[diceStateIndex]
val := stackPop()
bInt, ok := val.ReadInt()
if !ok || bInt <= 0 {
ctx.Error = errors.New("骰子面数不为正整数")
return
}
if ok && (diceState.isKeepLH == 1 || diceState.isKeepLH == 3) && diceState.lowNum <= 0 {
ctx.Error = errors.New("骰子取低个数不为正整数")
return
}
if ok && (diceState.isKeepLH == 2 || diceState.isKeepLH == 4) && diceState.highNum <= 0 {
ctx.Error = errors.New("骰子取高个数不为正整数")
return
}
numOpCountAdd(diceState.times)
if ctx.Error != nil {
return
}
num, detail := RollCommon(ctx.RandSrc, diceState.times, bInt, diceState.min, diceState.max, diceState.isKeepLH, diceState.lowNum, diceState.highNum, getRollMode())
diceStateIndex -= 1
ret := NewIntVal(num)
details[len(details)-1].Ret = ret
details[len(details)-1].Text = detail
details[len(details)-1].Tag = "dice"
stackPush(ret)
case typeDiceFate:
sum, detail := RollFate(ctx.RandSrc, getRollMode())
ret := NewIntVal(sum)
details[len(details)-1].Ret = ret
details[len(details)-1].Text = detail
details[len(details)-1].Tag = "dice-fate"
stackPush(ret)
case typeDiceCocBonus, typeDiceCocPenalty:
t := stackPop()
diceNum := t.MustReadInt()
if numOpCountAdd(diceNum) {
return
}
isBonus := code.T == typeDiceCocBonus
r, detailText := RollCoC(ctx.RandSrc, isBonus, diceNum, getRollMode())
ret := NewIntVal(r)
details[len(details)-1].Ret = ret
details[len(details)-1].Text = detailText
if isBonus {
details[len(details)-1].Tag = "dice-coc-bonus"
} else {
details[len(details)-1].Tag = "dice-coc-penalty"
}
stackPush(ret)
case typeWodSetInit:
// WOD 系列
wodInit()
case typeWodSetPoints:
v := stackPop()
// if v.TypeId != VMTypeInt {
// // ...
// }
wodState.points = v.MustReadInt()
case typeWodSetThreshold:
v := stackPop()
wodState.threshold = v.MustReadInt()
wodState.isGE = true
case typeWodSetThresholdQ:
v := stackPop()
wodState.threshold = v.MustReadInt()
wodState.isGE = false
case typeWodSetPool:
v := stackPop()
wodState.pool = v.MustReadInt()
case typeDiceWod:
v := stackPop() // 加骰线
// 变量检查
if !wodCheck(ctx, v.MustReadInt(), wodState.pool, wodState.points, wodState.threshold) {
return
}
num, _, _, detailText := RollWoD(ctx.RandSrc, v.MustReadInt(), wodState.pool, wodState.points, wodState.threshold, wodState.isGE, getRollMode())
ret := NewIntVal(num)
details[len(details)-1].Ret = ret
details[len(details)-1].Text = detailText
details[len(details)-1].Tag = "dice-wod"
stackPush(ret)
case typeDCSetInit:
// Double Cross
dcInit()
case typeDCSetPool:
v := stackPop()
dcState.pool = v.MustReadInt()
case typeDCSetPoints:
v := stackPop()
dcState.points = v.MustReadInt()
case typeDiceDC:
v := stackPop() // 暴击值 / 也可以理解为加骰线
if !doubleCrossCheck(ctx, v.MustReadInt(), dcState.pool, dcState.points) {
return
}
success, _, _, detailText := RollDoubleCross(nil, v.MustReadInt(), dcState.pool, dcState.points, getRollMode())
ret := NewIntVal(success)
details[len(details)-1].Ret = ret
details[len(details)-1].Text = detailText
details[len(details)-1].Tag = "dice-dc"
stackPush(ret)
case typeBlockPush:
if blockIndex > 20 {
ctx.Error = errors.New("语句块嵌套层数过多")
return
}
blockStack[blockIndex] = e.top
blockIndex += 1
case typeBlockPop: