diff python/ppci/assembler.py @ 381:6df89163e114

Fix section and ldr pseudo instruction
author Windel Bouwman
date Sat, 26 Apr 2014 17:41:56 +0200
parents 19eacf4f7270
children 0c44e494ef58
line wrap: on
line diff
--- a/python/ppci/assembler.py	Fri Apr 18 13:08:45 2014 +0200
+++ b/python/ppci/assembler.py	Sat Apr 26 17:41:56 2014 +0200
@@ -89,7 +89,8 @@
         if prod == 'instruction':
             def f_wrap(*args):
                 i = f(args)
-                self.emit(i)
+                if i:
+                    self.emit(i)
         else:
             def f_wrap(*rhs):
                 return f(rhs)
@@ -112,7 +113,6 @@
         g.add_production('asmline2', ['label'])
         g.add_production('asmline2', [])
         g.add_production('label', ['ID', ':'], self.p_label)
-        #g.add_production('label', [])
 
         # Add instruction rules for the target in question:
         for prod, rhs, f in instruction_rules:
@@ -134,11 +134,14 @@
         self.p.parse(lexer)
 
 
-class Assembler:
+class BaseAssembler:
+    """ Assembler base class, inherited by assemblers specific for a target """
     def __init__(self, target):
         self.target = target
         assert isinstance(target, Target)
-        self.parser = Parser(target.asm_keywords, target.assembler_rules, self.emit)
+
+    def make_parser(self):
+        self.parser = Parser(self.target.asm_keywords, self.target.assembler_rules, self.emit)
 
     def emit(self, *args):
         self.stream.emit(*args)
@@ -151,7 +154,9 @@
 
     def assemble(self, asmsrc, stream):
         """ Assemble this source snippet """
-        if hasattr(asmsrc, 'read'):
+        if type(asmsrc) is str:
+            pass
+        elif hasattr(asmsrc, 'read'):
             asmsrc2 = asmsrc.read()
             asmsrc.close()
             asmsrc = asmsrc2
@@ -160,5 +165,6 @@
         self.stream = stream
         for line in asmsrc.split('\n'):
             self.parse_line(line)
-        self.stream = None
 
+    def flush(self):
+        pass