200
|
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
|
225
|
14
|
200
|
15 class AInstruction(ANode):
|
203
|
16 def __init__(self, mnemonic, operands):
|
|
17 self.mnemonic = mnemonic
|
200
|
18 self.operands = operands
|
|
19 def __repr__(self):
|
|
20 ops = ', '.join(map(str, self.operands))
|
203
|
21 return '{0} {1}'.format(self.mnemonic, ops)
|
200
|
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):
|
206
|
40 def __init__(self, operation, arg):
|
|
41 self.operation = operation
|
200
|
42 self.arg = arg
|
|
43 def __repr__(self):
|
206
|
44 return '{0} {1}'.format(self.operation, self.arg)
|
200
|
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
|
201
|
56 self.number = n
|
200
|
57 def __repr__(self):
|
|
58 return '{0}'.format(self.n)
|
|
59
|