Skip to content

Commit aa874bd

Browse files
committed
Fix handling expressions such as "!value"
Substitution converts this to " not value" and space at beginning needs stripping. Add test case.
1 parent a64096d commit aa874bd

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

Tools/Python/evaluator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def run(self, expr):
146146
raise ValueError("Empty expression")
147147
processed = expr.replace("&&", " and ").replace("||", " or ")
148148
processed = re.sub(r'!(?!=)', ' not ', processed)
149-
tree = ast.parse(processed, mode='eval')
149+
tree = ast.parse(processed.strip(), mode='eval')
150150
return self._eval(tree.body)
151151

152152

Tools/Python/evaluator/evaluator_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def get_variable(name: str):
5151
("~0 & 0xFF", 255, "Bitwise Invert & AND"),
5252
("align_up(15, 8)", 16, "Custom Function"),
5353
("NUMERIC_VAR * 2", 123 * 2, "Custom variable"),
54+
("!NUMERIC_VAR", 0, "Bool invert"),
5455

5556
# --- Multi-Argument & Custom Math ---
5657
("max(1, 5, 2)", 5, "Variadic Function (max)"),
@@ -103,7 +104,7 @@ def get_variable(name: str):
103104
actual = evaluator.run(expr)
104105
success = (actual == expected)
105106
except Exception as e:
106-
success = f'Error: {e}'.startswith(expected)
107+
success = f'Error: {e}'.startswith(str(expected))
107108
actual = repr(e)
108109

109110
if success:

0 commit comments

Comments
 (0)