# HG changeset patch # User Windel Bouwman # Date 1369605539 -7200 # Node ID f091e7d70996f2577acbf5478335bfb5246aca38 # Parent 6cd6260789a1111574716fe5e1a9413b7f18e380 Added even more checks diff -r 6cd6260789a1 -r f091e7d70996 python/libasm.py --- 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' diff -r 6cd6260789a1 -r f091e7d70996 python/testasm.py --- 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()