Skip to content

Change +, -, *, /, //, and % operators' associativity to left-to-right #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 55 additions & 52 deletions src/expression_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,64 +196,67 @@ ExpressionParser::ParseResult<ExpressionEvaluatorPtr<Expression>> ExpressionPars

ExpressionParser::ParseResult<ExpressionEvaluatorPtr<Expression>> ExpressionParser::ParseMathPlusMinus(LexScanner& lexer)
{
auto left = ParseMathMulDiv(lexer);
if (!left)
return left;

auto tok = lexer.NextToken();
BinaryExpression::Operation operation;
switch (tok.type)
{
case '+':
operation = BinaryExpression::Plus;
break;
case '-':
operation = BinaryExpression::Minus;
break;
default:
lexer.ReturnToken();
return left;
auto res = ParseMathMulDiv(lexer);
if (!res)
return res;

while (true) {
auto tok = lexer.NextToken();
BinaryExpression::Operation operation;
switch (tok.type)
{
case '+':
operation = BinaryExpression::Plus;
break;
case '-':
operation = BinaryExpression::Minus;
break;
default:
lexer.ReturnToken();
return res;
}
auto right = ParseMathMulDiv(lexer);
if (!right)
return right;
res = std::make_shared<BinaryExpression>(operation, *res, *right);
}

auto right = ParseMathPlusMinus(lexer);
if (!right)
return right;

return std::make_shared<BinaryExpression>(operation, *left, *right);
return res;
}

ExpressionParser::ParseResult<ExpressionEvaluatorPtr<Expression>> ExpressionParser::ParseMathMulDiv(LexScanner& lexer)
{
auto left = ParseUnaryPlusMinus(lexer);
if (!left)
return left;

auto tok = lexer.NextToken();
BinaryExpression::Operation operation;
switch (tok.type)
{
case '*':
operation = BinaryExpression::Mul;
break;
case '/':
operation = BinaryExpression::Div;
break;
case Token::DivDiv:
operation = BinaryExpression::DivInteger;
break;
case '%':
operation = BinaryExpression::DivRemainder;
break;
default:
lexer.ReturnToken();
return left;
auto res = ParseUnaryPlusMinus(lexer);
if (!res)
return res;

while (true) {
auto tok = lexer.NextToken();
BinaryExpression::Operation operation;
switch (tok.type)
{
case '*':
operation = BinaryExpression::Mul;
break;
case '/':
operation = BinaryExpression::Div;
break;
case Token::DivDiv:
operation = BinaryExpression::DivInteger;
break;
case '%':
operation = BinaryExpression::DivRemainder;
break;
default:
lexer.ReturnToken();
return res;
}
auto right = ParseUnaryPlusMinus(lexer);
if (!right)
return right;
res = std::make_shared<BinaryExpression>(operation, *res, *right);
}

auto right = ParseMathMulDiv(lexer);
if (!right)
return right;

return std::make_shared<BinaryExpression>(operation, *left, *right);

return res;
}

ExpressionParser::ParseResult<ExpressionEvaluatorPtr<Expression>> ExpressionParser::ParseUnaryPlusMinus(LexScanner& lexer)
Expand Down
4 changes: 4 additions & 0 deletions test/expressions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ R"(
{{ 3 ** 4 }}
{{ 10 ** -2 }}
{{ 10/10 + 2*5 }}
{{ 10 - 2 - 4 }}
{{ 200 / 2 / 2 }}
{{ ([1, 2] + [3, 4]) | pprint }}
{{ 'Hello' + " " + 'World ' + stringValue }}
{{ 'Hello' + " " + 'World ' + wstringValue }}
Expand Down Expand Up @@ -53,6 +55,8 @@ R"(
81
0.01
11
4
50
[1, 2, 3, 4]
Hello World rain
Hello World rain
Expand Down