comparison python/asm.py @ 206:6c6bf8890d8a

Added push and pop encodings
author Windel Bouwman
date Fri, 28 Jun 2013 16:49:38 +0200
parents ca1ea402f6a1
children 003c8a976fff
comparison
equal deleted inserted replaced
205:d77cb5962cc5 206:6c6bf8890d8a
15 ('REAL', r'\d+\.\d+'), 15 ('REAL', r'\d+\.\d+'),
16 ('HEXNUMBER', r'0x[\da-fA-F]+'), 16 ('HEXNUMBER', r'0x[\da-fA-F]+'),
17 ('NUMBER', r'\d+'), 17 ('NUMBER', r'\d+'),
18 ('ID', r'[A-Za-z][A-Za-z\d_]*'), 18 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
19 ('SKIP', r'[ \t]'), 19 ('SKIP', r'[ \t]'),
20 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<'), 20 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<|}|{'),
21 ('STRING', r"'.*?'"), 21 ('STRING', r"'.*?'"),
22 ('COMMENT', r";.*") 22 ('COMMENT', r";.*")
23 ] 23 ]
24 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec) 24 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
25 gettok = re.compile(tok_re).match 25 gettok = re.compile(tok_re).match
71 def __init__(self, target=None): 71 def __init__(self, target=None):
72 self.target = target 72 self.target = target
73 self.restart() 73 self.restart()
74 # Construct a parser given a grammar: 74 # Construct a parser given a grammar:
75 ident = lambda x: x # Identity helper function 75 ident = lambda x: x # Identity helper function
76 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS, 'COMMENT']) 76 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS, 'COMMENT', '{', '}'])
77 g.add_production('asmline', ['asmline2']) 77 g.add_production('asmline', ['asmline2'])
78 g.add_production('asmline', ['asmline2', 'COMMENT']) 78 g.add_production('asmline', ['asmline2', 'COMMENT'])
79 g.add_production('asmline2', ['label', 'instruction']) 79 g.add_production('asmline2', ['label', 'instruction'])
80 g.add_production('asmline2', ['instruction']) 80 g.add_production('asmline2', ['instruction'])
81 g.add_production('asmline2', ['label']) 81 g.add_production('asmline2', ['label'])
88 g.add_production('opcode', ['ID'], ident) 88 g.add_production('opcode', ['ID'], ident)
89 g.add_production('operands', ['operand'], self.p_operands_1) 89 g.add_production('operands', ['operand'], self.p_operands_1)
90 g.add_production('operands', ['operands', ',', 'operand'], self.p_operands_2) 90 g.add_production('operands', ['operands', ',', 'operand'], self.p_operands_2)
91 g.add_production('operand', ['expression'], ident) 91 g.add_production('operand', ['expression'], ident)
92 g.add_production('operand', ['[', 'expression', ']'], self.p_mem_op) 92 g.add_production('operand', ['[', 'expression', ']'], self.p_mem_op)
93 g.add_production('operand', ['{', 'listitems', '}'], self.p_list_op)
94 g.add_production('listitems', ['expression'], self.p_listitems_1)
95 g.add_production('listitems', ['listitems', ',', 'expression'], self.p_listitems_2)
93 g.add_production('expression', ['term'], ident) 96 g.add_production('expression', ['term'], ident)
94 g.add_production('expression', ['expression', 'addop', 'term'], self.p_binop) 97 g.add_production('expression', ['expression', 'addop', 'term'], self.p_binop)
95 g.add_production('addop', ['-'], ident) 98 g.add_production('addop', ['-'], ident)
96 g.add_production('addop', ['+'], ident) 99 g.add_production('addop', ['+'], ident)
97 g.add_production('mulop', ['*'], ident) 100 g.add_production('mulop', ['*'], ident)
112 return [op1] 115 return [op1]
113 def p_operands_2(self, ops, comma, op2): 116 def p_operands_2(self, ops, comma, op2):
114 assert type(ops) is list 117 assert type(ops) is list
115 ops.append(op2) 118 ops.append(op2)
116 return ops 119 return ops
120
121 def p_listitems_1(self, li1):
122 return [li1]
123
124 def p_listitems_2(self, lis, comma, li2):
125 assert type(lis) is list
126 lis.append(li2)
127 return lis
128
129 def p_list_op(self, brace_open, lst, brace_close):
130 return AUnop('{}', lst)
117 def p_mem_op(self, brace_open, exp, brace_close): 131 def p_mem_op(self, brace_open, exp, brace_close):
118 return AUnop('[]', exp) 132 return AUnop('[]', exp)
119 def p_label(self, lname, cn): 133 def p_label(self, lname, cn):
120 lab = ALabel(lname) 134 lab = ALabel(lname)
121 self.emit(lab) 135 self.emit(lab)