diff --git a/interpreter.py b/interpreter.py index eb04fbbe9..aa7a0f932 100644 --- a/interpreter.py +++ b/interpreter.py @@ -843,6 +843,8 @@ class Interpreter(): return cur elif isinstance(cur, nodes.AndStatement): return self.evaluate_andstatement(cur) + elif isinstance(cur, nodes.OrStatement): + return self.evaluate_orstatement(cur) else: raise InvalidCode("Unknown statement.") @@ -1247,6 +1249,21 @@ class Interpreter(): raise InterpreterException('Second argument to "and" is not a boolean.') return r + def evaluate_orstatement(self, cur): + l = self.evaluate_statement(cur.left) + if isinstance(l, nodes.BoolStatement): + l = l.get_value() + if not isinstance(l, bool): + raise InterpreterException('First argument to "or" is not a boolean.') + if l: + return True + r = self.evaluate_statement(cur.right) + if isinstance(r, nodes.BoolStatement): + r = r.get_value() + if not isinstance(r, bool): + raise InterpreterException('Second argument to "or" is not a boolean.') + return r + def evaluate_arraystatement(self, cur): (arguments, kwargs) = self.reduce_arguments(cur.get_args()) if len(kwargs) > 0: diff --git a/mparser.py b/mparser.py index 5a28ccc87..c4f1c390f 100644 --- a/mparser.py +++ b/mparser.py @@ -71,7 +71,7 @@ precedence = ( ('nonassoc', 'EQUALS', 'NEQUALS'), ('left', 'OR'), ('left', 'AND'), -('nonassoc', 'COLON') +('nonassoc', 'COLON'), ('left', 'DOT'), ) @@ -196,6 +196,10 @@ def p_statement_and(t): 'statement : statement AND statement' t[0] = nodes.AndStatement(t[1], t[3]) +def p_statement_or(t): + 'statement : statement OR statement' + t[0] = nodes.OrStatement(t[1], t[3]) + def p_empty_else(t): 'elseblock : ' return None diff --git a/test cases/common/40 logic ops/meson.build b/test cases/common/40 logic ops/meson.build index a4466f449..8d2dc9f59 100644 --- a/test cases/common/40 logic ops/meson.build +++ b/test cases/common/40 logic ops/meson.build @@ -27,13 +27,13 @@ else error('Not ok.') endif -if t and t +if true and t message('Ok.') else error('Not ok.') endif -if t and f +if t and false error('Not ok.') else message('Ok.') @@ -44,3 +44,21 @@ if f and t else message('Ok.') endif + +if f or false + error('Not ok.') +else + message('Ok.') +endif + +if true or f + message('Ok.') +else + error('Not ok.') +endif + +if t or true + message('Ok.') +else + error('Not ok.') +endif