package expression_test import ( "testing" "git.urbach.dev/cli/q/src/expression" "git.urbach.dev/cli/q/src/token" "git.urbach.dev/go/assert" ) func TestParse(t *testing.T) { tests := []struct { Name string Expression string Result string }{ {"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)"}, {"Grouping left", "(1+2)*3", "(* (+ 1 2) 3)"}, {"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))"}, {"Grouping error", "(", ""}, {"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)"}, {"Operator priority 4", "1+2*(3+4)+5", "(+ (+ 1 (* 2 (+ 3 4))) 5)"}, {"Operator priority 5", "1+2*3*4", "(+ 1 (* (* 2 3) 4))"}, {"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 not", "!", "!"}, {"Unary not 2", "!a", "(! a)"}, {"Unary not 3", "!!a", "(! (! a))"}, {"Unary not 4", "!!!a", "(! (! (! a)))"}, {"Unary not 5", "!(!a)", "(! (! a))"}, {"Unary not 6", "!(a||b)", "(! (|| a b))"}, {"Unary not 7", "a || !b", "(|| a (! b))"}, {"Unary minus", "-", "-"}, {"Unary minus 2", "-a", "(- a)"}, {"Unary minus 3", "--a", "(- (- a))"}, {"Unary minus 4", "---a", "(- (- (- a)))"}, {"Unary minus 5", "-(-a)", "(- (- a))"}, {"Unary minus 6", "-a+b", "(+ (- a) b)"}, {"Unary minus 7", "-(a+b)", "(- (+ a b))"}, {"Unary minus 8", "a + -b", "(+ a (- b))"}, {"Unary minus 9", "-a + -b", "(+ (- a) (- b))"}, {"Assign bitwise operation", "a|=b", "(|= a b)"}, {"Assign bitwise operation 2", "a|=b<