Skip to content

Commit

Permalink
feat: 增加store函数; 向下兼容允许【】()四个字符作为变量后继字符
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Aug 16, 2024
1 parent e177356 commit 28fab8c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
16 changes: 16 additions & 0 deletions builtin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ func funcLoadRaw(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
return funcLoadBase(ctx, this, params, true)
}

func funcStore(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
name := params[0]
if name.TypeId != VMTypeString {
ctx.Error = errors.New("(store)类型错误: 参数1类型必须为str")
return nil
}

val := params[1]
ctx.StoreName(name.Value.(string), val, true)
return val
}

func funcDir(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
typeId := params[0].TypeId
var arr []*VMValue
Expand Down Expand Up @@ -217,6 +229,7 @@ var builtinValues = map[string]*VMValue{
"repr": nnf(&ndf{"repr", []string{"value"}, nil, nil, funcRepr}),
"load": nnf(&ndf{"load", []string{"value"}, nil, nil, nil}),
"loadRaw": nnf(&ndf{"loadRaw", []string{"value"}, nil, nil, nil}),
"store": nnf(&ndf{"store", []string{"name", "value"}, nil, nil, nil}),

// TODO: roll()

Expand All @@ -233,6 +246,9 @@ func _init() bool {

nfd, _ = builtinValues["loadRaw"].ReadNativeFunctionData()
nfd.NativeFunc = funcLoadRaw

nfd, _ = builtinValues["store"].ReadNativeFunctionData()
nfd.NativeFunc = funcStore
return false
}

Expand Down
8 changes: 8 additions & 0 deletions builtin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,11 @@ func TestNativeFunctionTypeId(t *testing.T) {
assert.True(t, valueEqual(vm.Ret, ni(IntType(VMTypeInt))))
}
}

func TestNativeFunctionStore(t *testing.T) {
vm := NewVM()
err := vm.Run("store('test', 123); test")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ni(123)))
}
}
2 changes: 1 addition & 1 deletion roll.peg
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ identifierWithoutColon <- keywords_test xidStart xidContinue* {
xidStart <- [_$\p{L}\p{Other_ID_Start}]

// xid_start + Nl + Mn + Mc + Nd + Pc + Other_ID_Continue \u200C\u200D
xidContinue <- [$\p{L}\p{Other_ID_Start}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Other_ID_Continue}]
xidContinue <- [$\p{L}\p{Other_ID_Start}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Other_ID_Continue}()【】]

// 括号
sub <- parenOpen exprRoot parenClose
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.

8 changes: 8 additions & 0 deletions rollvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,14 @@ func TestFStringV1IfCompatible(t *testing.T) {
}
}

func TestFStringV1IdentCompatible(t *testing.T) {
vm := NewVM()
err := vm.Run("$t(测试) = 1; $t(测试)")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ni(1)))
}
}

func TestFStringV1IfCompatibleWhile(t *testing.T) {
// while 不返回内容,所以如果是字符串模板的最后一项,无输出
vm := NewVM()
Expand Down

0 comments on commit 28fab8c

Please sign in to comment.