Mercurial > lcfOS
diff 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 |
line wrap: on
line diff
--- a/python/asm.py Fri Jul 05 14:13:59 2013 +0200 +++ b/python/asm.py Fri Jul 05 15:30:22 2013 +0200 @@ -66,11 +66,8 @@ def Peak(self): return self.curTok - -class Assembler: - def __init__(self, target=None): - self.target = target - self.restart() +class Parser: + def __init__(self): # Construct a parser given a grammar: ident = lambda x: x # Identity helper function g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS, 'COMMENT', '{', '}']) @@ -136,6 +133,17 @@ def p_binop(self, exp1, op, exp2): return ABinop(op, exp1, exp2) + def parse(self, tokens, emitter): + self.emit = emitter + self.p.parse(tokens) + +asmParser = Parser() +class Assembler: + def __init__(self, target=None): + self.target = target + self.restart() + self.p = asmParser + # Top level interface: def restart(self): self.output = [] @@ -151,7 +159,7 @@ def parse_line(self, line): """ Parse line into asm AST """ tokens = tokenize(line) - self.p.parse(tokens) + self.p.parse(tokens, self.emit) def assemble(self, asmsrc): """ Assemble this source snippet """