Mercurial > lcfOS
comparison python/libasm.py @ 193:f091e7d70996
Added even more checks
author | Windel Bouwman |
---|---|
date | Sun, 26 May 2013 23:58:59 +0200 |
parents | 6b2bec5653f1 |
children | b01429a5d695 |
comparison
equal
deleted
inserted
replaced
192:6cd6260789a1 | 193:f091e7d70996 |
---|---|
79 return self.curTok | 79 return self.curTok |
80 | 80 |
81 class Assembler: | 81 class Assembler: |
82 def __init__(self): | 82 def __init__(self): |
83 # Construct a parser given a grammar: | 83 # Construct a parser given a grammar: |
84 g = pyyacc.Grammar(['ID', ',', '[', ']', ':']) | 84 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', pyyacc.EPS]) |
85 | 85 |
86 g.add_production('asmline', ['label', 'instruction', 'operands']) | 86 g.add_production('asmline', ['label', 'instruction', 'operands']) |
87 g.add_production('label', ['ID', ':']) | 87 g.add_production('label', ['ID', ':']) |
88 g.add_production('label', ['EPS']) | 88 g.add_production('label', [pyyacc.EPS]) # label is optional |
89 g.add_production('instruction', ['ID']) | 89 g.add_production('instruction', ['ID']) |
90 g.add_production('operands', ['operand']) | 90 g.add_production('operands', ['operand']) |
91 g.add_production('operands', ['operands', ',', 'operand']) | 91 g.add_production('operands', ['operands', ',', 'operand']) |
92 g.add_production('operand', ['expression']) | 92 g.add_production('operand', ['expression']) |
93 g.add_production('expression', ['ID']) | 93 g.add_production('operand', ['[', 'expression', ']']) |
94 g.add_production('expression', ['term']) | |
95 g.add_production('expression', ['expression', 'addop', 'term']) | |
96 g.add_production('addop', ['-']) | |
97 g.add_production('addop', ['+']) | |
98 g.add_production('term', ['factor']) | |
99 g.add_production('factor', ['ID']) | |
100 g.add_production('factor', ['NUMBER']) | |
94 # TODO: expand grammar | 101 # TODO: expand grammar |
95 g.start_symbol = 'asmline' | 102 g.start_symbol = 'asmline' |
96 | 103 |
97 self.p = g.genParser() | 104 self.p = g.genParser() |
98 | 105 |