diff --git a/src/arch/z80/peephole/evaluator.py b/src/arch/z80/peephole/evaluator.py index 29156458..82c57632 100644 --- a/src/arch/z80/peephole/evaluator.py +++ b/src/arch/z80/peephole/evaluator.py @@ -195,7 +195,7 @@ def __init__(self, expression): expression[2] = Evaluator(expression[2]) else: # It's a list assert len(expression) % 2 # Must be odd length - assert all(x == FN.OP_COMMA for i, x in enumerate(expression) if i % 2) + assert all(x == FN.OP_COMMA for i, x in enumerate(expression) if i % 2), f"Invalid expression {expression}" self.expression = [Evaluator(x) if not i % 2 else x for i, x in enumerate(expression)] @staticmethod diff --git a/src/arch/z80/peephole/parser.py b/src/arch/z80/peephole/parser.py index 47332f3b..99d2958e 100644 --- a/src/arch/z80/peephole/parser.py +++ b/src/arch/z80/peephole/parser.py @@ -161,6 +161,10 @@ def parse_ifline(if_line: str, lineno: int) -> TreeType | None: errmsg.warning(lineno, "missing element in list") return None + if any(x != FN.OP_COMMA for i, x in enumerate(expr) if i % 2): + errmsg.warning(lineno, f"Invalid list {expr}") + return None + stack[-1].append(expr) expr = stack.pop() else: diff --git a/tests/arch/zx48k/peephole/test_parser.py b/tests/arch/zx48k/peephole/test_parser.py index 844f2b24..54762b2e 100644 --- a/tests/arch/zx48k/peephole/test_parser.py +++ b/tests/arch/zx48k/peephole/test_parser.py @@ -399,14 +399,8 @@ def test_parse_if_must_start_in_a_new_line(self): def test_parse_with_ending_binary_error(self): result = parser.parse_str( """ - ;; Remove the boolean normalization if it's done after calling - ;; certain routines that return the bool result already normalized. - - ;; The sequence - ;; sub 1 - ;; sbc a, a - ;; inc a - ;; can be removed + ;; Sample Comment + ;; OLEVEL: 1 OFLAG: 20 @@ -428,15 +422,6 @@ def test_parse_with_ending_binary_error(self): def test_parse_with_comma_error(self): result = parser.parse_str( """ - ;; Remove the boolean normalization if it's done after calling - ;; certain routines that return the bool result already normalized. - - ;; The sequence - ;; sub 1 - ;; sbc a, a - ;; inc a - ;; can be removed - OLEVEL: 1 OFLAG: 20 @@ -458,15 +443,6 @@ def test_parse_with_comma_error(self): def test_parse_with_nested_comma_error(self): result = parser.parse_str( """ - ;; Remove the boolean normalization if it's done after calling - ;; certain routines that return the bool result already normalized. - - ;; The sequence - ;; sub 1 - ;; sbc a, a - ;; inc a - ;; can be removed - OLEVEL: 1 OFLAG: 20 @@ -484,3 +460,43 @@ def test_parse_with_nested_comma_error(self): """ ) assert result is None + + def test_parse_with_list_error(self): + result = parser.parse_str( + """ + OLEVEL: 1 + OFLAG: 20 + + REPLACE {{ + $1 + }} + + WITH {{ + }} + + IF {{ + $1 IN ("x", "y" . "pera") + }} + """ + ) + assert result is None + + def test_parse_with_list_error2(self): + result = parser.parse_str( + """ + OLEVEL: 1 + OFLAG: 20 + + REPLACE {{ + $1 + }} + + WITH {{ + }} + + IF {{ + $1 IN ("x", , , "pera") + }} + """ + ) + assert result is None