Mercurial > lcfOS
comparison python/asm.py @ 218:494828a7adf1
added some sort of cache to assembler
author | Windel Bouwman |
---|---|
date | Fri, 05 Jul 2013 15:30:22 +0200 |
parents | 003c8a976fff |
children | 1fa3e0050b49 |
comparison
equal
deleted
inserted
replaced
217:8b2e5f3cd579 | 218:494828a7adf1 |
---|---|
64 return t | 64 return t |
65 @property | 65 @property |
66 def Peak(self): | 66 def Peak(self): |
67 return self.curTok | 67 return self.curTok |
68 | 68 |
69 | 69 class Parser: |
70 class Assembler: | 70 def __init__(self): |
71 def __init__(self, target=None): | |
72 self.target = target | |
73 self.restart() | |
74 # Construct a parser given a grammar: | 71 # Construct a parser given a grammar: |
75 ident = lambda x: x # Identity helper function | 72 ident = lambda x: x # Identity helper function |
76 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS, 'COMMENT', '{', '}']) | 73 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS, 'COMMENT', '{', '}']) |
77 g.add_production('asmline', ['asmline2']) | 74 g.add_production('asmline', ['asmline2']) |
78 g.add_production('asmline', ['asmline2', 'COMMENT']) | 75 g.add_production('asmline', ['asmline2', 'COMMENT']) |
134 lab = ALabel(lname) | 131 lab = ALabel(lname) |
135 self.emit(lab) | 132 self.emit(lab) |
136 def p_binop(self, exp1, op, exp2): | 133 def p_binop(self, exp1, op, exp2): |
137 return ABinop(op, exp1, exp2) | 134 return ABinop(op, exp1, exp2) |
138 | 135 |
136 def parse(self, tokens, emitter): | |
137 self.emit = emitter | |
138 self.p.parse(tokens) | |
139 | |
140 asmParser = Parser() | |
141 class Assembler: | |
142 def __init__(self, target=None): | |
143 self.target = target | |
144 self.restart() | |
145 self.p = asmParser | |
146 | |
139 # Top level interface: | 147 # Top level interface: |
140 def restart(self): | 148 def restart(self): |
141 self.output = [] | 149 self.output = [] |
142 self.binout = bytearray() | 150 self.binout = bytearray() |
143 self.current_section = '.text' | 151 self.current_section = '.text' |
149 # TODO | 157 # TODO |
150 | 158 |
151 def parse_line(self, line): | 159 def parse_line(self, line): |
152 """ Parse line into asm AST """ | 160 """ Parse line into asm AST """ |
153 tokens = tokenize(line) | 161 tokens = tokenize(line) |
154 self.p.parse(tokens) | 162 self.p.parse(tokens, self.emit) |
155 | 163 |
156 def assemble(self, asmsrc): | 164 def assemble(self, asmsrc): |
157 """ Assemble this source snippet """ | 165 """ Assemble this source snippet """ |
158 for line in asmsrc.split('\n'): | 166 for line in asmsrc.split('\n'): |
159 self.assemble_line(line) | 167 self.assemble_line(line) |