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):
|
|
15 def __init__(self, opcode, operands):
|
|
16 self.opcode = opcode
|
|
17 self.operands = operands
|
|
18 def __repr__(self):
|
|
19 ops = ', '.join(map(str, self.operands))
|
|
20 return '{0} {1}'.format(self.opcode, ops)
|
|
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):
|
|
39 def __init__(self, op, arg):
|
|
40 self.op = op
|
|
41 self.arg = arg
|
|
42 def __repr__(self):
|
|
43 return '{0} {1}'.format(self.op, self.arg)
|
|
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
|
|
55 def __repr__(self):
|
|
56 return '{0}'.format(self.n)
|
|
57
|