diff python/libasm.py @ 194:b01429a5d695

Fixed test
author Windel Bouwman
date Wed, 29 May 2013 22:36:37 +0200
parents f091e7d70996
children 37ac6c016e0f
line wrap: on
line diff
--- a/python/libasm.py	Sun May 26 23:58:59 2013 +0200
+++ b/python/libasm.py	Wed May 29 22:36:37 2013 +0200
@@ -79,14 +79,19 @@
       return self.curTok
 
 class Assembler:
+    def handle_ins(self, id0):
+        self.ins = id0
+    def p_label(self, lname, cn):
+        self.label = lname
+
     def __init__(self):
         # Construct a parser given a grammar:
         g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', pyyacc.EPS])
 
         g.add_production('asmline', ['label', 'instruction', 'operands'])
-        g.add_production('label', ['ID', ':'])
-        g.add_production('label', [pyyacc.EPS])   # label is optional
-        g.add_production('instruction', ['ID'])
+        g.add_production('asmline', ['instruction', 'operands'])
+        g.add_production('label', ['ID', ':'], self.p_label)
+        g.add_production('instruction', ['ID'], self.handle_ins)
         g.add_production('operands', ['operand'])
         g.add_production('operands', ['operands', ',', 'operand'])
         g.add_production('operand', ['expression'])
@@ -102,22 +107,28 @@
         g.start_symbol = 'asmline'
 
         self.p = g.genParser()
+    def parse_line(self, line):
+        """ Parse line into asm AST """
+        tokens = tokenize(line)
+        self.p.parse(tokens)
+        aast = 1 # TODO
+        return aast
 
     def assemble(self, asmsrc):
-      lxr = Lexer(asmsrc)
-      prsr = Parser(lxr)
-      instructions = prsr.parse()
-      return instructions
+        lxr = Lexer(asmsrc)
+        prsr = Parser(lxr)
+        instructions = prsr.parse()
+        return instructions
 
     def assembleLine(self, line):
         """ 
             Assemble a single source line. 
             Do not take newlines into account 
         """
-        tokens = tokenize(line)
-        self.p.parse(tokens)
+        aast = self.parseLine(line)
+        self.assemble_aast(aast)
 
-    def assembleAst(self, at):
+    def assemble_aast(self, at):
         """ Assemble a parsed asm line """
         pass