Skip to content

Commit

Permalink
imp: 改进字符串模板、读写钩子接口、计算过程
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Jun 11, 2024
1 parent 655f62c commit 942ef93
Show file tree
Hide file tree
Showing 8 changed files with 503 additions and 509 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ DiceScript将更好的实现骰点功能,语法规范化的同时,具有更

## 更新记录

#### 2024.6.11
* 字符串模板中的 {% %} 不再输出内容
* 字符串模板 {} 报错优化
* 计算过程信息进行算符标注
* 优化值读写钩子接口

#### 2024.6.8
* 赋值语句调整为表达式,这意味着能够在字符串模板中使用
* 赋值语句取值后不再从栈中弹出值
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {
vm.Config.DefaultDiceSideExpr = "面数 ?? 50"
vm.Config.OpCountLimit = 30000

vm.Config.CallbackLoadVar = func(name string) (string, *ds.VMValue) {
vm.Config.HookFuncValueLoad = func(name string) (string, *ds.VMValue) {
re := regexp.MustCompile(`^(困难|极难|大成功|常规|失败|困難|極難|常規|失敗)?([^\d]+)(\d+)?$`)
m := re.FindStringSubmatch(name)
var cocFlagVarPrefix string
Expand Down
2 changes: 2 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type BufferSpan struct {
End IntType
Ret *VMValue
Text string
// 来源标记
Tag string
}

func (pd *ParserData) init() {
Expand Down
17 changes: 11 additions & 6 deletions roll.peg
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ nextLine <- ((spNoCR '\n' / sp ';') sp)+ stmtLines?

stmtBreak <- "break" sp {
if c.data.loopLayer == 0 {
panic("`break` is not allowed outside loop.")
p.addErr(errors.New("`break` is not allowed outside loop."))
return false
} else {
c.data.BreakPush()
}
}

stmtContinue <- "continue" sp {
if c.data.loopLayer == 0 {
panic("`continue` is not allowed outside loop.")
p.addErr(errors.New("`continue` is not allowed outside loop."))
return false
} else {
c.data.ContinuePush()
}
Expand Down Expand Up @@ -334,14 +336,17 @@ strPart1 <- text:< (escape / (![{\x1e\\].))+ > { c.data.PushStr(text.(string));
strPart2 <- text:< (escape / (![{"\\\n\r].))+ > { c.data.PushStr(text.(string)); c.data.CounterAdd(1) }
strPart3 <- text:< (escape / (![{'\\\n\r].))+ > { c.data.PushStr(text.(string)); c.data.CounterAdd(1) }

fstringE1 <- ('}' / &{ p.addErr(errors.New("无法处理字符 " + string(p.pt.rn))); return false })
fstringExpr1 <- exprRoot {c.data.CounterAdd(1)} / &{ p.addErr(errors.New("{} 内必须是一个表达式")); return false; }
fstringExpr <- '{' sp fstringExpr1 sp ('}' / &{ p.addErr(errors.New("无法处理字符 " + string(p.pt.rn))); return false })
fstringStmt <- "{%" sp stmtRoot { c.data.WriteCode(typePop, nil) } sp "%}"

fstring <- (
('\'' '\'' { c.data.PushStr("") })
/ ('\x1e' '\x1e' { c.data.PushStr("") })
/ ('"' '"' { c.data.PushStr("") })
/ ('`' '`' { c.data.PushStr("") })
/ ('`' { c.data.CounterPush() } ( ('{' sp exprRoot {c.data.CounterAdd(1)} sp fstringE1) / ("{%" sp stmtRoot {c.data.CounterAdd(1)} sp "%}") / strPart )* '`' { c.data.AddFormatString(c.data.CounterPop()) })
/ ('\x1e' { c.data.CounterPush() } ( ('{' sp exprRoot {c.data.CounterAdd(1)} sp fstringE1) / ("{%" sp stmtRoot {c.data.CounterAdd(1)} sp "%}") / strPart1 )* '\x1e' { c.data.AddFormatString(c.data.CounterPop()) }) // 特殊标记 0x1E
/ ('`' { c.data.CounterPush() } ( fstringStmt / fstringExpr / strPart )* '`' { c.data.AddFormatString(c.data.CounterPop()) })
/ ('\x1e' { c.data.CounterPush() } ( fstringStmt / fstringExpr / strPart1 )* '\x1e' { c.data.AddFormatString(c.data.CounterPop()) }) // 特殊标记 0x1E
/ ('"' { c.data.CounterPush() } strPart2* '"' { c.data.CounterPop() })
/ ('\'' { c.data.CounterPush() } strPart3* '\'' { c.data.CounterPop() } )
) sp
Expand All @@ -351,7 +356,7 @@ escape <- '\\' ([btnfr"'\\])


keywords <- "while" / "if" / "else" / "continue" / "break" / "return" / "func"
keywords_test <- !(keywords !xidContinue &{ p.addErr(errors.New("使用关键字作为变量名")); return true})
keywords_test "keywords" <- !(keywords !xidContinue &{ p.addErr(errors.New("使用关键字作为变量名")); return true})

identifier <- keywords_test xidStart (xidContinue / ':')* {
return toStr(c.text);
Expand Down
Loading

0 comments on commit 942ef93

Please sign in to comment.