11import operator
2+ from types import LambdaType
23from mamba .exceptions import *
34import mamba .symbol_table
45
@@ -202,8 +203,8 @@ class BinaryOperation(BaseExpression):
202203 '==' : operator .eq ,
203204 '!=' : operator .ne ,
204205
205- 'and' : operator . and_ ,
206- 'or' : operator . or_ ,
206+ 'and' : lambda a , b : a . eval () and b . eval () ,
207+ 'or' : lambda a , b : a . eval () or b . eval () ,
207208
208209 '&' : operator .and_ ,
209210 '|' : operator .or_ ,
@@ -221,11 +222,23 @@ def __init__(self, left, right, op):
221222 self .op = op
222223
223224 def eval (self ):
224- left = self . left . eval ()
225- right = self . right . eval ()
225+ left = None
226+ right = None
226227
227228 try :
228- return self .__operations [self .op ](left , right )
229+ # find the operation that needs to be performed
230+ op = self .__operations [self .op ]
231+
232+ # in case of lambda, pass the arguments unevaluated as they will
233+ # be properly evaluated only when necessary during the lambda call.
234+ if isinstance (op , LambdaType ):
235+ return op (self .left , self .right )
236+
237+ # otherwise, straight up call the operation, also save the variables
238+ # in case they are to be used for the exception block
239+ left = self .left .eval ()
240+ right = self .right .eval ()
241+ return op (left , right )
229242 except TypeError :
230243 fmt = (left .__class__ .__name__ , left , self .op , right .__class__ .__name__ , right )
231244 raise InterpreterRuntimeError ("Unable to apply operation (%s: %s) %s (%s: %s)" % fmt )
0 commit comments