Skip to content

Commit

Permalink
fix: 修正+-符号结合问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Jun 14, 2024
1 parent 4caa31e commit 63407cb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
4 changes: 2 additions & 2 deletions roll.peg
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ exprExp <- exprUnaryNeg (
)*

// 正数 负数
exprUnaryNeg <- minus exprRoot { c.data.AddOp(typeNegation) }
exprUnaryNeg <- minus exprDice { c.data.AddOp(typeNegation) }
/ exprUnaryPos

exprUnaryPos <- add exprRoot { c.data.AddOp(typePositive) }
exprUnaryPos <- add exprDice { c.data.AddOp(typePositive) }
/ exprDice

// 骰子算符
Expand Down
4 changes: 2 additions & 2 deletions roll.peg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 30 additions & 10 deletions rollvm_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dicescript

import (
"fmt"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -460,22 +459,26 @@ func TestUnary(t *testing.T) {
}

vm = NewVM()
err = vm.Run("--1")
err = vm.Run("- 1")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ni(1)))
assert.True(t, valueEqual(vm.Ret, ni(-1)))
}

vm = NewVM()
err = vm.Run("--1")
assert.Error(t, err)

vm = NewVM()
err = vm.Run("++1")
assert.Error(t, err)

vm = NewVM()
err = vm.Run("-+1")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ni(-1)))
}
assert.Error(t, err)

vm = NewVM()
err = vm.Run("+-1")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ni(-1)))
}
assert.Error(t, err)

vm = NewVM()
err = vm.Run("-1.3")
Expand Down Expand Up @@ -1579,7 +1582,6 @@ func TestIfError(t *testing.T) {
vm := NewVM()
var err error
err = vm.Run("if 1 ")
fmt.Println(err)
assert.Contains(t, err.Error(), "不符合if语法")
}

Expand All @@ -1600,3 +1602,21 @@ func TestFStringV1IfCompatible(t *testing.T) {
assert.Equal(t, vm.V1IfCompatibleCount, 1)
}
}

func TestNeg(t *testing.T) {
vm := NewVM()
var err error
err = vm.Run("-1 + 5")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ni(4)))
}
}

func TestNeg2(t *testing.T) {
vm := NewVM()
var err error
err = vm.Run("-1-5")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ni(-6)))
}
}
10 changes: 5 additions & 5 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type Context struct {
parser *parser
subThreadDepth int
attrs *ValueMap
upCtx *Context
UpCtx *Context
// subThread *Context // 用于执行子句

code []ByteCode
Expand Down Expand Up @@ -300,10 +300,10 @@ func (ctx *Context) LoadName(name string, isRaw bool, useHook bool) *VMValue {
if ret.TypeId != VMTypeNull {
return ret
}
if curCtx.upCtx == nil {
if curCtx.UpCtx == nil {
break
} else {
curCtx = curCtx.upCtx
curCtx = curCtx.UpCtx
}
}

Expand Down Expand Up @@ -1393,7 +1393,7 @@ func (v *VMValue) ComputedExecute(ctx *Context) *VMValue {
vm.GlobalValueLoadFunc = ctx.GlobalValueLoadFunc
vm.GlobalValueLoadOverwriteFunc = ctx.GlobalValueLoadOverwriteFunc
vm.subThreadDepth = ctx.subThreadDepth + 1
vm.upCtx = ctx
vm.UpCtx = ctx
vm.NumOpCount = ctx.NumOpCount + 100
ctx.NumOpCount = vm.NumOpCount // 防止无限递归
vm.randSrc = ctx.randSrc
Expand Down Expand Up @@ -1454,7 +1454,7 @@ func (v *VMValue) FuncInvoke(ctx *Context, params []*VMValue) *VMValue {
vm.GlobalValueLoadFunc = ctx.GlobalValueLoadFunc
vm.GlobalValueLoadOverwriteFunc = ctx.GlobalValueLoadOverwriteFunc
vm.subThreadDepth = ctx.subThreadDepth + 1
vm.upCtx = ctx
vm.UpCtx = ctx
vm.NumOpCount = ctx.NumOpCount + 100 // 递归视为消耗 + 100
ctx.NumOpCount = vm.NumOpCount // 防止无限递归
vm.randSrc = ctx.randSrc
Expand Down

0 comments on commit 63407cb

Please sign in to comment.