39 lines
990 B
Go
39 lines
990 B
Go
package ast
|
|
|
|
import (
|
|
"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.
|
|
func Parse(tokens []token.Token, source []byte) (AST, error) {
|
|
tree := make(AST, 0, len(tokens)/64)
|
|
|
|
err := EachInstruction(tokens, func(instruction token.List) error {
|
|
node, err := parseNode(instruction, source)
|
|
|
|
if err == nil && node != nil {
|
|
tree = append(tree, node)
|
|
}
|
|
|
|
return err
|
|
})
|
|
|
|
return tree, err
|
|
}
|
|
|
|
// IsAssignment returns true if the expression is an assignment.
|
|
func IsAssignment(expr *expression.Expression) bool {
|
|
return expr.Token.IsAssignment()
|
|
}
|
|
|
|
// IsFunctionCall returns true if the expression is a function call.
|
|
func IsFunctionCall(expr *expression.Expression) bool {
|
|
return expr.Token.Kind == token.Call
|
|
}
|
|
|
|
// IsVariableDefinition returns true if the expression is a variable definition.
|
|
func IsVariableDefinition(expr *expression.Expression) bool {
|
|
return expr.Token.Kind == token.Define
|
|
}
|