From f09344739dcef7a987e76fcc1c458d17c7cbb586 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Sun, 14 Jan 2024 16:20:37 +0300 Subject: [PATCH] oppc/code: align ternary if expressions --- src/openpower/oppc/pc_code.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/openpower/oppc/pc_code.py b/src/openpower/oppc/pc_code.py index ac68b48b..f713e55b 100644 --- a/src/openpower/oppc/pc_code.py +++ b/src/openpower/oppc/pc_code.py @@ -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]: -- 2.30.2