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