Implemented for loops

This commit is contained in:
Eduard Urbach 2025-02-19 23:46:17 +01:00
parent ac03657307
commit d26bf9d1a9
Signed by: eduard
GPG key ID: 49226B848C78F6C8
16 changed files with 142 additions and 120 deletions

9
src/ast/For.go Normal file
View file

@ -0,0 +1,9 @@
package ast
import "git.akyoto.dev/cli/q/src/expression"
// For is a loop with a defined iteration limit.
type For struct {
Head *expression.Expression
Body AST
}

View file

@ -1,9 +1,6 @@
package ast
import "git.akyoto.dev/cli/q/src/expression"
// Loop represents a block of repeatable statements.
// Loop is a block of infinitely repeating instructions.
type Loop struct {
Head *expression.Expression
Body AST
}

View file

@ -39,17 +39,21 @@ func parseKeyword(tokens token.List, source []byte, nodes AST) (Node, error) {
ifNode.Else = body
return nil, err
case token.Loop:
case token.For:
blockStart, _, body, err := block(tokens, source)
head := tokens[1:blockStart]
loop := &Loop{
loop := &For{
Head: expression.Parse(head),
Body: body,
}
return loop, err
case token.Loop:
_, _, body, err := block(tokens, source)
return &Loop{Body: body}, err
case token.Return:
if len(tokens) == 1 {
return &Return{}, nil