Browse Source

Added possibility to add an inline comment message to a statement replacement.

Taddeus Kroes 14 năm trước cách đây
mục cha
commit
50c79cdfc5
3 tập tin đã thay đổi với 34 bổ sung4 xóa
  1. 2 0
      main.py
  2. 4 1
      src/program.py
  3. 28 3
      src/statement.py

+ 2 - 0
main.py

@@ -2,6 +2,7 @@
 from src.parser import parse_file
 from src.optimize import optimize
 
+
 if __name__ == '__main__':
     from sys import argv, exit
 
@@ -12,6 +13,7 @@ if __name__ == '__main__':
 
     # Parse file
     program = parse_file(argv[1])
+    program.debug = True
 
     if len(argv) > 3:
         # Save input assembly in new file for easy comparison

+ 4 - 1
src/program.py

@@ -71,7 +71,10 @@ class Program(Block):
         """Divide the statement list into basic blocks."""
         self.blocks = find_basic_blocks(self.statements)
 
-        # Remove the old statment list, since it will probably change
+        for b in self.blocks:
+            b.debug = self.debug
+
+        # Remove the old statement list, since it will probably change
         del self.statements
 
     def perform_dataflow_analysis(self):

+ 28 - 3
src/statement.py

@@ -38,6 +38,9 @@ class Statement:
     def __repr__(self):  # pragma: nocover
         return str(self)
 
+    def set_inline_comment(self, comment):
+        self.options['comment'] = comment
+
     def has_inline_comment(self):
         return 'comment' in self.options and len(self.options['comment'])
 
@@ -221,7 +224,7 @@ class Statement:
 class Block:
     bid = 1
 
-    def __init__(self, statements=[]):
+    def __init__(self, statements=[], debug=False):
         self.statements = statements
         self.pointer = 0
 
@@ -229,6 +232,8 @@ class Block:
         self.bid = Block.bid
         Block.bid += 1
 
+        self.debug = debug
+
     def __str__(self):
         return '<Block bid=%d statements=%d>' % (self.bid, len(self))
 
@@ -265,7 +270,7 @@ class Block:
         return self.statements[self.pointer] if count == 1 \
                else self.statements[self.pointer:self.pointer + count]
 
-    def replace(self, count, replacement, start=None):
+    def replace(self, count, replacement, start=None, message=''):
         """Replace the given range start-(start + count) with the given
         statement list, and move the pointer to the first statement after the
         replacement."""
@@ -275,15 +280,35 @@ class Block:
         if start == None:
             start = self.pointer - 1
 
+        # Add a message in inline comments
+        if self.debug:
+            if len(message):
+                message = ' ' + message
+
+                if len(replacement):
+                    replacement[0].set_inline_comment(message)
+
+                    for s in replacement[1:]:
+                        s.set_inline_comment('|')
+                else:
+                    replacement = [Statement('comment', message)]
+            elif not len(replacement):
+                # Statement is removed, comment it
+                replacement = [Statement('comment', str(b)) \
+                               for b in self.statements[start:start + count]]
+
         before = self.statements[:start]
         after = self.statements[start + count:]
         self.statements = before + replacement + after
         self.pointer = start + len(replacement)
 
-    def insert(self, statement, index=None):
+    def insert(self, statement, index=None, message=''):
         if index == None:
             index = self.pointer
 
+        if self.debug and len(message):
+            statement.set_inline_comment(' ' + message)
+
         self.statements.insert(index, statement)
 
     def apply_filter(self, callback):