oppc: simplify ternary conditionals
authorDmitry Selyutin <ghostmansd@gmail.com>
Tue, 9 Jan 2024 18:29:45 +0000 (21:29 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Tue, 16 Jan 2024 19:10:07 +0000 (22:10 +0300)
src/openpower/oppc/pc_pseudocode.py

index 76058ecb62ebffaa54862e825828bfaf46971469..f6b5a56c1d0e5626e3636a652a59db1960679121 100644 (file)
@@ -103,32 +103,22 @@ class PseudocodeVisitor(mdis.visitor.ContextVisitor):
         rvalue = str(self[node.rvalue])
 
         if isinstance(node.rvalue, pc_ast.IfExpr):
-            # All right, this deserves an explanation.
-            # We basically convert T <- C ? A : B into this code:
-            #
-            # if C then
-            #     T <- A
-            # else
-            #     T <- B
-            #
-            # To make things work, we must ensure that objects are unique.
-            # Otherwise we'll reuse the bogus code already produced before.
-            (body, orelse) = map(lambda node: node.clone(),
-                (node.rvalue.body[0], node.rvalue.orelse[0]))
-            body = pc_ast.Scope([node.__class__(lvalue=node.lvalue.clone(), rvalue=body)])
-            orelse = pc_ast.Scope([node.__class__(lvalue=node.lvalue.clone(), rvalue=orelse)])
-            tmpnode = node.rvalue.clone(body=body, orelse=orelse)
-            walker = mdis.walker.Walker()
-            traverse(root=tmpnode, visitor=self, walker=walker)
-            for (level, stmt) in self[tmpnode]:
-                self[node].emit(stmt=stmt, level=level)
+            stmt = " ".join([
+                lvalue,
+                mapping[node.__class__],
+                str(self[node.rvalue.test]),
+                "?",
+                str(self[node.rvalue.body[0]]),
+                ":",
+                str(self[node.rvalue.orelse[0]]),
+            ])
         else:
             stmt = " ".join([
                 lvalue,
                 mapping[node.__class__],
                 rvalue,
             ])
-            self[node].emit(stmt=stmt)
+        self[node].emit(stmt=stmt)
 
     @Hook(pc_ast.BinaryExpr)
     def BinaryExpr(self, node):