From 6a17d6994dc1b9a249ad5be31e06a7ba9e80454d Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Sat, 27 Jul 2013 17:58:17 +0300 Subject: [PATCH] Cleaner, simpler elif. --- interpreter.py | 23 ++-------------------- mparser.py | 2 +- nodes.py | 7 ------- test cases/common/41 elif/meson.build | 28 +++++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 29 deletions(-) create mode 100644 test cases/common/41 elif/meson.build diff --git a/interpreter.py b/interpreter.py index 154ed40e9..5b2dbbac2 100644 --- a/interpreter.py +++ b/interpreter.py @@ -1209,32 +1209,13 @@ class Interpreter(): self.evaluate_codeblock(node.trueblock) else: block = node.falseblock - if isinstance(block, nodes.ElifStatement): - self.evaluate_elif(block) + if isinstance(block, nodes.IfStatement): + self.evaluate_if(block) else: self.evaluate_codeblock(block) else: raise InvalidCode('If clause does not evaluate to true or false.') - def evaluate_elif(self, node): - result = self.evaluate_statement(node.clause) - cond = None - if isinstance(result, nodes.BoolExpression) or \ - isinstance(result, nodes.BoolStatement): - cond = result.get_value() - if isinstance(result, bool): - cond = result - if cond is not None: - if cond: - self.evaluate_codeblock(node.trueblock) - else: - block = node.elseblock - if isinstance(block, nodes.ElifStatement): - self.evaluate_elif(block) - self.evaluate_codeblock(block) - else: - raise InvalidCode('Elif clause does not evaluate to true or false.') - def is_elementary_type(self, v): if isinstance(v, int) or isinstance(v, str) or isinstance(v, bool): return True diff --git a/mparser.py b/mparser.py index 20531d429..e9cad0f66 100644 --- a/mparser.py +++ b/mparser.py @@ -208,7 +208,7 @@ def p_statement_not(t): def p_statement_elif(t): 'elseblock : ELIF statement EOL codeblock elseblock' - t[0] = nodes.ElifStatement(t[2], t[4], t[5], t.lineno(1)) + t[0] = nodes.IfStatement(t[2], t[4], t[5], t.lineno(1)) def p_empty_else(t): 'elseblock : ' diff --git a/nodes.py b/nodes.py index bc149f800..49d68e8ff 100644 --- a/nodes.py +++ b/nodes.py @@ -106,13 +106,6 @@ class IfStatement(Statement): self.trueblock = trueblock self.falseblock = falseblock -class ElifStatement(Statement): - def __init__(self, clause, trueblock, elseblock, lineno): - Statement.__init__(self, lineno) - self.clause = clause - self.trueblock = trueblock - self.elseblock = elseblock - def get_clause(self): return self.clause diff --git a/test cases/common/41 elif/meson.build b/test cases/common/41 elif/meson.build new file mode 100644 index 000000000..d37df0080 --- /dev/null +++ b/test cases/common/41 elif/meson.build @@ -0,0 +1,28 @@ +project('elseif', 'c') + +t = true +f = false + +if true + message('Ok.') +elif true + error('Error') +else + error('Error') +endif + +if f + error('Error.') +elif t + message('Ok') +else + error('Error') +endif + +if f + error('Error.') +elif false + error('Error') +else + message('Ok') +endif