Skip to content

Commit

Permalink
refactor: 切换数字类型为IntType(可配置),同时修复一个值复制问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Apr 24, 2024
1 parent 053ea09 commit 864b2ec
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 222 deletions.
10 changes: 5 additions & 5 deletions builtin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func funcCeil(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
v, ok := params[0].ReadFloat()
if ok {
return VMValueNewInt(int64(math.Ceil(v)))
return VMValueNewInt(IntType(math.Ceil(v)))
} else {
ctx.Error = errors.New("类型错误: 只能是float")
}
Expand All @@ -19,7 +19,7 @@ func funcCeil(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
func funcRound(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
v, ok := params[0].ReadFloat()
if ok {
return VMValueNewInt(int64(math.Round(v)))
return VMValueNewInt(IntType(math.Round(v)))
} else {
ctx.Error = errors.New("类型错误: 只能是float")
}
Expand All @@ -29,7 +29,7 @@ func funcRound(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
func funcFloor(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
v, ok := params[0].ReadFloat()
if ok {
return VMValueNewInt(int64(math.Floor(v)))
return VMValueNewInt(IntType(math.Floor(v)))
} else {
ctx.Error = errors.New("类型错误: 只能是float")
}
Expand Down Expand Up @@ -63,12 +63,12 @@ func funcInt(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
return params[0]
case VMTypeFloat:
v, _ := params[0].ReadFloat()
return VMValueNewInt(int64(v))
return VMValueNewInt(IntType(v))
case VMTypeString:
s, _ := params[0].ReadString()
val, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return VMValueNewInt(val)
return VMValueNewInt(IntType(val))
} else {
ctx.Error = errors.New("值错误: 无法进行 int() 转换: " + s)
}
Expand Down
8 changes: 4 additions & 4 deletions bytecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@ const (
func (code *ByteCode) CodeString() string {
switch code.T {
case TypePushIntNumber:
return "push.int " + strconv.FormatInt(code.Value.(int64), 10)
return "push.int " + strconv.FormatInt(int64(code.Value.(IntType)), 10)
case TypePushFloatNumber:
return "push.flt " + strconv.FormatFloat(code.Value.(float64), 'f', 2, 64)
case TypePushString:
return "push.str " + code.Value.(string)
case TypePushRange:
return "push.range"
case TypePushArray:
return "push.arr " + strconv.FormatInt(code.Value.(int64), 10)
return "push.arr " + strconv.FormatInt(int64(code.Value.(IntType)), 10)
case TypePushDict:
return "push.dict " + strconv.FormatInt(code.Value.(int64), 10)
return "push.dict " + strconv.FormatInt(int64(code.Value.(IntType)), 10)
case TypePushComputed:
computed, _ := code.Value.(*VMValue).ReadComputed()
return "push.computed " + computed.Expr
Expand All @@ -141,7 +141,7 @@ func (code *ByteCode) CodeString() string {
return "push.func " + computed.Name

case TypeInvoke:
return "invoke " + strconv.FormatInt(code.Value.(int64), 10)
return "invoke " + strconv.FormatInt(int64(code.Value.(IntType)), 10)

case TypeInvokeSelf:
return "invoke.self " + code.Value.(string)
Expand Down
23 changes: 12 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"fmt"
"github.com/peterh/liner"
dice "github.com/sealdice/dicescript"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/peterh/liner"
ds "github.com/sealdice/dicescript"
)

var (
Expand All @@ -29,25 +30,25 @@ func main() {
_ = f.Close()
}

attrs := map[string]*dice.VMValue{}
attrs := map[string]*ds.VMValue{}

fmt.Println("DiceScript Shell v0.0.1")
ccTimes := 0
vm := dice.NewVM()
vm := ds.NewVM()
vm.Config.EnableDiceWoD = true
vm.Config.EnableDiceCoC = true
vm.Config.EnableDiceFate = true
vm.Config.EnableDiceDoubleCross = true
vm.Config.PrintBytecode = true
vm.Config.CallbackSt = func(_type string, name string, val *dice.VMValue, extra *dice.VMValue, op string, detail string) {
vm.Config.CallbackSt = func(_type string, name string, val *ds.VMValue, extra *ds.VMValue, op string, detail string) {
fmt.Println("st:", _type, name, val.ToString(), extra.ToString(), op, detail)
}

vm.Config.IgnoreDiv0 = true
vm.Config.DefaultDiceSideExpr = "面数 ?? 50"
vm.Config.OpCountLimit = 30000

vm.Config.CallbackLoadVar = func(name string) (string, *dice.VMValue) {
vm.Config.CallbackLoadVar = func(name string) (string, *ds.VMValue) {
re := regexp.MustCompile(`^(困难|极难|大成功|常规|失败|困難|極難|常規|失敗)?([^\d]+)(\d+)?$`)
m := re.FindStringSubmatch(name)
var cocFlagVarPrefix string
Expand All @@ -62,16 +63,16 @@ func main() {
if m[3] != "" {
v, _ := strconv.ParseInt(m[3], 10, 64)
fmt.Println("COC值:", name, cocFlagVarPrefix)
return name, dice.VMValueNewInt(v)
return name, ds.VMValueNewInt(ds.IntType(v))
}
}

fmt.Println("COC值:", name, cocFlagVarPrefix)
return name, nil
}

_ = vm.RegCustomDice(`E(\d+)`, func(ctx *dice.Context, groups []string) *dice.VMValue {
return dice.VMValueNewInt(2)
_ = vm.RegCustomDice(`E(\d+)`, func(ctx *ds.Context, groups []string) *ds.VMValue {
return ds.VMValueNewInt(2)
})

//vm.ValueStoreNameFunc = func(name string, v *dice.VMValue) {
Expand All @@ -80,12 +81,12 @@ func main() {

re := regexp.MustCompile(`^(\D+)(\d+)$`)

vm.GlobalValueLoadFunc = func(name string) *dice.VMValue {
vm.GlobalValueLoadFunc = func(name string) *ds.VMValue {
m := re.FindStringSubmatch(name)
if len(m) > 1 {
//val, _ := strconv.ParseInt(m[2], 10, 64)
//return dice.VMValueNewInt(val)
return dice.VMValueNewInt(0)
return ds.VMValueNewInt(0)
}

if val, ok := attrs[name]; ok {
Expand Down
4 changes: 2 additions & 2 deletions jsport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newVM(name string) *js.Object {
m := re.FindStringSubmatch(name)
if len(m) > 1 {
val, _ := strconv.ParseInt(m[2], 10, 64)
return ds.VMValueNewInt(val)
return ds.VMValueNewInt(ds.IntType(val))
}

if v, exists := player.Load(name); exists {
Expand Down Expand Up @@ -59,7 +59,7 @@ func main() {
"newValueMap": func() *js.Object {
return js.MakeFullWrapper(&ds.ValueMap{})
},
"vmNewInt": func(i int64) *js.Object {
"vmNewInt": func(i ds.IntType) *js.Object {
return js.MakeFullWrapper(ds.VMValueNewInt(i))
},
"vmNewFloat": func(i float64) *js.Object {
Expand Down
60 changes: 30 additions & 30 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ import (
)

type ParserData struct {
counterStack []int64 // f-string 嵌套计数,在解析时中起作用
varnameStack []string // 另一个解析用栈
jmpStack []int64
breakStack []int64 // break,用时创建
continueStack []int64 // continue用,用时创建
counterStack []IntType // f-string 嵌套计数,在解析时中起作用
varnameStack []string // 另一个解析用栈
jmpStack []IntType
breakStack []IntType // break,用时创建
continueStack []IntType // continue用,用时创建
loopInfo []struct {
continueIndex int
breakIndex int
}
loopLayer int64 // 当前loop层数
loopLayer int // 当前loop层数
codeStack []struct {
code []ByteCode
index int
}
}

type BufferSpan struct {
begin int64
end int64
begin IntType
end IntType
ret *VMValue
text string
}

func (pd *ParserData) init() {
pd.counterStack = []int64{}
pd.counterStack = []IntType{}
pd.varnameStack = []string{}
pd.jmpStack = []int64{} // 不复用counterStack的原因是在 ?: 算符中两个都有用到
pd.jmpStack = []IntType{} // 不复用counterStack的原因是在 ?: 算符中两个都有用到
pd.codeStack = []struct {
code []ByteCode
index int
Expand Down Expand Up @@ -84,14 +84,14 @@ func (e *Parser) WriteCode(T CodeType, value interface{}) {
e.codeIndex += 1
}

func (p *Parser) AddDiceDetail(begin int64, end int64) {
func (p *Parser) AddDiceDetail(begin IntType, end IntType) {
p.WriteCode(TypeDetailMark, BufferSpan{begin: begin, end: end})
}

func (e *Parser) AddOp(operator CodeType) {
var val interface{} = nil
if operator == TypeJne || operator == TypeJmp {
val = int64(0)
val = IntType(0)
}
e.WriteCode(operator, val)
}
Expand All @@ -102,18 +102,18 @@ func (e *Parser) AddLoadName(value string) {

func (e *Parser) PushIntNumber(value string) {
val, _ := strconv.ParseInt(value, 10, 64)
e.WriteCode(TypePushIntNumber, int64(val))
e.WriteCode(TypePushIntNumber, IntType(val))
}

func (e *Parser) PushStr(value string) {
e.WriteCode(TypePushString, value)
}

func (e *Parser) PushArray(value int64) {
func (e *Parser) PushArray(value IntType) {
e.WriteCode(TypePushArray, value)
}

func (e *Parser) PushDict(value int64) {
func (e *Parser) PushDict(value IntType) {
e.WriteCode(TypePushDict, value)
}

Expand All @@ -129,7 +129,7 @@ func (e *Parser) PushGlobal() {
e.WriteCode(TypePushGlobal, nil)
}

func (e *Parser) AddFormatString(value string, num int64) {
func (e *Parser) AddFormatString(value string, num IntType) {
//e.PushStr(value)
e.WriteCode(TypeLoadFormatString, num) // num
}
Expand Down Expand Up @@ -176,16 +176,16 @@ func (e *Parser) NamePop() string {
}

func (e *Parser) OffsetPush() {
e.jmpStack = append(e.jmpStack, int64(e.codeIndex)-1)
e.jmpStack = append(e.jmpStack, IntType(e.codeIndex)-1)
}

func (p *Parser) ContinuePush() {
if p.loopLayer > 0 {
if p.continueStack == nil {
p.continueStack = []int64{}
p.continueStack = []IntType{}
}
p.AddOp(TypeJmp)
p.continueStack = append(p.continueStack, int64(p.codeIndex)-1)
p.continueStack = append(p.continueStack, IntType(p.codeIndex)-1)
} else {
p.Error = errors.New("循环外不能放置continue")
}
Expand All @@ -198,7 +198,7 @@ func (p *Parser) ContinueSet(offsetB int) {
lastB := len(p.jmpStack) - 1 - offsetB
jmpIndex := p.jmpStack[lastB]
// 试出来的,这个是对的,那么也许while那个是错的??还是说因为while最后多push了一个jmp呢?
p.code[codeIndex].Value = -(int64(codeIndex) - jmpIndex)
p.code[codeIndex].Value = -(IntType(codeIndex) - jmpIndex)
}
}
}
Expand All @@ -207,18 +207,18 @@ func (p *Parser) BreakSet() {
if p.breakStack != nil {
info := p.loopInfo[len(p.loopInfo)-1]
for _, codeIndex := range p.breakStack[info.breakIndex:] {
p.code[codeIndex].Value = int64(p.codeIndex) - codeIndex - 1
p.code[codeIndex].Value = IntType(p.codeIndex) - codeIndex - 1
}
}
}

func (p *Parser) BreakPush() {
if p.loopLayer > 0 {
if p.breakStack == nil {
p.breakStack = []int64{}
p.breakStack = []IntType{}
}
p.AddOp(TypeJmp)
p.breakStack = append(p.breakStack, int64(p.codeIndex)-1)
p.breakStack = append(p.breakStack, IntType(p.codeIndex)-1)
} else {
p.Error = errors.New("循环外不能放置break")
}
Expand All @@ -228,7 +228,7 @@ func (e *Parser) OffsetPopAndSet() {
last := len(e.jmpStack) - 1
codeIndex := e.jmpStack[last]
e.jmpStack = e.jmpStack[:last]
e.code[codeIndex].Value = int64(int64(e.codeIndex) - codeIndex - 1)
e.code[codeIndex].Value = IntType(IntType(e.codeIndex) - codeIndex - 1)
//fmt.Println("XXXX", e.Code[codeIndex], "|", e.Top, codeIndex)
}

Expand All @@ -245,24 +245,24 @@ func (e *Parser) OffsetJmpSetX(offsetA int, offsetB int, rev bool) {
jmpIndex := e.jmpStack[lastB]

if rev {
e.code[codeIndex].Value = -(int64(e.codeIndex) - jmpIndex - 1)
e.code[codeIndex].Value = -(IntType(e.codeIndex) - jmpIndex - 1)
} else {
e.code[codeIndex].Value = int64(e.codeIndex) - jmpIndex - 1
e.code[codeIndex].Value = IntType(e.codeIndex) - jmpIndex - 1
}
}

func (e *Parser) CounterPush() {
e.counterStack = append(e.counterStack, 0)
}

func (e *Parser) CounterAdd(offset int64) {
func (e *Parser) CounterAdd(offset IntType) {
last := len(e.counterStack) - 1
if last != -1 {
e.counterStack[last] += offset
}
}

func (e *Parser) CounterPop() int64 {
func (e *Parser) CounterPop() IntType {
last := len(e.counterStack) - 1
num := e.counterStack[last]
e.counterStack = e.counterStack[:last]
Expand All @@ -279,12 +279,12 @@ func (e *Parser) FlagsPop() {
e.flagsStack = e.flagsStack[:last]
}

func (e *Parser) AddInvokeMethod(name string, paramsNum int64) {
func (e *Parser) AddInvokeMethod(name string, paramsNum IntType) {
e.WriteCode(TypePushIntNumber, paramsNum)
e.WriteCode(TypeInvokeSelf, name)
}

func (e *Parser) AddInvoke(paramsNum int64) {
func (e *Parser) AddInvoke(paramsNum IntType) {
//e.WriteCode(TypePushIntNumber, paramsNum)
e.WriteCode(TypeInvoke, paramsNum)
}
Expand Down
Loading

0 comments on commit 864b2ec

Please sign in to comment.