oppc/code: align ternary if expressions
authorDmitry Selyutin <ghostmansd@gmail.com>
Sun, 14 Jan 2024 13:20:37 +0000 (16:20 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Tue, 16 Jan 2024 19:10:07 +0000 (22:10 +0300)
src/openpower/oppc/pc_code.py

index ac68b48bd0d1d20994e7b5b6aad5cb39bcf54a0d..f713e55b42f090898de61c5746d99c148cf62421 100644 (file)
@@ -75,6 +75,20 @@ class CodeVisitor(pc_util.Visitor):
         self.traverse(root=ccall)
         return ccall
 
+    def ternary(self, node):
+        self[node].clear()
+        self[node].emit(stmt="(")
+        with self[node]:
+            for (level, stmt) in self[node.test]:
+                self[node].emit(stmt=stmt, level=level)
+            self[node].emit(stmt="?")
+            for (level, stmt) in self[node.body]:
+                self[node].emit(stmt=stmt, level=level)
+            self[node].emit(stmt=":")
+            for (level, stmt) in self[node.orelse]:
+                self[node].emit(stmt=stmt, level=level)
+        self[node].emit(stmt=")")
+
     @contextlib.contextmanager
     def pseudocode(self, node):
         for (level, stmt) in self.__pseudocode[node]:
@@ -97,33 +111,26 @@ class CodeVisitor(pc_util.Visitor):
         if isinstance(node.rvalue, (pc_ast.GPR, pc_ast.FPR)):
             self.__regfetch[str(node.rvalue)].append(node.rvalue)
 
-        rvalue = self[node.rvalue]
         if isinstance(node.rvalue, pc_ast.IfExpr):
-            rvalue = [(0, " ".join([
-                str(self[node.rvalue.test]),
-                "?",
-                str(self[node.rvalue.body[0]]),
-                ":",
-                str(self[node.rvalue.orelse[0]]),
-            ]))]
+            self.ternary(node=node.rvalue)
 
         if isinstance(node.lvalue, pc_ast.SubscriptExpr):
             ccall = self.ccall(name="oppc_subscript_assign", node=node, stmt=True, code=[
                 self[node.lvalue.subject],
                 self[node.lvalue.index],
-                rvalue,
+                self[node.rvalue],
             ])
         elif isinstance(node.lvalue, pc_ast.RangeSubscriptExpr):
             ccall = self.ccall(name="oppc_range_subscript_assign", node=node, stmt=True, code=[
                 self[node.lvalue.subject],
                 self[node.lvalue.start],
                 self[node.lvalue.end],
-                rvalue,
+                self[node.rvalue],
             ])
         else:
             ccall = self.ccall(name="oppc_assign", stmt=True, node=node, code=[
                 self[node.lvalue],
-                rvalue,
+                self[node.rvalue],
             ])
         with self.pseudocode(node=node):
             for (level, stmt) in self[ccall]: