From 536ad49f05d6668b1fbcc1165cd50e83e264f645 Mon Sep 17 00:00:00 2001 From: fy Date: Wed, 5 Jun 2024 19:39:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=90=91=E5=A4=96=E6=9A=B4?= =?UTF-8?q?=E9=9C=B2=20BufferSpan=20=E7=9A=84=E6=88=90=E5=91=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bytecode.go | 2 +- parser.go | 18 ++++++++-------- rollvm.go | 61 ++++++++++++++++++++++++++--------------------------- types.go | 16 +++++++------- 4 files changed, 48 insertions(+), 49 deletions(-) diff --git a/bytecode.go b/bytecode.go index 0d689fea..105ce5b1 100644 --- a/bytecode.go +++ b/bytecode.go @@ -248,7 +248,7 @@ func (code *ByteCode) CodeString() string { return "halt" case typeDetailMark: v := code.Value.(BufferSpan) - return fmt.Sprintf("mark.detail %d, %d", v.begin, v.end) + return fmt.Sprintf("mark.detail %d, %d", v.Begin, v.End) case typeJmp: return fmt.Sprintf("jmp %d", code.Value) case typeJe: diff --git a/parser.go b/parser.go index 9d623ed7..6e96a6ea 100644 --- a/parser.go +++ b/parser.go @@ -28,10 +28,10 @@ type ParserData struct { } type BufferSpan struct { - begin IntType - end IntType - ret *VMValue - text string + Begin IntType + End IntType + Ret *VMValue + Text string } func (pd *ParserData) init() { @@ -68,7 +68,7 @@ func (e *ParserData) checkStackOverflow() bool { copy(newCode, e.code) e.code = newCode } else { - //e.Error = errors.New("E1:指令虚拟机栈溢出,请不要发送过长的指令") + // e.Error = errors.New("E1:指令虚拟机栈溢出,请不要发送过长的指令") return true } } @@ -87,7 +87,7 @@ func (e *ParserData) WriteCode(T CodeType, value any) { } func (p *ParserData) AddDiceDetail(begin IntType, end IntType) { - p.WriteCode(typeDetailMark, BufferSpan{begin: begin, end: end}) + p.WriteCode(typeDetailMark, BufferSpan{Begin: begin, End: end}) } func (e *ParserData) AddOp(operator CodeType) { @@ -132,7 +132,7 @@ func (e *ParserData) PushGlobal() { } func (e *ParserData) AddFormatString(num IntType) { - //e.PushStr(value) + // e.PushStr(value) e.WriteCode(typeLoadFormatString, num) // num } @@ -233,7 +233,7 @@ func (e *ParserData) OffsetPopAndSet() { 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) + // fmt.Println("XXXX", e.Code[codeIndex], "|", e.Top, codeIndex) } func (e *ParserData) OffsetPopN(num int) { @@ -289,7 +289,7 @@ func (e *ParserData) AddInvokeMethod(name string, paramsNum IntType) { } func (e *ParserData) AddInvoke(paramsNum IntType) { - //e.WriteCode(typePushIntNumber, paramsNum) + // e.WriteCode(typePushIntNumber, paramsNum) e.WriteCode(typeInvoke, paramsNum) } diff --git a/rollvm.go b/rollvm.go index e16f63e9..8fb2a974 100644 --- a/rollvm.go +++ b/rollvm.go @@ -122,13 +122,13 @@ 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 } +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 } +func (a spanByEnd) Less(i, j int) bool { return a[i].End < a[j].End } // getE5 := func() error { // return errors.New("E5: 超出单指令允许算力,不予计算") @@ -136,8 +136,9 @@ func (a spanByEnd) Less(i, j int) bool { return a[i].end < a[j].end } func (ctx *Context) makeDetailStr(details []BufferSpan) string { if ctx.Config.CustomMakeDetailFunc != nil { - return ctx.Config.CustomMakeDetailFunc(ctx, details) + return ctx.Config.CustomMakeDetailFunc(ctx, details, ctx.parser.data) } + detailResult := ctx.parser.data curPoint := IntType(-1) // nolint lastEnd := IntType(-1) // nolint @@ -150,27 +151,25 @@ func (ctx *Context) makeDetailStr(details []BufferSpan) string { for _, i := range details { // fmt.Println("?", i, lastEnd) - if i.begin > lastEnd { - curPoint = i.begin + if i.Begin > lastEnd { + curPoint = i.Begin m = append(m, struct { begin IntType end IntType spans []BufferSpan - }{begin: curPoint, end: i.end, spans: []BufferSpan{i}}) + }{begin: curPoint, end: i.End, spans: []BufferSpan{i}}) } 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 > m[len(m)-1].end { + m[len(m)-1].end = i.End } } - if i.end > lastEnd { - lastEnd = i.end + if i.End > lastEnd { + lastEnd = i.End } } - detailResult := ctx.parser.data - for i := len(m) - 1; i >= 0; i-- { // for i := 0; i < len(m); i++ { item := m[i] @@ -184,7 +183,7 @@ func (ctx *Context) makeDetailStr(details []BufferSpan) string { // 例如 (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() + subDetailsText += "," + string(detailResult[span.Begin:span.End]) + "=" + span.Ret.ToString() } } @@ -194,13 +193,13 @@ func (ctx *Context) makeDetailStr(details []BufferSpan) string { r = append(r, detailResult[:item.begin]...) // 主体结果部分,如 (10d3)d5=63[(10d3)d5=63=2+2+2+5+2+5+5+4+1+3+4+1+4+5+4+3+4+5+2,10d3=19] - detail := "[" + exprText + "=" + last.ret.ToString() - if last.text != "" { - detail += "=" + last.text + detail := "[" + exprText + "=" + last.Ret.ToString() + if last.Text != "" { + detail += "=" + last.Text } detail += subDetailsText + "]" - r = append(r, ([]byte)(last.ret.ToString()+detail)...) + r = append(r, ([]byte)(last.Ret.ToString()+detail)...) r = append(r, detailResult[item.end:]...) detailResult = r } @@ -597,8 +596,8 @@ func (ctx *Context) evaluate() { if val != nil { // 使用弄进来的替代值进行计算 if typeLoadNameWithDetail == code.T { - details[len(details)-1].ret = val - details[len(details)-1].text = "" + details[len(details)-1].Ret = val + details[len(details)-1].Text = "" } stackPush(val) continue @@ -610,8 +609,8 @@ func (ctx *Context) evaluate() { return } if typeLoadNameWithDetail == code.T { - details[len(details)-1].ret = val - details[len(details)-1].text = "" + details[len(details)-1].Ret = val + details[len(details)-1].Text = "" } stackPush(val) @@ -743,15 +742,15 @@ func (ctx *Context) evaluate() { diceStateIndex -= 1 ret := NewIntVal(num) - details[len(details)-1].ret = ret - details[len(details)-1].text = detail + details[len(details)-1].Ret = ret + details[len(details)-1].Text = detail stackPush(ret) case typeDiceFate: sum, detail := RollFate(ctx.randSrc) ret := NewIntVal(sum) - details[len(details)-1].ret = ret - details[len(details)-1].text = detail + details[len(details)-1].Ret = ret + details[len(details)-1].Text = detail stackPush(ret) case typeDiceCocBonus, typeDiceCocPenalty: @@ -764,8 +763,8 @@ func (ctx *Context) evaluate() { r, detailText := RollCoC(ctx.randSrc, code.T == typeDiceCocBonus, diceNum) ret := NewIntVal(r) - details[len(details)-1].ret = ret - details[len(details)-1].text = detailText + details[len(details)-1].Ret = ret + details[len(details)-1].Text = detailText stackPush(ret) case typeWodSetInit: @@ -798,8 +797,8 @@ func (ctx *Context) evaluate() { num, _, _, detailText := RollWoD(ctx.randSrc, v.MustReadInt(), wodState.pool, wodState.points, wodState.threshold, wodState.isGE) ret := NewIntVal(num) - details[len(details)-1].ret = ret - details[len(details)-1].text = detailText + details[len(details)-1].Ret = ret + details[len(details)-1].Text = detailText stackPush(ret) case typeDCSetInit: @@ -818,8 +817,8 @@ func (ctx *Context) evaluate() { } success, _, _, detailText := RollDoubleCross(nil, v.MustReadInt(), dcState.pool, dcState.points) ret := NewIntVal(success) - details[len(details)-1].ret = ret - details[len(details)-1].text = detailText + details[len(details)-1].Ret = ret + details[len(details)-1].Text = detailText stackPush(ret) case typeStSetName: diff --git a/types.go b/types.go index 0812ab52..39f7c786 100644 --- a/types.go +++ b/types.go @@ -81,7 +81,7 @@ type RollConfig struct { CallbackLoadVar func(name string) (string, *VMValue) // 加载变量回调,返回值会成为新变量名 CallbackSt func(_type string, name string, val *VMValue, extra *VMValue, op string, detail string) // st回调 - CustomMakeDetailFunc func(ctx *Context, details []BufferSpan) string // 自定义计算过程 + CustomMakeDetailFunc func(ctx *Context, details []BufferSpan, dataBuffer []byte) string // 自定义计算过程 OpCountLimit IntType // 算力限制,超过这个值会报错,0为无限,建议值30000 DefaultDiceSideExpr string // 默认骰子面数 @@ -289,18 +289,18 @@ func (ctx *Context) LoadName(name string, isRaw bool) *VMValue { return ctx.LoadNameGlobal(name, isRaw) // if ctx.GlobalValueLoadFunc != nil { - // ret := ctx.GlobalValueLoadFunc(name) + // Ret := ctx.GlobalValueLoadFunc(name) // if ctx.Error != nil { // return nil // } - // if ret != nil { - // if !isRaw && ret.TypeId == VMTypeComputedValue { - // ret = ret.ComputedExecute(ctx) + // if Ret != nil { + // if !isRaw && Ret.TypeId == VMTypeComputedValue { + // Ret = Ret.ComputedExecute(ctx) // if ctx.Error != nil { // return nil // } // } - // return ret + // return Ret // } // } // return VMValueNewUndefined() @@ -1026,8 +1026,8 @@ func (v *VMValue) AttrGet(ctx *Context, name string) *VMValue { } } - // if ret == nil { - // ret = VMValueNewUndefined() + // if Ret == nil { + // Ret = VMValueNewUndefined() // } } // TODO: 思考一下 Dict.keys 和 Dict.values 与 ArrtGet 的冲突