|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import ast |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from expr_simplifier.analyzer import analyze_external_symbols |
| 7 | + |
| 8 | + |
| 9 | +class AnyObject: |
| 10 | + """A top type that can represent any object in Python""" |
| 11 | + |
| 12 | + def __init__(self, name: str): |
| 13 | + self.name = name |
| 14 | + |
| 15 | + def __add__(self, other: Any): |
| 16 | + other = AnyObject.to_any_object(other) |
| 17 | + return AnyObject(f"{self.name} + {other.name}") |
| 18 | + |
| 19 | + def __radd__(self, other: Any): |
| 20 | + other = AnyObject.to_any_object(other) |
| 21 | + return AnyObject(f"{other.name} + {self.name}") |
| 22 | + |
| 23 | + def __mul__(self, other: Any): |
| 24 | + other = AnyObject.to_any_object(other) |
| 25 | + return AnyObject(f"{self.name} * {other.name}") |
| 26 | + |
| 27 | + def __rmul__(self, other: Any): |
| 28 | + other = AnyObject.to_any_object(other) |
| 29 | + return AnyObject(f"{other.name} * {self.name}") |
| 30 | + |
| 31 | + def __truediv__(self, other: Any): |
| 32 | + other = AnyObject.to_any_object(other) |
| 33 | + return AnyObject(f"{self.name} / {other.name}") |
| 34 | + |
| 35 | + def __rtruediv__(self, other: Any): |
| 36 | + other = AnyObject.to_any_object(other) |
| 37 | + return AnyObject(f"{other.name} / {self.name}") |
| 38 | + |
| 39 | + def __floordiv__(self, other: Any): |
| 40 | + other = AnyObject.to_any_object(other) |
| 41 | + return AnyObject(f"{self.name} // {other.name}") |
| 42 | + |
| 43 | + def __rfloordiv__(self, other: Any): |
| 44 | + other = AnyObject.to_any_object(other) |
| 45 | + return AnyObject(f"{other.name} // {self.name}") |
| 46 | + |
| 47 | + def __getattr__(self, name: str): |
| 48 | + return AnyObject(f"{self.name}.{name}") |
| 49 | + |
| 50 | + def __getitem__(self, key: Any): |
| 51 | + key = AnyObject.to_any_object(key) |
| 52 | + return AnyObject(f"{self.name}[{key.name}]") |
| 53 | + |
| 54 | + def __call__(self, *args: Any, **kwargs: Any): |
| 55 | + formatted_args = ", ".join(AnyObject.to_any_object(arg).name for arg in args) |
| 56 | + formatted_kwargs = ", ".join(f"{key}={AnyObject.to_any_object(value).name}" for key, value in kwargs.items()) |
| 57 | + return AnyObject(f"{self.name}({formatted_args}, {formatted_kwargs})") |
| 58 | + |
| 59 | + def __bool__(self): |
| 60 | + return hash(self.name) % 2 == 0 |
| 61 | + |
| 62 | + def __hash__(self): |
| 63 | + return hash(self.name) |
| 64 | + |
| 65 | + # def __eq__(self, other: object): |
| 66 | + # other = AnyObject.to_any_object(other) |
| 67 | + # return AnyObject(f"{self.name} == {other.name}") |
| 68 | + |
| 69 | + # def __ne__(self, other: object): |
| 70 | + # other = AnyObject.to_any_object(other) |
| 71 | + # return AnyObject(f"{self.name} != {other.name}") |
| 72 | + |
| 73 | + def __lt__(self, other: Any): |
| 74 | + other = AnyObject.to_any_object(other) |
| 75 | + return AnyObject(f"{self.name} < {other.name}") |
| 76 | + |
| 77 | + def __le__(self, other: Any): |
| 78 | + other = AnyObject.to_any_object(other) |
| 79 | + return AnyObject(f"{self.name} <= {other.name}") |
| 80 | + |
| 81 | + def __repr__(self): |
| 82 | + return f"AnyObject({self.name})" |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def to_any_object(value: Any): |
| 86 | + if isinstance(value, AnyObject): |
| 87 | + return value |
| 88 | + return AnyObject.from_raw(value) |
| 89 | + |
| 90 | + @staticmethod |
| 91 | + def from_raw(value: Any): |
| 92 | + return AnyObject(f"$R({value!r})") |
| 93 | + |
| 94 | + |
| 95 | +def check_expr_at_runtime(original_ast: ast.AST, transformed_ast: ast.AST) -> None: |
| 96 | + original_externel_symbols = analyze_external_symbols(original_ast) |
| 97 | + transformed_externel_symbols = analyze_external_symbols(transformed_ast) |
| 98 | + assert original_externel_symbols == transformed_externel_symbols, "External symbols are different" |
| 99 | + original_expr = ast.unparse(original_ast) |
| 100 | + transformed_expr = ast.unparse(transformed_ast) |
| 101 | + externel_fake_values = {symbol: AnyObject(symbol) for symbol in original_externel_symbols} |
| 102 | + original_expr_result = eval(original_expr, externel_fake_values) |
| 103 | + transformed_expr_result = eval(transformed_expr, externel_fake_values) |
| 104 | + assert repr(original_expr_result) == repr(transformed_expr_result), "Results are different" |
0 commit comments