On 17 November 2015 at 15:42, Nathaniel Smith
So for clarity: True and -> right hand side evaluated stand alone False and -> False True or -> True False or -> right hand side evaluated stand alone
We can roll that up using the parse tree: (('posix', '==', 'dud'), [('and', ('posix', '==', 'odd')), ('or', ('posix', '==', 'fred'))])) evaluate the start to get a 'result' pop an expression from the beginning of the list giving opcode, next. lookup (result, opcode) in the above truth table, giving one of True, False, None on None, result = next and loop. on True or False, return that.
Not sure if we're communicating or not? The case I'm concerned about is
a or b and c
which Python parses as (a or (b and c)) because 'and' has higher precedence than 'or'. So for example in Python,
True or True and False
returns True, but if you use a left-to-right evaluation rule than it returns False.
True or True and False
->
lookup = {
(True, 'and'): None,
(False, 'and'): False,
(True, 'or'): True,
(False, 'or'): None}
def evaluate(expr):
result = expr[0]
remainder = expr[1]
while remainder:
opcode, next = remainder.pop(0)
branch = lookup[(result, opcode)]
if branch is not None:
return branch
result = next
return result
expr = (True, [('or', True), ('and', False)])
evaluate(expr)
-> True
So, I think what I describe does what you want it to.
-Rob
--
Robert Collins