Skip to content

Commit

Permalink
Add check for unsupported exponentiations
Browse files Browse the repository at this point in the history
Ref. #320
  • Loading branch information
treiher committed Jul 8, 2020
1 parent 0f30f7b commit 9bc4c9b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
23 changes: 23 additions & 0 deletions rflx/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ def prefixed_expression(expression: Expr) -> Expr:

return self.copy(structure=structure, types=types)

# pylint: disable=too-many-branches
def __verify(self) -> None:
type_fields = self.__types.keys() | {INITIAL, FINAL}
structure_fields = {l.source for l in self.structure} | {l.target for l in self.structure}
Expand Down Expand Up @@ -756,6 +757,28 @@ def __verify(self) -> None:
[("duplicate link", Subsystem.MODEL, Severity.INFO, l.location,) for l in links]
)

for l in self.structure:
exponentiations = And(l.condition, l.first, l.length).findall(
lambda x: isinstance(x, Pow)
)
for e in exponentiations:
assert isinstance(e, Pow)
variables = e.right.findall(lambda x: isinstance(x, Variable))
if variables:
self.error.append(
f'unsupported expression in "{self.identifier}"',
Subsystem.MODEL,
Severity.ERROR,
e.location,
)
for v in variables:
self.error.append(
f'variable "{v}" in exponent',
Subsystem.MODEL,
Severity.INFO,
v.location,
)

def __verify_conditions(self) -> None:
literals = qualified_literals(self.types, self.package)
variables = {f.identifier for f in self.fields}
Expand Down
29 changes: 29 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,35 @@ def test_message_multiple_duplicate_links() -> None:
)


def test_message_unsupported_expression() -> None:
x = Field("X")

structure = [
Link(INITIAL, x),
Link(
x,
FINAL,
condition=LessEqual(
Pow(
Number(2),
Add(Variable("X", location=Location((10, 23))), Number(1)),
location=Location((10, 19)),
),
Number(1024),
),
),
]

types = {x: MODULAR_INTEGER}

assert_message_model_error(
structure,
types,
'^<stdin>:10:19: model: error: unsupported expression in "P.M"\n'
'<stdin>:10:23: model: info: variable "X" in exponent',
)


def test_message_unreachable_field() -> None:
structure = [
Link(INITIAL, Field("X")),
Expand Down

0 comments on commit 9bc4c9b

Please sign in to comment.