fixed logical operator priority

This commit is contained in:
Umur Ozkul 2022-05-20 00:29:04 +02:00
parent 6b2e509aea
commit c1f269e9a4
3 changed files with 25 additions and 24 deletions

View File

@ -135,32 +135,33 @@ describe("Peggy parse", () => {
describe("logical", () => { describe("logical", () => {
testParse("true || false", "{(::or true false)}") testParse("true || false", "{(::or true false)}")
testParse("true && false", "{(::and true false)}") testParse("true && false", "{(::and true false)}")
testParse("a && b || c", "{(::and :a (::or :b :c))}") testParse("a * b + c", "{(::add (::multiply :a :b) :c)}") // for comparison
testParse("a && b || c && d", "{(::and (::and :a (::or :b :c)) :d)}") testParse("a && b || c", "{(::or (::and :a :b) :c)}")
testParse("a && !b || c", "{(::and :a (::or (::not :b) :c))}") testParse("a && b || c && d", "{(::or (::and :a :b) (::and :c :d))}")
testParse("a && b==c || d", "{(::and :a (::or (::equal :b :c) :d))}") testParse("a && !b || c", "{(::or (::and :a (::not :b)) :c)}")
testParse("a && b!=c || d", "{(::and :a (::or (::unequal :b :c) :d))}") testParse("a && b==c || d", "{(::or (::and :a (::equal :b :c)) :d)}")
testParse("a && !(b==c) || d", "{(::and :a (::or (::not (::equal :b :c)) :d))}") testParse("a && b!=c || d", "{(::or (::and :a (::unequal :b :c)) :d)}")
testParse("a && b>=c || d", "{(::and :a (::or (::largerEq :b :c) :d))}") testParse("a && !(b==c) || d", "{(::or (::and :a (::not (::equal :b :c))) :d)}")
testParse("a && !(b>=c) || d", "{(::and :a (::or (::not (::largerEq :b :c)) :d))}") testParse("a && b>=c || d", "{(::or (::and :a (::largerEq :b :c)) :d)}")
testParse("a && b<=c || d", "{(::and :a (::or (::smallerEq :b :c) :d))}") testParse("a && !(b>=c) || d", "{(::or (::and :a (::not (::largerEq :b :c))) :d)}")
testParse("a && b>c || d", "{(::and :a (::or (::larger :b :c) :d))}") testParse("a && b<=c || d", "{(::or (::and :a (::smallerEq :b :c)) :d)}")
testParse("a && b<c || d", "{(::and :a (::or (::smaller :b :c) :d))}") testParse("a && b>c || d", "{(::or (::and :a (::larger :b :c)) :d)}")
testParse("a && b<c[i] || d", "{(::and :a (::or (::smaller :b (::$atIndex :c :i)) :d))}") testParse("a && b<c || d", "{(::or (::and :a (::smaller :b :c)) :d)}")
testParse("a && b<c.i || d", "{(::and :a (::or (::smaller :b (::$atIndex :c 'i')) :d))}") testParse("a && b<c[i] || d", "{(::or (::and :a (::smaller :b (::$atIndex :c :i))) :d)}")
testParse("a && b<c(i) || d", "{(::and :a (::or (::smaller :b (::c :i)) :d))}") testParse("a && b<c.i || d", "{(::or (::and :a (::smaller :b (::$atIndex :c 'i'))) :d)}")
testParse("a && b<1+2 || d", "{(::and :a (::or (::smaller :b (::add 1 2)) :d))}") testParse("a && b<c(i) || d", "{(::or (::and :a (::smaller :b (::c :i))) :d)}")
testParse("a && b<1+2 || d", "{(::or (::and :a (::smaller :b (::add 1 2))) :d)}")
testParse( testParse(
"a && b<1+2*3 || d", "a && b<1+2*3 || d",
"{(::and :a (::or (::smaller :b (::add 1 (::multiply 2 3))) :d))}", "{(::or (::and :a (::smaller :b (::add 1 (::multiply 2 3)))) :d)}",
) )
testParse( testParse(
"a && b<1+2*-3+4 || d", "a && b<1+2*-3+4 || d",
"{(::and :a (::or (::smaller :b (::add (::add 1 (::multiply 2 (::unaryMinus 3))) 4)) :d))}", "{(::or (::and :a (::smaller :b (::add (::add 1 (::multiply 2 (::unaryMinus 3))) 4))) :d)}",
) )
testParse( testParse(
"a && b<1+2*3 || d ? true : false", "a && b<1+2*3 || d ? true : false",
"{(::$$ternary (::and :a (::or (::smaller :b (::add 1 (::multiply 2 3))) :d)) true false)}", "{(::$$ternary (::or (::and :a (::smaller :b (::add 1 (::multiply 2 3)))) :d) true false)}",
) )
}) })

View File

@ -245,8 +245,8 @@ function peg$parse(input, options) {
var peg$c8 = "else"; var peg$c8 = "else";
var peg$c9 = "?"; var peg$c9 = "?";
var peg$c10 = ":"; var peg$c10 = ":";
var peg$c11 = "&&"; var peg$c11 = "||";
var peg$c12 = "||"; var peg$c12 = "&&";
var peg$c13 = "=="; var peg$c13 = "==";
var peg$c14 = "!="; var peg$c14 = "!=";
var peg$c15 = "<="; var peg$c15 = "<=";
@ -303,8 +303,8 @@ function peg$parse(input, options) {
var peg$e8 = peg$literalExpectation("else", false); var peg$e8 = peg$literalExpectation("else", false);
var peg$e9 = peg$literalExpectation("?", false); var peg$e9 = peg$literalExpectation("?", false);
var peg$e10 = peg$literalExpectation(":", false); var peg$e10 = peg$literalExpectation(":", false);
var peg$e11 = peg$literalExpectation("&&", false); var peg$e11 = peg$literalExpectation("||", false);
var peg$e12 = peg$literalExpectation("||", false); var peg$e12 = peg$literalExpectation("&&", false);
var peg$e13 = peg$literalExpectation("==", false); var peg$e13 = peg$literalExpectation("==", false);
var peg$e14 = peg$literalExpectation("!=", false); var peg$e14 = peg$literalExpectation("!=", false);
var peg$e15 = peg$literalExpectation("<=", false); var peg$e15 = peg$literalExpectation("<=", false);

View File

@ -124,14 +124,14 @@ ternary
{ return nodeTernary(condition, trueExpression, falseExpression) } { return nodeTernary(condition, trueExpression, falseExpression) }
logicalAdditive logicalAdditive
= head:logicalMultiplicative tail:(_ operator:'&&' _nl arg:logicalMultiplicative {return {operator: operator, right: arg}})* = head:logicalMultiplicative tail:(_ operator:'||' _nl arg:logicalMultiplicative {return {operator: operator, right: arg}})*
{ return tail.reduce(function(result, element) { { return tail.reduce(function(result, element) {
return makeFunctionCall(toFunction[element.operator], [result, element.right]) return makeFunctionCall(toFunction[element.operator], [result, element.right])
}, head)} }, head)}
// start binary operators // start binary operators
logicalMultiplicative logicalMultiplicative
= head:equality tail:(_ operator:'||' _nl arg:equality {return {operator: operator, right: arg}})* = head:equality tail:(_ operator:'&&' _nl arg:equality {return {operator: operator, right: arg}})*
{ return tail.reduce(function(result, element) { { return tail.reduce(function(result, element) {
return makeFunctionCall(toFunction[element.operator], [result, element.right]) return makeFunctionCall(toFunction[element.operator], [result, element.right])
}, head)} }, head)}