Mercurial > lcfOS
comparison python/ppci/assembler.py @ 375:19eacf4f7270
Started on memory manager
author | Windel Bouwman |
---|---|
date | Sun, 23 Mar 2014 15:44:06 +0100 |
parents | 3bb7dcfe5529 |
children | 6df89163e114 |
comparison
equal
deleted
inserted
replaced
374:72a3b646d543 | 375:19eacf4f7270 |
---|---|
95 return f(rhs) | 95 return f(rhs) |
96 self.g.add_production(prod, rhs, f_wrap) | 96 self.g.add_production(prod, rhs, f_wrap) |
97 | 97 |
98 def __init__(self, kws, instruction_rules, emit): | 98 def __init__(self, kws, instruction_rules, emit): |
99 # Construct a parser given a grammar: | 99 # Construct a parser given a grammar: |
100 tokens2 = ['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', | 100 tokens2 = ['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', '=', |
101 pyyacc.EPS, 'COMMENT', '{', '}', | 101 pyyacc.EPS, 'COMMENT', '{', '}', |
102 pyyacc.EOF, 'val32', 'val16', 'val12', 'val8', 'val5', 'val3'] | 102 pyyacc.EOF, 'val32', 'val16', 'val12', 'val8', 'val5', 'val3'] |
103 tokens2.extend(kws) | 103 tokens2.extend(kws) |
104 self.kws = kws | 104 self.kws = kws |
105 g = pyyacc.Grammar(tokens2) | 105 g = pyyacc.Grammar(tokens2) |
117 # Add instruction rules for the target in question: | 117 # Add instruction rules for the target in question: |
118 for prod, rhs, f in instruction_rules: | 118 for prod, rhs, f in instruction_rules: |
119 self.add_rule(prod, rhs, f) | 119 self.add_rule(prod, rhs, f) |
120 | 120 |
121 #g.add_production('instruction', []) | 121 #g.add_production('instruction', []) |
122 g.add_production('expression', ['term'], lambda x: x) | |
123 g.add_production('expression', ['expression', 'addop', 'term'], self.p_binop) | |
124 g.add_production('addop', ['-'], lambda x: x.val) | |
125 g.add_production('addop', ['+'], lambda x: x.val) | |
126 g.add_production('mulop', ['*'], lambda x: x.val) | |
127 g.add_production('term', ['factor'], lambda x: x) | |
128 g.add_production('term', ['term', 'mulop', 'factor'], self.p_binop) | |
129 g.add_production('factor', ['ID'], lambda name: ASymbol(name.val)) | |
130 g.add_production('factor', ['NUMBER'], lambda num: ANumber(int(num.val))) | |
131 g.start_symbol = 'asmline' | 122 g.start_symbol = 'asmline' |
132 self.emit = emit | 123 self.emit = emit |
133 self.p = g.generate_parser() | 124 self.p = g.generate_parser() |
134 # print('length of table:', len(self.p.action_table)) | 125 # print('length of table:', len(self.p.action_table)) |
135 | 126 |
136 # Parser handlers: | 127 # Parser handlers: |
137 def p_ins_1(self, opc, ops): | |
138 ins = AInstruction(opc, ops) | |
139 self.emit(ins) | |
140 | |
141 def p_ins_2(self, opc): | |
142 self.p_ins_1(opc, []) | |
143 | |
144 def p_operands_1(self, op1): | |
145 return [op1] | |
146 | |
147 def p_operands_2(self, ops, comma, op2): | |
148 assert type(ops) is list | |
149 ops.append(op2) | |
150 return ops | |
151 | |
152 def p_listitems_1(self, li1): | |
153 return [li1] | |
154 | |
155 def p_listitems_2(self, lis, comma, li2): | |
156 assert type(lis) is list | |
157 lis.append(li2) | |
158 return lis | |
159 | |
160 def p_list_op(self, brace_open, lst, brace_close): | |
161 return AUnop('{}', lst) | |
162 | |
163 def p_mem_op(self, brace_open, exp, brace_close): | |
164 return AUnop('[]', exp) | |
165 | 128 |
166 def p_label(self, lname, cn): | 129 def p_label(self, lname, cn): |
167 lab = Label(lname.val) | 130 lab = Label(lname.val) |
168 self.emit(lab) | 131 self.emit(lab) |
169 | |
170 def p_binop(self, exp1, op, exp2): | |
171 return ABinop(op, exp1, exp2) | |
172 | 132 |
173 def parse(self, lexer): | 133 def parse(self, lexer): |
174 self.p.parse(lexer) | 134 self.p.parse(lexer) |
175 | 135 |
176 | 136 |