Comparisons work.

pull/15/head
Jussi Pakkanen 11 years ago
parent d6924da005
commit 3c591385ed
  1. 10
      interpreter.py
  2. 16
      parsertest.py

@ -725,7 +725,7 @@ class Interpreter():
return self.evaluate_if(cur) return self.evaluate_if(cur)
elif isinstance(cur, mparser2.IdNode): elif isinstance(cur, mparser2.IdNode):
return self.get_variable(cur.value) return self.get_variable(cur.value)
elif isinstance(cur, mparser2.EqualNode): # FIXME, should be comparison elif isinstance(cur, mparser2.ComparisonNode):
return self.evaluate_comparison(cur) return self.evaluate_comparison(cur)
elif isinstance(cur, mparser2.ArrayNode): elif isinstance(cur, mparser2.ArrayNode):
return self.evaluate_arraystatement(cur) return self.evaluate_arraystatement(cur)
@ -1310,8 +1310,8 @@ class Interpreter():
return False return False
def evaluate_comparison(self, node): def evaluate_comparison(self, node):
v1 = self.evaluate_statement(node.get_first()) v1 = self.evaluate_statement(node.left)
v2 = self.evaluate_statement(node.get_second()) v2 = self.evaluate_statement(node.right)
if self.is_elementary_type(v1): if self.is_elementary_type(v1):
val1 = v1 val1 = v1
else: else:
@ -1323,9 +1323,9 @@ class Interpreter():
if type(val1) != type(val2): if type(val1) != type(val2):
raise InterpreterException('Comparison of different types %s and %s.' % raise InterpreterException('Comparison of different types %s and %s.' %
(str(type(val1)), str(type(val2)))) (str(type(val1)), str(type(val2))))
if node.get_ctype() == '==': if node.ctype == '==':
return val1 == val2 return val1 == val2
elif node.get_ctype() == '!=': elif node.ctype == '!=':
return val1 != val2 return val1 != val2
else: else:
raise InvalidCode('You broke me.') raise InvalidCode('You broke me.')

@ -169,19 +169,13 @@ class AndNode:
self.left = left self.left = left
self.right = right self.right = right
class EqualNode: class ComparisonNode:
def __init__(self, lineno, colno, left, right): def __init__(self, lineno, colno, ctype, left, right):
self.lineno = lineno
self.colno = colno
self.left = left
self.right = right
class NEqualNode:
def __init__(self, lineno, colno, left, right):
self.lineno = lineno self.lineno = lineno
self.colno = colno self.colno = colno
self.left = left self.left = left
self.right = right self.right = right
self.ctype = ctype
class NotNode: class NotNode:
def __init__(self, lineno, colno, value): def __init__(self, lineno, colno, value):
@ -337,9 +331,9 @@ class Parser:
def e4(self): def e4(self):
left = self.e5() left = self.e5()
if self.accept('equal'): if self.accept('equal'):
return EqualNode(left.lineno, left.colno, left, self.e5()) return ComparisonNode(left.lineno, left.colno, '==', left, self.e5())
if self.accept('nequal'): if self.accept('nequal'):
return NEqualNode(left.lineno, left.colno, left, self.e5()) return ComparisonNode(left.lineno, left.colno, '!=', left, self.e5())
return left return left
def e5(self): def e5(self):

Loading…
Cancel
Save