Skip to content

Commit

Permalink
Add modulo operation to specification parser
Browse files Browse the repository at this point in the history
Ref. #476
  • Loading branch information
treiher committed Oct 26, 2020
1 parent 61c6f66 commit 88ef28e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion rflx/specification/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
Less,
LessEqual,
MessageAggregate,
Mod,
Mul,
NotEqual,
NotIn,
Expand Down Expand Up @@ -202,7 +203,7 @@ def expression(restricted: bool = False) -> Token:
expr = Forward()

highest_precedence_operator = Literal("**")
multiplying_operator = Literal("*") | Literal("/")
multiplying_operator = Literal("*") | Literal("/") | Literal("mod")
binary_adding_operator = Literal("+") | Literal("-")
relational_operator = (
Keyword("=") | Keyword("/=") | Keyword("<=") | Keyword("<") | Keyword(">=") | Keyword(">")
Expand Down Expand Up @@ -784,6 +785,7 @@ def parse_mathematical_operator(string: str, location: int, tokens: ParseResults
"-": Sub,
"*": Mul,
"/": Div,
"mod": Mod,
"**": Pow,
}
return parse_operator(operators, string, location, tokens)
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/specification/grammar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ def test_expression_suffix(string: str, expected: expr.Expr) -> None:
"A + B * (-8)",
expr.Add(expr.Variable("A"), expr.Mul(expr.Variable("B"), expr.Number(-8))),
),
(
"A + B mod 8 * 2",
expr.Add(
expr.Variable("A"),
expr.Mul(expr.Mod(expr.Variable("B"), expr.Number(8)), expr.Number(2)),
),
),
],
)
def test_expression_mathematical(string: str, expected: expr.Expr) -> None:
Expand Down

0 comments on commit 88ef28e

Please sign in to comment.