Teg
Teg copied to clipboard
Broken boolean overrides
Boolean operator overrides are parsed incorrectly; currently you have to explicitly construct an And object to use it
Here's an example unittest to see this
def test_bool_override(self):
from teg.lang.integrable_program import And
x = Var('x', 0)
a = And(1 < x, x < 2)
b = 1 < x and x < 2
c = 1 < x < 2
print(f'{a}:\t{evaluate(a)}')
print(f'{b}:\t{evaluate(b)}')
print(f'{c}:\t{evaluate(c)}')
self.assertEqual(a, b)
self.assertEqual(a, c)
Unfortunately, these warts are part of the price that we pay for living in Python rather than writing a parser. You can use b = (1 < x) & (x < 2) instead though!
b = 1 < x and x < 2
Due to short-circuiting, the and operator cannot be overloaded.
c = 1 < x < 2
Short-circuiting is the issue here as well. To test, run
def f():
print('hi')
return 0
1 < 0 < f()
As a result, I'd view this less as a bug and more as a factor in deciding what our frontend should be in the end.
Yes, it does look like there are AST hacks that work. For example, https://stackoverflow.com/questions/25469423/defining-new-semantics-for-expressions-in-python/25469702#25469702