浏览代码

Fix (?) for the need for parentheses

Yentl Van Tendeloo 7 年之前
父节点
当前提交
d3222ee551

+ 23 - 10
interface/HUTN/hutn_compiler/semantics_visitor.py

@@ -127,9 +127,16 @@ class SemanticsVisitor(Visitor):
                         self.get_type(tree)))
 
     def replace_child_binary_op_with_call(self, tree, i=0):
-        child = tree.get_tail()[i]
+        if i == -1:
+            child = tree
+        else:
+            child = tree.get_tail()[i]
         if len(child.get_tail()) > 1:
-            l, op, r = child.get_tail()
+            try:
+                l, op, r = child.get_tail()
+            except:
+                # Something went wrong... this code is severely broken
+                return
             l_type, r_type = self.get_type(l), self.get_type(r)
             if type(l_type) != type(r_type):
                 print("Error: " + str(l_type) + " <-> " + str(r_type))
@@ -140,7 +147,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], tree)
+            call_tree = self.func_call(call_name, [l, r], tree)
             try:
                 self.visit(call_tree)
             except RuntimeError:
@@ -154,7 +161,12 @@ class SemanticsVisitor(Visitor):
                         tree.startpos['column'],
                         child.head,
                         call_signature))
-            tree.replace_child(child, call_tree)
+            if i == -1:
+                tree.head = call_tree.head
+                tree.tail = call_tree.tail
+                tree._tail = None
+            else:
+                tree.replace_child(child, call_tree)
         self.set_type(tree, self.get_type(tree.get_tail()[i]))
 
     def replace_child_unary_op_with_call(self, tree):
@@ -165,7 +177,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], tree)
+            call_tree = self.func_call(call_name, [l], tree)
             try:
                 self.visit(call_tree)
             except RuntimeError:
@@ -202,8 +214,7 @@ class SemanticsVisitor(Visitor):
         p_type = self.promote_unary_ops_arithmetic(tree)
         self.perform_implicit_cast(tree, l, l_type, p_type)
 
-    @staticmethod
-    def func_call(name, params, old_tree):
+    def func_call(self, name, params, old_tree):
         startpos = old_tree.startpos
         endpos = old_tree.endpos
         inputfile = old_tree.inputfile
@@ -220,6 +231,9 @@ class SemanticsVisitor(Visitor):
             ],
             startpos, endpos, inputfile)
 
+        for p in params:
+            self.replace_child_binary_op_with_call(p, -1)
+
         params = [hp.Tree("expression", [p], startpos, endpos, inputfile) for p in params]
 
         tree.tail.extend(params)
@@ -252,8 +266,7 @@ class SemanticsVisitor(Visitor):
         if type(from_type) == type(to_type):
             return
         cast_name = SemanticsVisitor.cast_name(from_type, to_type)
-        cast_tree = \
-            SemanticsVisitor.func_call(cast_name, [child], tree)
+        cast_tree = self.func_call(cast_name, [child], tree)
         try:
             self.visit(cast_tree)
         except RuntimeError:
@@ -507,7 +520,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], tree)
+            call_tree = self.func_call(operation, [node, expression], tree)
             self.visit(call_tree)
             tree.head = call_tree.head
             tree._tail = call_tree.tail

+ 2 - 2
interface/HUTN/test/constructor_compilation_action_language/code/string_concat.al

@@ -2,6 +2,6 @@ Element string_join = ?primitives/string_join
 Element integer_addition = ?primitives/integer_addition
 
 Void function main():
-	Integer a
-	a = 1 + 2 + 3
+	String a
+	a = "1" + "2" + "3"
 	return!

+ 1 - 1
interface/HUTN/test/constructor_compilation_action_language/expected/string_concat

@@ -1 +1 @@
-[42, "global", "string_join", "deref", "primitives/string_join", true, "global", "integer_addition", "deref", "primitives/integer_addition", true, "funcdef", "main", 0, "declare", "0", "none", true, "assign", "resolve", "0", "call", "access", "resolve", "integer_addition", 2, "call", "access", "resolve", "integer_addition", 2, "const", 1, "const", 2, false, "const", 3, false, true, "return", false, false]
+[42, "global", "string_join", "deref", "primitives/string_join", true, "global", "integer_addition", "deref", "primitives/integer_addition", true, "funcdef", "main", 0, "declare", "0", "none", true, "assign", "resolve", "0", "call", "access", "resolve", "string_join", 2, "call", "access", "resolve", "string_join", 2, "const", "1", "const", "2", false, "const", "3", false, true, "return", false, false]