Skip to content

Commit

Permalink
imp: 改进st兼容问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Jun 23, 2024
1 parent fad9003 commit 0e5e379
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 更新记录

#### 2024.6.23
* 重写计算过程机制
* 改进st兼容问题

#### 2024.6.18
* round ceil floor 允许输入int类型
* 加入新函数load用于动态载入变量
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ DiceScript将更好的实现骰点功能,语法规范化的同时,具有更

## TODO

* d2d(4d4d5)d6 计算过程问题
* ~~d2d(4d4d5)d6 计算过程问题~~

## 开发

如果修改了文法:
如果修改了文法,使用这个工具重新生成:
```
go install github.com/fy0/pigeon@latest
pigeon -nolint -optimize-parser -optimize-ref-expr-by-index -o .\roll.peg.go .\roll.peg
Expand Down
6 changes: 3 additions & 3 deletions roll.peg
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,13 @@ st_modify_multi_rest <- (st_modify_lead sp ','? sp)*

st_modify_rest1 <- sp (
"+=" sp text:< exprRoot > {c.data.AddStModify("+", text.(string))} /
"-=" sp text:< exprRoot > {c.data.AddStModify("-", text.(string))}
"-=" sp text:< exprRoot > {c.data.AddStModify("-=", text.(string))}
)

st_modify_rest <- sp (
'+' '='? sp text:< exprRoot > {c.data.AddStModify("+", text.(string))} /
'-' '='? sp text:< exprRoot > {c.data.AddStModify("-", text.(string))}
/// &( '-' ) sp text:< exprRoot > {c.data.AddStModify("-", text.(string))} /
"-=" sp text:< exprRoot > {c.data.AddStModify("-=", text.(string))} /
&( '-' ) sp text:< exprRoot > {c.data.AddStModify("-", text.(string))} // 这一个式子是为了兼容st a-1-1视为a-2
)

st_name1 <- text:<( id_ch+ ":" id_ch+ )> { c.data.PushStr(text.(string)) } // 结尾不带数字
Expand Down
29 changes: 25 additions & 4 deletions roll.peg.go

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

4 changes: 4 additions & 0 deletions rollvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,10 @@ func (ctx *Context) evaluate() {

if e.Config.CallbackSt != nil {
name, _ := stName.ReadString()
if stInfo.Op == "-" {
// 负号取正,以免-和-=出现符号一正一反的情况
stVal = stVal.OpNegation()
}
e.Config.CallbackSt("mod", name, stVal.Clone(), nil, stInfo.Op, stInfo.Text)
}
case typeStX0:
Expand Down
41 changes: 40 additions & 1 deletion rollvm_st_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func (item *checkItem) check(t *testing.T, _type string, name string, val *VMVal
if item.Extra != nil {
assert.True(t, valueEqual(extra, item.Extra))
}
if item.Op != "" {
assert.Equal(t, op, item.Op)
}
assert.Equal(t, _type, item.Type)
}

Expand Down Expand Up @@ -175,7 +178,7 @@ func TestStMod1(t *testing.T) {
checkItem{Name: "力量", Value: ni(1), Type: "mod"},
checkItem{Name: "力量", Value: ni(4), Type: "mod"},
checkItem{Name: "力量", Value: ni(6), Type: "mod"},
checkItem{Name: "力量", Value: ni(6), Type: "mod", Op: "-"},
checkItem{Name: "力量", Value: ni(2), Type: "mod", Op: "-"},
}

index := 0
Expand Down Expand Up @@ -206,3 +209,39 @@ func TestStMod2(t *testing.T) {
err := vm.Run(`^st'力量123'+=3`)
assert.NoError(t, err)
}

func TestStModMinus(t *testing.T) {
vm := NewVM()

items := []checkItem{
checkItem{Name: "力量", Value: ni(4), Type: "mod", Op: "-"},
}

index := 0
vm.Config.CallbackSt = func(_type string, name string, val *VMValue, extra *VMValue, op string, detail string) {
// fmt.Println("!!", _type, name, val, extra, op, detail)
items[index].check(t, _type, name, val, extra, op, detail)
index += 1
}

err := vm.Run(`^st力量-3d1-1 `)
assert.NoError(t, err)
}

func TestStModMinus2(t *testing.T) {
vm := NewVM()

items := []checkItem{
checkItem{Name: "力量", Value: ni(2), Type: "mod", Op: "-="},
}

index := 0
vm.Config.CallbackSt = func(_type string, name string, val *VMValue, extra *VMValue, op string, detail string) {
// fmt.Println("!!", _type, name, val, extra, op, detail)
items[index].check(t, _type, name, val, extra, op, detail)
index += 1
}

err := vm.Run(`^st力量-=3d1-1 `)
assert.NoError(t, err)
}

0 comments on commit 0e5e379

Please sign in to comment.