Skip to content

Commit

Permalink
Fix division remainder operator (%) (#272)
Browse files Browse the repository at this point in the history
* Fix compatibility with Jinja for the % operator.

* Rename enum value DivReminder -> DivRemainder

* Add test for the remainder operator %

---------

Co-authored-by: Vesselin Atanasov <vesselin@awcreator.com>
  • Loading branch information
MeanSquaredError and Vesselin Atanasov authored Nov 22, 2024
1 parent 86dfb93 commit 2053cfa
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/expression_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ InternalValue BinaryExpression::Evaluate(RenderContext& context)
case jinja2::BinaryExpression::Minus:
case jinja2::BinaryExpression::Mul:
case jinja2::BinaryExpression::Div:
case jinja2::BinaryExpression::DivReminder:
case jinja2::BinaryExpression::DivRemainder:
case jinja2::BinaryExpression::DivInteger:
case jinja2::BinaryExpression::Pow:
result = Apply2<visitors::BinaryMathOperation>(leftVal, rightVal, m_oper);
Expand Down
2 changes: 1 addition & 1 deletion src/expression_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class BinaryExpression : public Expression
Minus,
Mul,
Div,
DivReminder,
DivRemainder,
DivInteger,
Pow,
StringConcat
Expand Down
2 changes: 1 addition & 1 deletion src/expression_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ ExpressionParser::ParseResult<ExpressionEvaluatorPtr<Expression>> ExpressionPars
operation = BinaryExpression::DivInteger;
break;
case '%':
operation = BinaryExpression::DivReminder;
operation = BinaryExpression::DivRemainder;
break;
default:
lexer.ReturnToken();
Expand Down
6 changes: 3 additions & 3 deletions src/value_visitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ struct BinaryMathOperation : BaseVisitor<>
case jinja2::BinaryExpression::Div:
result = left / right;
break;
case jinja2::BinaryExpression::DivReminder:
result = std::remainder(left, right);
case jinja2::BinaryExpression::DivRemainder:
result = std::fmod(left, right);
break;
case jinja2::BinaryExpression::DivInteger:
{
Expand Down Expand Up @@ -607,7 +607,7 @@ struct BinaryMathOperation : BaseVisitor<>
result = left / right;
break;
case jinja2::BinaryExpression::Div:
case jinja2::BinaryExpression::DivReminder:
case jinja2::BinaryExpression::DivRemainder:
case jinja2::BinaryExpression::Pow:
result = this->operator ()(static_cast<double>(left), static_cast<double>(right));
break;
Expand Down
2 changes: 2 additions & 0 deletions test/expressions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ R"(
{{ 7 / 3}}
{{ 7 // 3 }}
{{ 7 % intValue }}
{{ 11 % 7 }}
{{ 3 ** 4 }}
{{ 10 ** -2 }}
{{ 10/10 + 2*5 }}
Expand Down Expand Up @@ -48,6 +49,7 @@ R"(
2.3333333
2
1
4
81
0.01
11
Expand Down

0 comments on commit 2053cfa

Please sign in to comment.