Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解析时in 缺失 && 切片语法优化 #57

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/engine/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ func RunSliceExpr(ctx *Task, expr *ast.SliceExpr) (any, ast.DType, *errchain.PlE

if step != nil {
if stepT != ast.Int {
return nil, ast.Invalid, NewRunError(ctx, "invalid step type", expr.Step.StartPos())
return nil, ast.Invalid, NewRunError(ctx, "step type must be integer", expr.Step.StartPos())
}
stepInt = cast.ToInt(step)
if stepInt == 0 {
Expand All @@ -1058,7 +1058,7 @@ func RunSliceExpr(ctx *Task, expr *ast.SliceExpr) (any, ast.DType, *errchain.PlE

if start != nil {
if startT != ast.Int {
return nil, ast.Invalid, NewRunError(ctx, "invalid start type", expr.Start.StartPos())
return nil, ast.Invalid, NewRunError(ctx, "start type must be integer", expr.Start.StartPos())
}
startInt = cast.ToInt(start)
if startInt < 0 {
Expand All @@ -1072,7 +1072,7 @@ func RunSliceExpr(ctx *Task, expr *ast.SliceExpr) (any, ast.DType, *errchain.PlE

if end != nil {
if endT != ast.Int {
return nil, ast.Invalid, NewRunError(ctx, "invalid end type", expr.End.StartPos())
return nil, ast.Invalid, NewRunError(ctx, "end type must be integer", expr.End.StartPos())
}
endInt = cast.ToInt(end)
if endInt < 0 {
Expand Down
14 changes: 14 additions & 0 deletions pkg/engine/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ func TestInExpr(t *testing.T) {
x = 1
y = [1]
add_key("t4", x in y)

add_key("t5", x in function(tags=false))
`
stmts, err := parseScript(pl)
if err != nil {
Expand All @@ -308,6 +310,11 @@ func TestInExpr(t *testing.T) {
"test": callexprtest,
"add_key": addkeytest,
"len": lentest,
"function": func(ctx *Task, callExpr *ast.CallExpr) *errchain.PlError {
// 模拟函数返回 [1, 2, 3, 4]
ctx.Regs.ReturnAppend([]any{int64(1), int64(2), int64(3), int64(4)}, ast.List)
return nil
},
},
Name: "abc",
Namespace: "default",
Expand All @@ -319,6 +326,12 @@ func TestInExpr(t *testing.T) {
errR := script.Check(map[string]FuncCheck{
"add_key": addkeycheck,
"len": lencheck,
"function": func(ctx *Task, callExpr *ast.CallExpr) *errchain.PlError {
if len(callExpr.Param) != 1 {
return NewRunError(ctx, "function expects 2 argument", callExpr.NamePos)
}
return nil
},
})
if errR != nil {
t.Fatal(*errR)
Expand All @@ -344,6 +357,7 @@ func TestInExpr(t *testing.T) {
"t3_2": false,

"t4": true,
"t5": true,
}, inData.data)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/parser/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ slice_expr_start
{
$$ = $1
}
| call_expr
{
$$ = $1
}
;

slice_expr: identifier LEFT_BRACKET SPACE_EOLS expr COLON SPACE_EOLS expr COLON SPACE_EOLS expr RIGHT_BRACKET //a[1:3:2]
Expand Down
Loading
Loading