Skip to content

Commit

Permalink
imp: round ceil floor 允许输入int类型
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Jun 17, 2024
1 parent 5e2a6f9 commit 5daed6d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
25 changes: 17 additions & 8 deletions builtin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,40 @@ import (
)

func funcCeil(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
if params[0].TypeId == VMTypeInt {
return params[0]
}
v, ok := params[0].ReadFloat()
if ok {
return NewIntVal(IntType(math.Ceil(v)))
} else {
ctx.Error = errors.New("类型错误: 只能是float")
ctx.Error = errors.New("(ceil)类型错误: 只能是数字类型")
}
return nil
}

func funcRound(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
if params[0].TypeId == VMTypeInt {
return params[0]
}
v, ok := params[0].ReadFloat()
if ok {
return NewIntVal(IntType(math.Round(v)))
} else {
ctx.Error = errors.New("类型错误: 只能是float")
ctx.Error = errors.New("(round)类型错误: 只能是数字类型")
}
return nil
}

func funcFloor(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
if params[0].TypeId == VMTypeInt {
return params[0]
}
v, ok := params[0].ReadFloat()
if ok {
return NewIntVal(IntType(math.Floor(v)))
} else {
ctx.Error = errors.New("类型错误: 只能是float")
ctx.Error = errors.New("(floor)类型错误: 只能是数字类型")
}
return nil
}
Expand All @@ -53,7 +62,7 @@ func funcAbs(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
return v
}

ctx.Error = errors.New("类型错误: 参数必须为int或float")
ctx.Error = errors.New("(abs)类型错误: 参数必须为int或float")
return nil
}

Expand All @@ -78,10 +87,10 @@ func funcInt(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
if err == nil {
return NewIntVal(IntType(val))
} else {
ctx.Error = errors.New("值错误: 无法进行 int() 转换: " + s)
ctx.Error = errors.New("(int)值错误: 无法进行 int() 转换: " + s)
}
default:
ctx.Error = errors.New("类型错误: 只能是数字类型")
ctx.Error = errors.New("(int)类型错误: 只能是数字类型")
}
return nil
}
Expand All @@ -99,10 +108,10 @@ func funcFloat(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
if err == nil {
return NewFloatVal(val)
} else {
ctx.Error = errors.New("值错误: 无法进行 float() 转换: " + s)
ctx.Error = errors.New("(float)值错误: 无法进行 float() 转换: " + s)
}
default:
ctx.Error = errors.New("类型错误: 只能是数字类型")
ctx.Error = errors.New("(float)类型错误: 只能是数字类型")
}
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions builtin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ func TestNativeFunctionFloat(t *testing.T) {
assert.True(t, valueEqual(funcCeil(vm, nil, []*VMValue{nf(1.0)}), ni(1)))

assert.True(t, valueEqual(funcRound(vm, nil, []*VMValue{nf(1.6)}), ni(2)))
funcRound(vm, nil, []*VMValue{ni(1)})
assert.True(t, valueEqual(funcRound(vm, nil, []*VMValue{ni(2)}), ni(2)))
funcRound(vm, nil, []*VMValue{ns("1.6")})
assert.Error(t, vm.Error)
vm.Error = nil

assert.True(t, valueEqual(funcFloor(vm, nil, []*VMValue{nf(1.6)}), ni(1)))
funcFloor(vm, nil, []*VMValue{ni(1)})
assert.True(t, valueEqual(funcFloor(vm, nil, []*VMValue{ni(1)}), ni(1)))
funcFloor(vm, nil, []*VMValue{ns("1.6")})
assert.Error(t, vm.Error)
vm.Error = nil
}
Expand Down
4 changes: 4 additions & 0 deletions rollvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,11 @@ func (ctx *Context) evaluate() {
details[len(details)-1].Text = ""
}
if ctx.Config.HookFuncValueLoadOverwrite != nil {
oldRet := details[len(details)-1].Ret
val = ctx.Config.HookFuncValueLoadOverwrite(name, val, &details[len(details)-1])
if oldRet == details[len(details)-1].Ret {
details[len(details)-1].Ret = val
}
}
stackPush(val)

Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type RollConfig struct {
// 读取后回调(返回值将覆盖之前读到的值。如果之前未读取到值curVal将为nil)
HookFuncValueLoadOverwrite func(name string, curVal *VMValue, detail *BufferSpan) *VMValue

// st回调,注意val和extra都经过clone,可以放心储存
CallbackSt func(_type string, name string, val *VMValue, extra *VMValue, op string, detail string) // st回调
CustomMakeDetailFunc func(ctx *Context, details []BufferSpan, dataBuffer []byte) string // 自定义计算过程

Expand Down

0 comments on commit 5daed6d

Please sign in to comment.