Implemented unary operator parsing
This commit is contained in:
@ -17,17 +17,22 @@ func TestParse(t *testing.T) {
|
||||
}{
|
||||
{"Identity", "1", "1"},
|
||||
{"Basic calculation", "1+2", "(+ 1 2)"},
|
||||
|
||||
{"Same operator", "1+2+3", "(+ (+ 1 2) 3)"},
|
||||
{"Same operator 2", "1+2+3+4", "(+ (+ (+ 1 2) 3) 4)"},
|
||||
|
||||
{"Different operator", "1+2-3", "(- (+ 1 2) 3)"},
|
||||
{"Different operator 2", "1+2-3+4", "(+ (- (+ 1 2) 3) 4)"},
|
||||
{"Different operator 3", "1+2-3+4-5", "(- (+ (- (+ 1 2) 3) 4) 5)"},
|
||||
|
||||
{"Grouped identity", "(1)", "1"},
|
||||
{"Grouped identity 2", "((1))", "1"},
|
||||
{"Grouped identity 3", "(((1)))", "1"},
|
||||
|
||||
{"Adding identity", "(1)+(2)", "(+ 1 2)"},
|
||||
{"Adding identity 2", "(1)+(2)+(3)", "(+ (+ 1 2) 3)"},
|
||||
{"Adding identity 3", "(1)+(2)+(3)+(4)", "(+ (+ (+ 1 2) 3) 4)"},
|
||||
|
||||
{"Grouping", "(1+2)", "(+ 1 2)"},
|
||||
{"Grouping 2", "(1+2+3)", "(+ (+ 1 2) 3)"},
|
||||
{"Grouping 3", "((1)+(2)+(3))", "(+ (+ 1 2) 3)"},
|
||||
@ -35,9 +40,11 @@ func TestParse(t *testing.T) {
|
||||
{"Grouping right", "1*(2+3)", "(* 1 (+ 2 3))"},
|
||||
{"Grouping same operator", "1+(2+3)", "(+ 1 (+ 2 3))"},
|
||||
{"Grouping same operator 2", "1+(2+3)+(4+5)", "(+ (+ 1 (+ 2 3)) (+ 4 5))"},
|
||||
|
||||
{"Two groups", "(1+2)*(3+4)", "(* (+ 1 2) (+ 3 4))"},
|
||||
{"Two groups 2", "(1+2-3)*(3+4-5)", "(* (- (+ 1 2) 3) (- (+ 3 4) 5))"},
|
||||
{"Two groups 3", "(1+2)*(3+4-5)", "(* (+ 1 2) (- (+ 3 4) 5))"},
|
||||
|
||||
{"Operator priority", "1+2*3", "(+ 1 (* 2 3))"},
|
||||
{"Operator priority 2", "1*2+3", "(+ (* 1 2) 3)"},
|
||||
{"Operator priority 3", "1+2*3+4", "(+ (+ 1 (* 2 3)) 4)"},
|
||||
@ -46,10 +53,25 @@ func TestParse(t *testing.T) {
|
||||
{"Operator priority 6", "1+2*3+4*5", "(+ (+ 1 (* 2 3)) (* 4 5))"},
|
||||
{"Operator priority 7", "1+2*3*4*5*6", "(+ 1 (* (* (* (* 2 3) 4) 5) 6))"},
|
||||
{"Operator priority 8", "1*2*3+4*5*6", "(+ (* (* 1 2) 3) (* (* 4 5) 6))"},
|
||||
|
||||
{"Complex", "(1+2-3*4)*(5+6-7*8)", "(* (- (+ 1 2) (* 3 4)) (- (+ 5 6) (* 7 8)))"},
|
||||
{"Complex 2", "(1+2*3-4)*(5+6*7-8)", "(* (- (+ 1 (* 2 3)) 4) (- (+ 5 (* 6 7)) 8))"},
|
||||
{"Complex 3", "(1+2*3-4)*(5+6*7-8)+9-10*11", "(- (+ (* (- (+ 1 (* 2 3)) 4) (- (+ 5 (* 6 7)) 8)) 9) (* 10 11))"},
|
||||
{"Unary", "!", "!"},
|
||||
|
||||
{"Unary not", "!", "!"},
|
||||
{"Unary not 2", "!a", "(! a)"},
|
||||
{"Unary not 3", "!(!a)", "(! (! a))"},
|
||||
{"Unary not 4", "!(a||b)", "(! (|| a b))"},
|
||||
{"Unary not 5", "a || !b", "(|| a (! b))"},
|
||||
|
||||
{"Unary minus", "-", "-"},
|
||||
{"Unary minus 2", "-a", "(- a)"},
|
||||
{"Unary minus 3", "-(-a)", "(- (- a))"},
|
||||
{"Unary minus 4", "-a+b", "(+ (- a) b)"},
|
||||
{"Unary minus 5", "-(a+b)", "(- (+ a b))"},
|
||||
{"Unary minus 6", "a + -b", "(+ a (- b))"},
|
||||
{"Unary minus 7", "-a + -b", "(+ (- a) (- b))"},
|
||||
|
||||
{"Function calls", "a()", "(λ a)"},
|
||||
{"Function calls 2", "a(1)", "(λ a 1)"},
|
||||
{"Function calls 3", "a(1)+1", "(+ (λ a 1) 1)"},
|
||||
@ -77,8 +99,10 @@ func TestParse(t *testing.T) {
|
||||
{"Function calls 25", "a(1-2*3)", "(λ a (- 1 (* 2 3)))"},
|
||||
{"Function calls 26", "1+2*a()+4", "(+ (+ 1 (* 2 (λ a))) 4)"},
|
||||
{"Function calls 27", "sum(a,b)*2+15*4", "(+ (* (λ sum a b) 2) (* 15 4))"},
|
||||
|
||||
{"Package function calls", "math.sum(a,b)", "(λ (. math sum) a b)"},
|
||||
{"Package function calls 2", "generic.math.sum(a,b)", "(λ (. (. generic math) sum) a b)"},
|
||||
|
||||
{"Array access", "a[0]", "(@ a 0)"},
|
||||
{"Array access 2", "a[b+c]", "(@ a (+ b c))"},
|
||||
{"Array access 3", "a.b[c]", "(@ (. a b) c)"},
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
type Operator struct {
|
||||
Symbol string
|
||||
Precedence int8
|
||||
Operands int
|
||||
Operands int8
|
||||
}
|
||||
|
||||
// Operators defines the operators used in the language.
|
||||
@ -19,6 +19,7 @@ var Operators = map[token.Kind]*Operator{
|
||||
token.Period: {".", 13, 2},
|
||||
token.Call: {"λ", 12, 1},
|
||||
token.Array: {"@", 12, 2},
|
||||
token.Negate: {"-", 11, 1},
|
||||
token.Not: {"!", 11, 1},
|
||||
token.Mul: {"*", 10, 2},
|
||||
token.Div: {"/", 10, 2},
|
||||
@ -73,7 +74,7 @@ func numOperands(symbol token.Kind) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
return operator.Operands
|
||||
return int(operator.Operands)
|
||||
}
|
||||
|
||||
func precedence(symbol token.Kind) int8 {
|
||||
|
@ -117,7 +117,11 @@ func Parse(tokens []token.Token) *Expression {
|
||||
newPrecedence := node.Precedence
|
||||
|
||||
if newPrecedence > oldPrecedence {
|
||||
cursor.LastChild().Replace(node)
|
||||
if len(cursor.Children) == numOperands(cursor.Token.Kind) {
|
||||
cursor.LastChild().Replace(node)
|
||||
} else {
|
||||
cursor.AddChild(node)
|
||||
}
|
||||
} else {
|
||||
start := cursor
|
||||
|
||||
|
Reference in New Issue
Block a user