From 2b018c5d4e4fb0a1b1cb5129031d53fb1bc9716d Mon Sep 17 00:00:00 2001 From: dbaumgarten Date: Thu, 26 Nov 2020 21:26:31 +0100 Subject: [PATCH] Changed precedence for and/or --- pkg/optimizers/expression_inversion_test.go | 36 ++++++++++----------- pkg/parser/parser.go | 14 +++++--- pkg/parser/printer.go | 28 ++++++++-------- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/pkg/optimizers/expression_inversion_test.go b/pkg/optimizers/expression_inversion_test.go index a7f4ce4..4ea5688 100644 --- a/pkg/optimizers/expression_inversion_test.go +++ b/pkg/optimizers/expression_inversion_test.go @@ -5,24 +5,24 @@ import ( ) var inversionCases = map[string]string{ - "a=123": "a=123", - "a=not true": "a=not true", - "x=a or b": "x=a or b", - "x=not (a or b)": "x=not (a or b)", - "x=not a and not b": "x=not (a or b)", - "x=not (a==b)": "x=a!=b", - "x=not (a > b)": "x=a<=b", - "x=not (a >= b)": "x=a=5", - "y=c++ >=5": "y=c++ >=5", + "a=123": "a=123", + "a=not true": "a=not true", + "x=a or b": "x=a or b", + "x=not (a or b)": "x=not (a or b)", + "x=not a and not b": "x=not (a or b)", + "x=not (a==b)": "x=a!=b", + "x=not (a > b)": "x=a<=b", + "x=not (a >= b)": "x=a=5", + "y=c++ >=5": "y=c++ >=5", } func TestInversionOptimization(t *testing.T) { diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 8240bfc..1e5854d 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -423,22 +423,26 @@ func (p *Parser) ParseBinaryExpression(idx int) ast.Expression { switch idx { case 0: - ops = []string{"or", "and"} + ops = []string{"and"} expectedType = ast.TypeKeyword break case 1: - ops = []string{"==", "!="} + ops = []string{"or"} + expectedType = ast.TypeKeyword break case 2: - ops = []string{"<=", ">=", "<", ">"} + ops = []string{"==", "!="} break case 3: - ops = []string{"+", "-"} + ops = []string{"<=", ">=", "<", ">"} break case 4: - ops = []string{"*", "/", "%"} + ops = []string{"+", "-"} break case 5: + ops = []string{"*", "/", "%"} + break + case 6: ops = []string{"^"} leftAssoc = false break diff --git a/pkg/parser/printer.go b/pkg/parser/printer.go index bd08957..34b6571 100644 --- a/pkg/parser/printer.go +++ b/pkg/parser/printer.go @@ -41,21 +41,21 @@ type Printer struct { } var operatorPriority = map[string]int{ - "or": 0, "and": 0, - "==": 1, - "!=": 1, - ">=": 2, - "<=": 2, - ">": 2, - "<": 2, - "+": 3, - "-": 3, - "*": 4, - "/": 4, - "%": 4, - "^": 5, - "not": 6, + "or": 1, + "==": 2, + "!=": 2, + ">=": 3, + "<=": 3, + ">": 3, + "<": 3, + "+": 4, + "-": 4, + "*": 5, + "/": 5, + "%": 5, + "^": 6, + "not": 7, } // end and else are missing here, because unlike other keywords they might require a space after them