Improved AST parser
This commit is contained in:
42
src/build/ast/parseNode.go
Normal file
42
src/build/ast/parseNode.go
Normal file
@ -0,0 +1,42 @@
|
||||
package ast
|
||||
|
||||
import (
|
||||
"git.akyoto.dev/cli/q/src/build/errors"
|
||||
"git.akyoto.dev/cli/q/src/build/expression"
|
||||
"git.akyoto.dev/cli/q/src/build/token"
|
||||
)
|
||||
|
||||
// parseNode generates an AST node from an instruction.
|
||||
func parseNode(tokens token.List, source []byte) (Node, error) {
|
||||
if tokens[0].IsKeyword() {
|
||||
return parseKeyword(tokens, source)
|
||||
}
|
||||
|
||||
expr := expression.Parse(tokens)
|
||||
|
||||
if expr == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
switch {
|
||||
case IsVariableDefinition(expr):
|
||||
if len(expr.Children) < 2 {
|
||||
return nil, errors.New(errors.MissingOperand, nil, expr.Token.End())
|
||||
}
|
||||
|
||||
return &Define{Expression: expr}, nil
|
||||
|
||||
case IsAssignment(expr):
|
||||
if len(expr.Children) < 2 {
|
||||
return nil, errors.New(errors.MissingOperand, nil, expr.Token.End())
|
||||
}
|
||||
|
||||
return &Assign{Expression: expr}, nil
|
||||
|
||||
case IsFunctionCall(expr):
|
||||
return &Call{Expression: expr}, nil
|
||||
|
||||
default:
|
||||
return nil, errors.New(&errors.InvalidInstruction{Instruction: expr.Token.Text(source)}, nil, expr.Token.Position)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user