2024-07-01 13:05:15 +00:00
|
|
|
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.
|
2024-07-30 10:48:48 +00:00
|
|
|
func Parse(tokens []token.Token, source []byte) (AST, error) {
|
2024-07-30 14:36:33 +00:00
|
|
|
nodes := make(AST, 0, len(tokens)/64)
|
2024-07-01 13:05:15 +00:00
|
|
|
|
|
|
|
err := EachInstruction(tokens, func(instruction token.List) error {
|
2024-07-30 14:36:33 +00:00
|
|
|
node, err := parseNode(instruction, source, nodes)
|
2024-07-01 13:05:15 +00:00
|
|
|
|
|
|
|
if err == nil && node != nil {
|
2024-07-30 14:36:33 +00:00
|
|
|
nodes = append(nodes, node)
|
2024-07-01 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2024-07-30 14:36:33 +00:00
|
|
|
return nodes, err
|
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
|
|
|
}
|