Mercurial > lcfOS
comparison python/asm.py @ 198:33d50727a23c
Fixup testscript
author | Windel Bouwman |
---|---|
date | Sat, 01 Jun 2013 13:11:22 +0200 |
parents | 4a1ca1271241 |
children | a690473b79e2 |
comparison
equal
deleted
inserted
replaced
197:4a1ca1271241 | 198:33d50727a23c |
---|---|
1 import re | 1 import re, sys, argparse |
2 import pyyacc | 2 import pyyacc |
3 from ppci import Token, CompilerError, SourceLocation | 3 from ppci import Token, CompilerError, SourceLocation |
4 import sys, argparse | 4 |
5 | |
6 | |
7 # Different instruction sets: | |
8 class InstructionSet: | |
9 pass | |
10 | |
11 class X86(InstructionSet): | |
12 pass | |
13 | 5 |
14 # Generic assembler: | 6 # Generic assembler: |
15 keywords = ['global', 'db'] | 7 keywords = ['global', 'db'] |
16 | 8 |
17 def tokenize(s): | 9 def tokenize(s): |
26 ('HEXNUMBER', r'0x[\da-fA-F]+'), | 18 ('HEXNUMBER', r'0x[\da-fA-F]+'), |
27 ('NUMBER', r'\d+'), | 19 ('NUMBER', r'\d+'), |
28 ('ID', r'[A-Za-z][A-Za-z\d_]*'), | 20 ('ID', r'[A-Za-z][A-Za-z\d_]*'), |
29 ('SKIP', r'[ \t]'), | 21 ('SKIP', r'[ \t]'), |
30 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<'), | 22 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<'), |
31 ('STRING', r"'.*?'") | 23 ('STRING', r"'.*?'"), |
24 ('COMMENT', r";.*") | |
32 ] | 25 ] |
33 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec) | 26 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec) |
34 gettok = re.compile(tok_re).match | 27 gettok = re.compile(tok_re).match |
35 line = 1 | 28 line = 1 |
36 pos = line_start = 0 | 29 pos = line_start = 0 |
136 class Assembler: | 129 class Assembler: |
137 def __init__(self): | 130 def __init__(self): |
138 self.output = [] | 131 self.output = [] |
139 # Construct a parser given a grammar: | 132 # Construct a parser given a grammar: |
140 ident = lambda x: x # Identity helper function | 133 ident = lambda x: x # Identity helper function |
141 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS]) | 134 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*', pyyacc.EPS, 'COMMENT']) |
142 g.add_production('asmline', ['label', 'instruction']) | 135 g.add_production('asmline', ['asmline2']) |
143 g.add_production('asmline', ['instruction']) | 136 g.add_production('asmline', ['asmline2', 'COMMENT']) |
144 g.add_production('asmline', ['label']) | 137 g.add_production('asmline2', ['label', 'instruction']) |
145 g.add_production('asmline', []) | 138 g.add_production('asmline2', ['instruction']) |
139 g.add_production('asmline2', ['label']) | |
140 g.add_production('asmline2', []) | |
141 g.add_production('optcomment', []) | |
142 g.add_production('optcomment', ['COMMENT']) | |
146 g.add_production('label', ['ID', ':'], self.p_label) | 143 g.add_production('label', ['ID', ':'], self.p_label) |
147 g.add_production('instruction', ['opcode', 'operands'], self.p_ins_1) | 144 g.add_production('instruction', ['opcode', 'operands'], self.p_ins_1) |
148 g.add_production('instruction', ['opcode'], self.p_ins_2) | 145 g.add_production('instruction', ['opcode'], self.p_ins_2) |
149 g.add_production('opcode', ['ID'], ident) | 146 g.add_production('opcode', ['ID'], ident) |
150 g.add_production('operands', ['operand'], self.p_operands_1) | 147 g.add_production('operands', ['operand'], self.p_operands_1) |
216 Do not take newlines into account | 213 Do not take newlines into account |
217 """ | 214 """ |
218 self.parse_line(line) | 215 self.parse_line(line) |
219 self.assemble_aast() | 216 self.assemble_aast() |
220 | 217 |
221 def assemble_aast(self, at): | 218 def assemble_aast(self): |
222 """ Assemble a parsed asm line """ | 219 """ Assemble a parsed asm line """ |
223 pass | 220 pass |
224 | 221 |
225 def back_patch(self): | 222 def back_patch(self): |
226 """ Fix references to earlier labels """ | 223 """ Fix references to earlier labels """ |