2024-07-01 13:05:15 +00:00
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
2024-07-03 09:39:24 +00:00
|
|
|
"git.akyoto.dev/cli/q/src/build/errors"
|
2024-07-01 13:05:15 +00:00
|
|
|
"git.akyoto.dev/cli/q/src/build/expression"
|
|
|
|
"git.akyoto.dev/cli/q/src/build/token"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parse generates an AST from a list of tokens.
|
2024-07-21 12:35:06 +00:00
|
|
|
func Parse(tokens []token.Token, buffer []byte) (AST, error) {
|
2024-07-01 13:05:15 +00:00
|
|
|
tree := make(AST, 0, len(tokens)/64)
|
|
|
|
|
|
|
|
err := EachInstruction(tokens, func(instruction token.List) error {
|
2024-07-21 12:35:06 +00:00
|
|
|
node, err := toASTNode(instruction, buffer)
|
2024-07-01 13:05:15 +00:00
|
|
|
|
|
|
|
if err == nil && node != nil {
|
|
|
|
tree = append(tree, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return tree, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// toASTNode generates an AST node from an instruction.
|
2024-07-21 12:35:06 +00:00
|
|
|
func toASTNode(tokens token.List, buffer []byte) (Node, error) {
|
|
|
|
if tokens[0].IsKeyword() {
|
|
|
|
if tokens[0].Kind == token.Return {
|
2024-07-16 18:22:28 +00:00
|
|
|
if len(tokens) == 1 {
|
|
|
|
return &Return{}, nil
|
|
|
|
}
|
|
|
|
|
2024-07-01 13:05:15 +00:00
|
|
|
value := expression.Parse(tokens[1:])
|
|
|
|
return &Return{Value: value}, nil
|
2024-07-16 10:01:38 +00:00
|
|
|
}
|
2024-07-01 13:05:15 +00:00
|
|
|
|
2024-07-25 14:47:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-21 12:35:06 +00:00
|
|
|
if keywordHasBlock(tokens[0].Kind) {
|
2024-07-07 12:07:34 +00:00
|
|
|
blockStart := tokens.IndexKind(token.BlockStart)
|
2024-07-01 13:05:15 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-07-21 12:35:06 +00:00
|
|
|
body, err := Parse(tokens[blockStart+1:blockEnd], buffer)
|
2024-07-07 10:30:57 +00:00
|
|
|
|
2024-07-21 12:35:06 +00:00
|
|
|
switch tokens[0].Kind {
|
|
|
|
case token.If:
|
2024-07-16 10:01:38 +00:00
|
|
|
condition := expression.Parse(tokens[1:blockStart])
|
|
|
|
return &If{Condition: condition, Body: body}, err
|
2024-07-07 10:30:57 +00:00
|
|
|
|
2024-07-21 12:35:06 +00:00
|
|
|
case token.Loop:
|
2024-07-16 10:01:38 +00:00
|
|
|
return &Loop{Body: body}, err
|
2024-07-07 10:30:57 +00:00
|
|
|
}
|
2024-07-01 13:05:15 +00:00
|
|
|
}
|
2024-07-16 10:01:38 +00:00
|
|
|
|
2024-07-21 12:35:06 +00:00
|
|
|
return nil, errors.New(&errors.KeywordNotImplemented{Keyword: tokens[0].Text(buffer)}, nil, tokens[0].Position)
|
2024-07-01 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr := expression.Parse(tokens)
|
|
|
|
|
|
|
|
if expr == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case IsVariableDefinition(expr):
|
|
|
|
if len(expr.Children) < 2 {
|
2024-07-05 15:11:30 +00:00
|
|
|
return nil, errors.New(errors.MissingOperand, nil, expr.Token.End())
|
2024-07-01 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-28 16:12:42 +00:00
|
|
|
return &Define{Expression: expr}, nil
|
2024-07-01 13:05:15 +00:00
|
|
|
|
|
|
|
case IsAssignment(expr):
|
|
|
|
if len(expr.Children) < 2 {
|
2024-07-05 15:11:30 +00:00
|
|
|
return nil, errors.New(errors.MissingOperand, nil, expr.Token.End())
|
2024-07-01 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 15:35:26 +00:00
|
|
|
return &Assign{Expression: expr}, nil
|
2024-07-01 13:05:15 +00:00
|
|
|
|
|
|
|
case IsFunctionCall(expr):
|
|
|
|
return &Call{Expression: expr}, nil
|
|
|
|
|
|
|
|
default:
|
2024-07-21 12:35:06 +00:00
|
|
|
return nil, errors.New(&errors.InvalidInstruction{Instruction: expr.Token.Text(buffer)}, nil, expr.Token.Position)
|
2024-07-01 13:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAssignment returns true if the expression is an assignment.
|
|
|
|
func IsAssignment(expr *expression.Expression) bool {
|
2024-07-21 12:35:06 +00:00
|
|
|
return expr.Token.IsAssignment()
|
2024-07-01 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsFunctionCall returns true if the expression is a function call.
|
|
|
|
func IsFunctionCall(expr *expression.Expression) bool {
|
2024-07-21 12:35:06 +00:00
|
|
|
return expr.Token.Kind == token.Call
|
2024-07-01 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsVariableDefinition returns true if the expression is a variable definition.
|
|
|
|
func IsVariableDefinition(expr *expression.Expression) bool {
|
2024-07-21 12:35:06 +00:00
|
|
|
return expr.Token.Kind == token.Define
|
2024-07-01 13:05:15 +00:00
|
|
|
}
|
2024-07-16 10:01:38 +00:00
|
|
|
|
|
|
|
// keywordHasBlock returns true if the keyword requires a block.
|
2024-07-21 12:35:06 +00:00
|
|
|
func keywordHasBlock(kind token.Kind) bool {
|
|
|
|
return kind == token.If || kind == token.Loop
|
2024-07-16 10:01:38 +00:00
|
|
|
}
|