comparison python/ppci/asmnodes.py @ 334:6f4753202b9a

Added more recipes
author Windel Bouwman
date Thu, 13 Feb 2014 22:02:08 +0100
parents python/asmnodes.py@83781bd10fdb
children
comparison
equal deleted inserted replaced
333:dcae6574c974 334:6f4753202b9a
1
2 """ Assembler AST nodes """
3
4 class ANode:
5 def __eq__(self, other):
6 return self.__repr__() == other.__repr__()
7
8 class ALabel(ANode):
9 def __init__(self, name):
10 self.name = name
11 def __repr__(self):
12 return '{0}:'.format(self.name)
13
14
15 class AInstruction(ANode):
16 def __init__(self, mnemonic, operands):
17 self.mnemonic = mnemonic
18 self.operands = operands
19 def __repr__(self):
20 ops = ', '.join(map(str, self.operands))
21 return '{0} {1}'.format(self.mnemonic, ops)
22
23 class AExpression(ANode):
24 def __add__(self, other):
25 assert isinstance(other, AExpression)
26 return ABinop('+', self, other)
27 def __mul__(self, other):
28 assert isinstance(other, AExpression)
29 return ABinop('*', self, other)
30
31 class ABinop(AExpression):
32 def __init__(self, op, arg1, arg2):
33 self.op = op
34 self.arg1 = arg1
35 self.arg2 = arg2
36 def __repr__(self):
37 return '{0} {1} {2}'.format(self.op, self.arg1, self.arg2)
38
39 class AUnop(AExpression):
40 def __init__(self, operation, arg):
41 self.operation = operation
42 self.arg = arg
43 def __repr__(self):
44 return '{0} {1}'.format(self.operation, self.arg)
45
46 class ASymbol(AExpression):
47 def __init__(self, name):
48 self.name = name
49 def __repr__(self):
50 return self.name
51
52 class ANumber(AExpression):
53 def __init__(self, n):
54 assert type(n) is int
55 self.n = n
56 self.number = n
57 def __repr__(self):
58 return '{0}'.format(self.n)
59