ソースを参照

Propagate line information in compiler

Yentl Van Tendeloo 8 年 前
コミット
b5c47ddd14

BIN
bootstrap/bootstrap.m.gz


+ 2 - 2
interface/HUTN/hutn_compiler/hutnparser.py

@@ -29,13 +29,13 @@ from copy import deepcopy
 from position import Position
 
 class Tree(object):
-    def __init__(self, head, tail, startpos, endpos):
+    def __init__(self, head, tail, startpos, endpos, inputfile = None):
         self.head = head
         self.tail = tail
         self.startpos = startpos
         self.endpos = endpos
         self._tail = None
-        self.inputfile = None
+        self.inputfile = inputfile
         # IMPORTANT: self.replaced: replace_child defines self.replaced
 
     def is_rule(self):

+ 13 - 14
interface/HUTN/hutn_compiler/semantics_visitor.py

@@ -137,7 +137,7 @@ class SemanticsVisitor(Visitor):
                         tree.startpos['column']
                     ))
             call_name = SemanticsVisitor.call_name_binary(l_type, op)
-            call_tree = SemanticsVisitor.func_call(call_name, [l, r])
+            call_tree = SemanticsVisitor.func_call(call_name, [l, r], tree)
             try:
                 self.visit(call_tree)
             except RuntimeError:
@@ -162,7 +162,7 @@ class SemanticsVisitor(Visitor):
             op, l = child.get_tail()
             l_type = self.get_type(l)
             call_name = SemanticsVisitor.call_name_unary(l_type, op)
-            call_tree = SemanticsVisitor.func_call(call_name, [l])
+            call_tree = SemanticsVisitor.func_call(call_name, [l], tree)
             try:
                 self.visit(call_tree)
             except RuntimeError:
@@ -200,29 +200,28 @@ class SemanticsVisitor(Visitor):
         self.perform_implicit_cast(tree, l, l_type, p_type)
 
     @staticmethod
-    def func_call(name, params):
-        zero = {'line': 0, 'column': 0}
+    def func_call(name, params, old_tree):
+        startpos = old_tree.startpos
+        endpos = old_tree.endpos
+        inputfile = old_tree.inputfile
 
         tree = hp.Tree(
             "func_call",
             [
                 hp.Tree("rvalue",
                         [
-                            hp.Tree("ID", [name], zero, zero)
+                            hp.Tree("ID", [name], startpos, endpos, inputfile)
                         ],
-                        zero, zero),
+                        startpos, endpos, inputfile),
                 # Tokens have no impact on visit_func_call. So leave them out.
-                # hp.Tree("LPAREN", ["("], zero, zero),
-                # hp.Tree("COMMA", [","], zero, zero),
-                # hp.Tree("RPAREN", [")"], zero, zero),
             ],
-            zero, zero)
+            startpos, endpos, inputfile)
 
-        params = [hp.Tree("expression", [p], zero, zero) for p in params]
+        params = [hp.Tree("expression", [p], startpos, endpos, inputfile) for p in params]
 
         tree.tail.extend(params)
 
-        return hp.Tree("expression", [tree], zero, zero)
+        return hp.Tree("expression", [tree], startpos, endpos, inputfile)
 
     @staticmethod
     def cast_name(from_type, to_type):
@@ -251,7 +250,7 @@ class SemanticsVisitor(Visitor):
             return
         cast_name = SemanticsVisitor.cast_name(from_type, to_type)
         cast_tree = \
-            SemanticsVisitor.func_call(cast_name, [child])
+            SemanticsVisitor.func_call(cast_name, [child], tree)
         try:
             self.visit(cast_tree)
         except RuntimeError:
@@ -504,7 +503,7 @@ class SemanticsVisitor(Visitor):
             node = tree.get_child("rvalue")
             expression = tree.get_child("expression")
             operation = "dict_read"
-            call_tree = SemanticsVisitor.func_call(operation, [node, expression])
+            call_tree = SemanticsVisitor.func_call(operation, [node, expression], tree)
             self.visit(call_tree)
             tree.head = call_tree.head
             tree._tail = call_tree.tail