Mercurial > lcfOS
changeset 193:f091e7d70996
Added even more checks
author | Windel Bouwman |
---|---|
date | Sun, 26 May 2013 23:58:59 +0200 |
parents | 6cd6260789a1 |
children | b01429a5d695 |
files | python/libasm.py python/testasm.py |
diffstat | 2 files changed, 22 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/python/libasm.py Sun May 26 23:19:27 2013 +0200 +++ b/python/libasm.py Sun May 26 23:58:59 2013 +0200 @@ -81,16 +81,23 @@ class Assembler: def __init__(self): # Construct a parser given a grammar: - g = pyyacc.Grammar(['ID', ',', '[', ']', ':']) + g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', pyyacc.EPS]) g.add_production('asmline', ['label', 'instruction', 'operands']) g.add_production('label', ['ID', ':']) - g.add_production('label', ['EPS']) + g.add_production('label', [pyyacc.EPS]) # label is optional g.add_production('instruction', ['ID']) g.add_production('operands', ['operand']) g.add_production('operands', ['operands', ',', 'operand']) g.add_production('operand', ['expression']) - g.add_production('expression', ['ID']) + g.add_production('operand', ['[', 'expression', ']']) + g.add_production('expression', ['term']) + g.add_production('expression', ['expression', 'addop', 'term']) + g.add_production('addop', ['-']) + g.add_production('addop', ['+']) + g.add_production('term', ['factor']) + g.add_production('factor', ['ID']) + g.add_production('factor', ['NUMBER']) # TODO: expand grammar g.start_symbol = 'asmline'
--- a/python/testasm.py Sun May 26 23:19:27 2013 +0200 +++ b/python/testasm.py Sun May 26 23:58:59 2013 +0200 @@ -17,10 +17,17 @@ self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks) def testLex1(self): + """ Test if lexer correctly maps some tokens """ asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID'] self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks) + def testLex1(self): + """ Test if lexer correctly maps some tokens """ + asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER'] + self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks) + def testLex2(self): + """ Test if lexer fails on a token that is invalid """ asmline = '0z4: mov rax, rbx $ ' with self.assertRaises(ppci.CompilerError): list(libasm.tokenize(asmline)) @@ -30,6 +37,11 @@ a = libasm.Assembler() a.assembleLine(asmline) + def testParse2(self): + asmline = 'a: mov rax, [rbx + 2]' + a = libasm.Assembler() + a.assembleLine(asmline) + if __name__ == '__main__': unittest.main()