Skip to content

Commit

Permalink
Changed precedence for and/or
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaumgarten committed Nov 26, 2020
1 parent 64bf4ff commit 2b018c5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
36 changes: 18 additions & 18 deletions pkg/optimizers/expression_inversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<b",
"x=not not y": "x=y",
"x=not not y<3": "x=y<3",
"x=not a and not b or not c and not d": "x=not (a or b and c or d)",
"x=not (not a and not b)": "x=a or b",
"x=not (not a and b)": "x=a or not b",
"x=not a and not b and not c and not d": "x=not (a or b or c or d)",
"x=:number%3==0 and :number%5==0": "x=:number%3==0 and :number%5==0",
"x=not(:number%3==0 and :number%5==0)": "x=:number%3!=0 or :number%5!=0",
"y=not (c++ <5)": "y=c++ >=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<b",
"x=not not y": "x=y",
"x=not not y<3": "x=y<3",
"x=(not a and not b) or (not c and not d)": "x=not (a or b and c or d)",
"x=not (not a and not b)": "x=a or b",
"x=not (not a and b)": "x=a or not b",
"x=not a and not b and not c and not d": "x=not (a or b or c or d)",
"x=:number%3==0 and :number%5==0": "x=:number%3==0 and :number%5==0",
"x=not(:number%3==0 and :number%5==0)": "x=:number%3!=0 or :number%5!=0",
"y=not (c++ <5)": "y=c++ >=5",
"y=c++ >=5": "y=c++ >=5",
}

func TestInversionOptimization(t *testing.T) {
Expand Down
14 changes: 9 additions & 5 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions pkg/parser/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2b018c5

Please sign in to comment.