Implemented if statements

This commit is contained in:
2024-07-07 12:30:57 +02:00
parent 6fc234c700
commit 91e300e49a
16 changed files with 280 additions and 14 deletions

View File

@ -37,6 +37,15 @@ func (node *Define) String() string {
return fmt.Sprintf("(= %s %s)", node.Name.Text(), node.Value)
}
type If struct {
Condition *expression.Expression
Body AST
}
func (node *If) String() string {
return fmt.Sprintf("(if %s %s)", node.Condition, node.Body)
}
type Loop struct {
Body AST
}

View File

@ -47,6 +47,22 @@ func toASTNode(tokens token.List) (Node, error) {
tree, err := Parse(tokens[blockStart:blockEnd])
return &Loop{Body: tree}, err
case keyword.If:
blockStart := tokens.IndexKind(token.BlockStart) + 1
blockEnd := tokens.LastIndexKind(token.BlockEnd)
if blockStart == -1 {
return nil, errors.New(errors.MissingBlockStart, nil, tokens[0].End())
}
if blockEnd == -1 {
return nil, errors.New(errors.MissingBlockEnd, nil, tokens[len(tokens)-1].End())
}
condition := expression.Parse(tokens[1:token.BlockStart])
tree, err := Parse(tokens[blockStart:blockEnd])
return &If{Condition: condition, Body: tree}, err
default:
return nil, errors.New(&errors.KeywordNotImplemented{Keyword: tokens[0].Text()}, nil, tokens[0].Position)
}