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
|
|
14 class AInstruction(ANode):
|
203
|
15 def __init__(self, mnemonic, operands):
|
|
16 self.mnemonic = mnemonic
|
200
|
17 self.operands = operands
|
|
18 def __repr__(self):
|
|
19 ops = ', '.join(map(str, self.operands))
|
203
|
20 return '{0} {1}'.format(self.mnemonic, ops)
|
200
|
21
|
|
22 class AExpression(ANode):
|
|
23 def __add__(self, other):
|
|
24 assert isinstance(other, AExpression)
|
|
25 return ABinop('+', self, other)
|
|
26 def __mul__(self, other):
|
|
27 assert isinstance(other, AExpression)
|
|
28 return ABinop('*', self, other)
|
|
29
|
|
30 class ABinop(AExpression):
|
|
31 def __init__(self, op, arg1, arg2):
|
|
32 self.op = op
|
|
33 self.arg1 = arg1
|
|
34 self.arg2 = arg2
|
|
35 def __repr__(self):
|
|
36 return '{0} {1} {2}'.format(self.op, self.arg1, self.arg2)
|
|
37
|
|
38 class AUnop(AExpression):
|
206
|
39 def __init__(self, operation, arg):
|
|
40 self.operation = operation
|
200
|
41 self.arg = arg
|
|
42 def __repr__(self):
|
206
|
43 return '{0} {1}'.format(self.operation, self.arg)
|
200
|
44
|
|
45 class ASymbol(AExpression):
|
|
46 def __init__(self, name):
|
|
47 self.name = name
|
|
48 def __repr__(self):
|
|
49 return self.name
|
|
50
|
|
51 class ANumber(AExpression):
|
|
52 def __init__(self, n):
|
|
53 assert type(n) is int
|
|
54 self.n = n
|
201
|
55 self.number = n
|
200
|
56 def __repr__(self):
|
|
57 return '{0}'.format(self.n)
|
|
58
|