Implemented expression parsing

This commit is contained in:
2024-06-16 16:57:33 +02:00
parent 864c9c7b43
commit ef16bdb4c7
18 changed files with 618 additions and 99 deletions

View File

@ -0,0 +1,65 @@
package expression
import "git.akyoto.dev/cli/q/src/build/token"
// Operator represents an operator for mathematical expressions.
type Operator struct {
Symbol string
Precedence int
Operands int
}
// Operators defines the operators used in the language.
// The number corresponds to the operator priority and can not be zero.
var Operators = map[string]*Operator{
".": {".", 12, 2},
"*": {"*", 11, 2},
"/": {"/", 11, 2},
"%": {"%", 11, 2},
"+": {"+", 10, 2},
"-": {"-", 10, 2},
">>": {">>", 9, 2},
"<<": {"<<", 9, 2},
">": {">", 8, 2},
"<": {"<", 8, 2},
">=": {">=", 8, 2},
"<=": {"<=", 8, 2},
"==": {"==", 7, 2},
"!=": {"!=", 7, 2},
"&": {"&", 6, 2},
"^": {"^", 5, 2},
"|": {"|", 4, 2},
"&&": {"&&", 3, 2},
"||": {"||", 2, 2},
"=": {"=", 1, 2},
"+=": {"+=", 1, 2},
"-=": {"-=", 1, 2},
"*=": {"*=", 1, 2},
"/=": {"/=", 1, 2},
">>=": {">>=", 1, 2},
"<<=": {"<<=", 1, 2},
}
func isComplete(expr *Expression) bool {
if expr == nil {
return false
}
if expr.Token.Kind == token.Identifier {
return true
}
if expr.Token.Kind == token.Operator && len(expr.Children) == numOperands(expr.Token.Text()) {
return true
}
return false
}
func numOperands(symbol string) int {
return Operators[symbol].Operands
}
func precedence(symbol string) int {
return Operators[symbol].Precedence
}