return super().__init__()
def __str__(self):
- return f"oppc_transient(&(struct oppc_int){{}}, {self.__value}, {self.__bits})"
+ return f"oppc_transient(&(struct oppc_value){{}}, {self.__value}, {self.__bits})"
class CCall(pc_ast.Dataclass):
self.__code[self.__header].emit(stmt=f"oppc_{name}(void) {{")
with self.__code[self.__header]:
for decl in self.__decls:
- self.__code[self.__header].emit(stmt=f"struct oppc_int {decl};")
+ self.__code[self.__header].emit(stmt=f"struct oppc_value {decl};")
self.__code[self.__footer].emit(stmt=f"}}")
def __iter__(self):
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]:
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]:
pc_ast.Lt, pc_ast.Le,
pc_ast.Eq, pc_ast.NotEq,
pc_ast.Ge, pc_ast.Gt,
+ pc_ast.LtU, pc_ast.GtU,
)
+ if isinstance(node.left, pc_ast.IfExpr):
+ self.ternary(node=node.left)
+ if isinstance(node.right, pc_ast.IfExpr):
+ self.ternary(node=node.right)
+
if isinstance(node.op, comparison):
ccall = self.ccall(name=str(self[node.op]), node=node, code=[
self[node.left],
@pc_util.Hook(pc_ast.UnaryExpr)
def UnaryExpr(self, node):
yield node
+ if isinstance(node.value, pc_ast.IfExpr):
+ self.ternary(node=node.value)
ccall = self.ccall(name=str(self[node.op]), node=node, code=[
self[node.value],
])
pc_ast.Lt, pc_ast.Le,
pc_ast.Eq, pc_ast.NotEq,
pc_ast.Ge, pc_ast.Gt,
+ pc_ast.LtU, pc_ast.GtU,
pc_ast.LShift, pc_ast.RShift,
pc_ast.BitAnd, pc_ast.BitOr, pc_ast.BitXor,
+ pc_ast.BitConcat,
)
def Op(self, node):
yield node
pc_ast.Lt: "oppc_lt",
pc_ast.Le: "oppc_le",
pc_ast.Eq: "oppc_eq",
+ pc_ast.LtU: "oppc_ltu",
+ pc_ast.GtU: "oppc_gtu",
pc_ast.NotEq: "oppc_noteq",
pc_ast.Ge: "oppc_ge",
pc_ast.Gt: "oppc_gt",
pc_ast.BitAnd: "oppc_and",
pc_ast.BitOr: "oppc_or",
pc_ast.BitXor: "oppc_xor",
+ pc_ast.BitConcat: "oppc_concat",
}[node.__class__]
self[node].emit(stmt=op)