Permalink
Browse files

fixed multiple assignment, added tests for it

  • Loading branch information...
1 parent 34c5739 commit 6930d39d156fa8267940d53bcc2b88bf5f8cf94d @alex committed Oct 13, 2008
Showing with 18 additions and 27 deletions.
  1. +10 −24 alexs_lang/ast.py
  2. +3 −3 alexs_lang/parser.py
  3. +3 −0 tests/arithmatic.al
  4. +2 −0 tests/arithmatic.out
View
@@ -29,6 +29,14 @@ class BinaryOperation(Expression):
'-': operator.sub,
'*': operator.mul,
'/': operator.div,
+ '==': operator.eq,
+ '!=': operator.ne,
+ '>': operator.gt,
+ '<': operator.lt,
+ '>=': operator.ge,
+ '<=': operator.le,
+ 'and': operator.and_,
+ 'or': operator.or_,
}
def __init__(self, left, right, op):
self.left = left
@@ -84,28 +92,6 @@ def __str__(self):
def calculate(self, context):
return None
-class Comparison(Expression):
- OPS = {
- '==': operator.eq,
- '!=': operator.ne,
- '>': operator.gt,
- '<': operator.lt,
- '>=': operator.ge,
- '<=': operator.le,
- 'and': operator.and_,
- 'or': operator.or_,
- }
- def __init__(self, left, right, op):
- self.left = left
- self.right = right
- self.op = comp
-
- def __str__(self):
- return "<Comparison: %s %s %s>" % (self.left, self.op, self.right)
-
- def calculate(self, context):
- return self.OPS[self.op](self.left.calculate(context), self.right.calculate(context))
-
class Assignment(Expression):
def __init__(self, left, right):
self.left = left
@@ -116,8 +102,8 @@ def __str__(self):
def calculate(self, context):
val = self.right.calculate(context)
- context[self.left.name] = val
- return val
+ for var in self.left:
+ context[var.name] = val
class Name(Expression):
def __init__(self, name):
@@ -58,15 +58,15 @@ def p_assignment_statement(self, t):
| NAME DIVIDE_EQUALS expression
'''
if t[2] == '=':
- t[0] = ast.Assignment(ast.Name(t[1]), t[3])
+ t[0] = ast.Assignment([ast.Name(t[1])], t[3])
else:
- t[0] = ast.Assignment(ast.Name(t[1]), ast.BinaryOperation(ast.Name(t[1]), t[3], t[2][0]))
+ t[0] = ast.Assignment([ast.Name(t[1])], ast.BinaryOperation(ast.Name(t[1]), t[3], t[2][0]))
def p_assignment_statement_multi(self, t):
'''
assignment_statement : NAME EQUALS assignment_statement
'''
- t[0] = ast.Assignment(ast.Name(t[1]), t[3].right)
+ t[0] = ast.Assignment([ast.Name(t[1])]+t[3].left, t[3].right)
def p_expression_binary(self, t):
'''
View
@@ -1,2 +1,5 @@
print(3+4)
print(2)
+a = b = 3
+print(a)
+print(a+b)
@@ -1,2 +1,4 @@
7
2
+3
+6

0 comments on commit 6930d39

Please sign in to comment.