191
|
1 import re
|
|
2 import pyyacc
|
|
3 from ppci import Token, CompilerError, SourceLocation
|
159
|
4
|
|
5 # Different instruction sets:
|
|
6 class InstructionSet:
|
|
7 pass
|
|
8
|
|
9 class X86(InstructionSet):
|
|
10 pass
|
|
11
|
|
12 # Generic assembler:
|
|
13 keywords = ['global', 'db']
|
|
14
|
|
15 def tokenize(s):
|
|
16 """
|
|
17 Tokenizer, generates an iterator that
|
|
18 returns tokens!
|
|
19
|
|
20 This GREAT example was taken from python re doc page!
|
|
21 """
|
|
22 tok_spec = [
|
|
23 ('REAL', r'\d+\.\d+'),
|
|
24 ('HEXNUMBER', r'0x[\da-fA-F]+'),
|
|
25 ('NUMBER', r'\d+'),
|
|
26 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
|
|
27 ('SKIP', r'[ \t]'),
|
191
|
28 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<'),
|
159
|
29 ('STRING', r"'.*?'")
|
|
30 ]
|
|
31 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
|
|
32 gettok = re.compile(tok_re).match
|
|
33 line = 1
|
|
34 pos = line_start = 0
|
|
35 mo = gettok(s)
|
|
36 while mo is not None:
|
|
37 typ = mo.lastgroup
|
|
38 val = mo.group(typ)
|
|
39 if typ == 'NEWLINE':
|
|
40 line_start = pos
|
|
41 line += 1
|
|
42 elif typ == 'COMMENTS':
|
|
43 pass
|
|
44 elif typ != 'SKIP':
|
|
45 if typ == 'ID':
|
|
46 if val in keywords:
|
|
47 typ = val
|
|
48 elif typ == 'LEESTEKEN':
|
|
49 typ = val
|
|
50 elif typ == 'NUMBER':
|
|
51 val = int(val)
|
|
52 elif typ == 'HEXNUMBER':
|
|
53 val = int(val[2:], 16)
|
|
54 typ = 'NUMBER'
|
|
55 elif typ == 'REAL':
|
|
56 val = float(val)
|
|
57 elif typ == 'STRING':
|
|
58 val = val[1:-1]
|
191
|
59 col = mo.start() - line_start
|
|
60 loc = SourceLocation(line, col, 0) # TODO retrieve length?
|
|
61 yield Token(typ, val, loc)
|
159
|
62 pos = mo.end()
|
|
63 mo = gettok(s, pos)
|
|
64 if pos != len(s):
|
|
65 col = pos - line_start
|
191
|
66 loc = SourceLocation(line, col, 0)
|
|
67 raise CompilerError('Unexpected character {0}'.format(s[pos]), loc)
|
159
|
68
|
|
69 class Lexer:
|
|
70 def __init__(self, src):
|
|
71 self.tokens = tokenize(src)
|
|
72 self.curTok = self.tokens.__next__()
|
|
73 def eat(self):
|
|
74 t = self.curTok
|
|
75 self.curTok = self.tokens.__next__()
|
|
76 return t
|
|
77 @property
|
|
78 def Peak(self):
|
|
79 return self.curTok
|
|
80
|
191
|
81 class Assembler:
|
194
|
82 def handle_ins(self, id0):
|
|
83 self.ins = id0
|
|
84 def p_label(self, lname, cn):
|
|
85 self.label = lname
|
|
86
|
191
|
87 def __init__(self):
|
|
88 # Construct a parser given a grammar:
|
193
|
89 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', pyyacc.EPS])
|
159
|
90
|
191
|
91 g.add_production('asmline', ['label', 'instruction', 'operands'])
|
194
|
92 g.add_production('asmline', ['instruction', 'operands'])
|
|
93 g.add_production('label', ['ID', ':'], self.p_label)
|
|
94 g.add_production('instruction', ['ID'], self.handle_ins)
|
191
|
95 g.add_production('operands', ['operand'])
|
|
96 g.add_production('operands', ['operands', ',', 'operand'])
|
|
97 g.add_production('operand', ['expression'])
|
193
|
98 g.add_production('operand', ['[', 'expression', ']'])
|
|
99 g.add_production('expression', ['term'])
|
|
100 g.add_production('expression', ['expression', 'addop', 'term'])
|
|
101 g.add_production('addop', ['-'])
|
|
102 g.add_production('addop', ['+'])
|
|
103 g.add_production('term', ['factor'])
|
|
104 g.add_production('factor', ['ID'])
|
|
105 g.add_production('factor', ['NUMBER'])
|
191
|
106 # TODO: expand grammar
|
|
107 g.start_symbol = 'asmline'
|
159
|
108
|
191
|
109 self.p = g.genParser()
|
194
|
110 def parse_line(self, line):
|
|
111 """ Parse line into asm AST """
|
|
112 tokens = tokenize(line)
|
|
113 self.p.parse(tokens)
|
|
114 aast = 1 # TODO
|
|
115 return aast
|
191
|
116
|
|
117 def assemble(self, asmsrc):
|
194
|
118 lxr = Lexer(asmsrc)
|
|
119 prsr = Parser(lxr)
|
|
120 instructions = prsr.parse()
|
|
121 return instructions
|
159
|
122
|
191
|
123 def assembleLine(self, line):
|
|
124 """
|
|
125 Assemble a single source line.
|
|
126 Do not take newlines into account
|
|
127 """
|
194
|
128 aast = self.parseLine(line)
|
|
129 self.assemble_aast(aast)
|
191
|
130
|
194
|
131 def assemble_aast(self, at):
|
191
|
132 """ Assemble a parsed asm line """
|
|
133 pass
|
|
134
|
|
135
|