Implemented assert keyword

This commit is contained in:
2024-07-25 16:47:25 +02:00
parent f0bc5039ee
commit f4dd9004be
14 changed files with 81 additions and 1 deletions

10
src/build/ast/Assert.go Normal file
View File

@ -0,0 +1,10 @@
package ast
import (
"git.akyoto.dev/cli/q/src/build/expression"
)
// Assert represents a condition that must be true, otherwise the program stops.
type Assert struct {
Condition *expression.Expression
}

View File

@ -35,6 +35,15 @@ func toASTNode(tokens token.List, buffer []byte) (Node, error) {
return &Return{Value: value}, nil
}
if tokens[0].Kind == token.Assert {
if len(tokens) == 1 {
return nil, errors.New(errors.MissingExpression, nil, tokens[0].End())
}
condition := expression.Parse(tokens[1:])
return &Assert{Condition: condition}, nil
}
if keywordHasBlock(tokens[0].Kind) {
blockStart := tokens.IndexKind(token.BlockStart)
blockEnd := tokens.LastIndexKind(token.BlockEnd)