Skip to content

Commit

Permalink
refactor: 向外暴露 BufferSpan 的成员
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Jun 5, 2024
1 parent a2ce5d2 commit 8b33725
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion bytecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down
56 changes: 28 additions & 28 deletions rollvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: 超出单指令允许算力,不予计算")
Expand All @@ -150,22 +150,22 @@ 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
}
}

Expand All @@ -184,7 +184,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()
}
}

Expand All @@ -194,13 +194,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
}
Expand Down Expand Up @@ -597,8 +597,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
Expand All @@ -610,8 +610,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)

Expand Down Expand Up @@ -743,15 +743,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:
Expand All @@ -764,8 +764,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:
Expand Down Expand Up @@ -798,8 +798,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:
Expand All @@ -818,8 +818,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:
Expand Down
14 changes: 7 additions & 7 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 的冲突
Expand Down

0 comments on commit 8b33725

Please sign in to comment.