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

Add slice syntax #54

Merged
merged 2 commits into from
Dec 4, 2024
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
14 changes: 14 additions & 0 deletions pkg/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
TypeAssignmentExpr

TypeCallExpr
TypeSliceExpr

// stmt.
TypeBlockStmt
Expand Down Expand Up @@ -91,6 +92,8 @@ func (t NodeType) String() string {
return "AssignmentExpr"
case TypeCallExpr:
return "CallExpr"
case TypeSliceExpr:
return "SliceExpr"
case TypeBlockStmt:
return "BlockStmt"
case TypeIfelseStmt:
Expand Down Expand Up @@ -203,6 +206,9 @@ func (n *Node) AssignmentExpr() *AssignmentExpr {
func (n *Node) CallExpr() *CallExpr {
return n.elem.(*CallExpr)
}
func (n *Node) SliceExpr() *SliceExpr {
return n.elem.(*SliceExpr)
}
func (n *Node) BlockStmt() *BlockStmt {
return n.elem.(*BlockStmt)
}
Expand Down Expand Up @@ -345,6 +351,12 @@ func WrapCallExpr(node *CallExpr) *Node {
}
}

func WrapSliceExpr(node *SliceExpr) *Node {
return &Node{
NodeType: TypeSliceExpr,
elem: node,
}
}
func WrapIfelseStmt(node *IfelseStmt) *Node {
return &Node{
NodeType: TypeIfelseStmt,
Expand Down Expand Up @@ -433,6 +445,8 @@ func NodeStartPos(node *Node) token.LnColPos {
case TypeCallExpr:
return node.CallExpr().NamePos

case TypeSliceExpr:
return node.SliceExpr().LBracket
case TypeBlockStmt:
return node.BlockStmt().LBracePos

Expand Down
32 changes: 32 additions & 0 deletions pkg/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,38 @@ func (e *CallExpr) String() string {
return fmt.Sprintf("%s(%s)", strings.ToLower(e.Name), strings.Join(arr, ", "))
}

type SliceExpr struct {
Obj *Node
Start *Node
End *Node
Step *Node
Colon2 *Node
LBracket token.LnColPos
RBracket token.LnColPos
}

func (e *SliceExpr) IsExpr() bool {
return true
}

func (e *SliceExpr) String() string {
startStr := ""
if e.Start != nil {
startStr = e.Start.String()
}
endStr := ""
if e.End != nil {
endStr = e.End.String()
}
stepStr := ""
if e.Step != nil {
stepStr = ":" + e.Step.String()
} else if e.Colon2 != nil {
stepStr = ":"
}
return fmt.Sprintf("%s[%s:%s%s]", e.Obj.String(), startStr, endStr, stepStr)
}

type AssignmentExpr struct {
LHS, RHS *Node
Op Op
Expand Down
26 changes: 26 additions & 0 deletions pkg/engine/runtime/checkstmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func RunStmtCheck(ctx *Task, ctxCheck *ContextCheck, node *ast.Node) *errchain.P

case ast.TypeCallExpr:
return RunCallExprCheck(ctx, ctxCheck, node.CallExpr())
case ast.TypeSliceExpr:
return RunSliceExprCheck(ctx, ctxCheck, node.SliceExpr())

case ast.TypeIfelseStmt:
return RunIfElseStmtCheck(ctx, ctxCheck, node.IfelseStmt())
Expand Down Expand Up @@ -191,6 +193,30 @@ func RunCallExprCheck(ctx *Task, ctxCheck *ContextCheck, expr *ast.CallExpr) *er
return funcCheck(ctx, expr)
}

func RunSliceExprCheck(ctx *Task, ctxCheck *ContextCheck, expr *ast.SliceExpr) *errchain.PlError {
if err := RunStmtCheck(ctx, ctxCheck, expr.Obj); err != nil {
return err
}

if expr.Start != nil {
if err := RunStmtCheck(ctx, ctxCheck, expr.Start); err != nil {
return err
}
}

if expr.End != nil {
if err := RunStmtCheck(ctx, ctxCheck, expr.End); err != nil {
return err
}
}
if expr.End != nil {
if err := RunStmtCheck(ctx, ctxCheck, expr.Step); err != nil {
return err
}
}
return nil
}

func RunAssignmentExprCheck(ctx *Task, ctxCheck *ContextCheck, expr *ast.AssignmentExpr) *errchain.PlError {
if err := RunStmtCheck(ctx, ctxCheck, expr.RHS); err != nil {
return err
Expand Down
128 changes: 128 additions & 0 deletions pkg/engine/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ func RunStmt(ctx *Task, node *ast.Node) (any, ast.DType, *errchain.PlError) {
return RunAssignmentExpr(ctx, node.AssignmentExpr())
case ast.TypeCallExpr:
return RunCallExpr(ctx, node.CallExpr())
case ast.TypeSliceExpr:
return RunSliceExpr(ctx, node.SliceExpr())
case ast.TypeInExpr:
return RunInExpr(ctx, node.InExpr())
case ast.TypeListLiteral:
Expand Down Expand Up @@ -1006,7 +1008,133 @@ func RunCallExpr(ctx *Task, expr *ast.CallExpr) (any, ast.DType, *errchain.PlErr
}
return nil, ast.Void, nil
}
func RunSliceExpr(ctx *Task, expr *ast.SliceExpr) (any, ast.DType, *errchain.PlError) {
obj, objT, err := RunStmt(ctx, expr.Obj)
if err != nil {
return nil, ast.Invalid, err
}
var start, end, step any
var startT, endT, stepT ast.DType
if expr.Start != nil {
start, startT, err = RunStmt(ctx, expr.Start)
if err != nil {
return nil, ast.Invalid, err
}
}
if expr.End != nil {
end, endT, err = RunStmt(ctx, expr.End)
if err != nil {
return nil, ast.Invalid, err
}
}
if expr.Step != nil {
step, stepT, err = RunStmt(ctx, expr.Step)
if err != nil {
return nil, ast.Invalid, err
}
}
var startInt, endInt, stepInt int
var length int
switch objT { //nolint:exhaustive
case ast.String:
length = len(obj.(string))
case ast.List, ast.DType(ast.TypeSliceExpr):
length = len(obj.([]any))
default:
return nil, ast.Invalid, NewRunError(ctx, "invalid obj type", expr.Obj.StartPos())
}

if step != nil {
if stepT != ast.Int {
return nil, ast.Invalid, NewRunError(ctx, "invalid step type", expr.Step.StartPos())
}
stepInt = cast.ToInt(step)
if stepInt == 0 {
return nil, ast.Invalid, NewRunError(ctx, "step must be non-zero", expr.Step.StartPos())
}
} else {
stepInt = 1
}

if start != nil {
if startT != ast.Int {
return nil, ast.Invalid, NewRunError(ctx, "invalid start type", expr.Start.StartPos())
}
startInt = cast.ToInt(start)
if startInt < 0 {
startInt = length + startInt
}
} else if stepInt > 0 {
startInt = 0
} else {
startInt = length - 1
}

if end != nil {
if endT != ast.Int {
return nil, ast.Invalid, NewRunError(ctx, "invalid end type", expr.End.StartPos())
}
endInt = cast.ToInt(end)
if endInt < 0 {
endInt = length + endInt
}
} else if stepInt > 0 {
endInt = length
} else {
endInt = -1
}

switch objT {
case ast.String:
str := obj.(string)
if stepInt > 0 {
result := ""
if startInt < 0 {
startInt = 0
}
for i := startInt; i < endInt && i < length; i += stepInt {
result += string(str[i])
}
return result, ast.String, nil
} else {
result := ""
if startInt > length-1 {
startInt = length - 1
}
for i := startInt; i > endInt && i >= 0; i += stepInt {
result += string(str[i])
}
return result, ast.String, nil
}
default:
list := obj.([]any)
if stepInt > 0 {
if startInt < 0 {
startInt = 0
}
if endInt > length {
endInt = length
}
result := make([]any, 0, (endInt-startInt+stepInt-1)/stepInt)
for i := startInt; i < endInt; i += stepInt {
result = append(result, list[i])
}
return result, ast.List, nil
} else {
if startInt > length-1 {
startInt = length - 1
}
if endInt < 0 {
endInt = -1
}
result := make([]any, 0, (startInt-endInt-stepInt-1)/(-stepInt))
for i := startInt; i > endInt; i += stepInt {
result = append(result, list[i])
}
return result, ast.List, nil
}
}
}
func typePromotion(l ast.DType, r ast.DType) ast.DType {
if l == ast.Float || r == ast.Float {
return ast.Float
Expand Down
Loading
Loading