Cleaner, simpler elif.

pull/15/head
Jussi Pakkanen 11 years ago
parent e8fba977f4
commit 6a17d6994d
  1. 23
      interpreter.py
  2. 2
      mparser.py
  3. 7
      nodes.py
  4. 28
      test cases/common/41 elif/meson.build

@ -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

@ -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 : '

@ -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

@ -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
Loading…
Cancel
Save