Implemented expression parsing
This commit is contained in:
65
src/build/expression/Operator.go
Normal file
65
src/build/expression/Operator.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user