[docs]classLazyCodeStatement:""" A base class representing a lazy code statement that will be converted to a string representation. """comment:str|None=None
[docs]defc_str(self)->str:""" Convert the lazy code statement to a C-style string representation. This method should be implemented by subclasses. """raiseNotImplementedError("Subclasses must implement this method.")
def__str__(self)->str:returnself.c_str()
[docs]@dataclassclassAssignmentExpr(LazyCodeStatement):""" A class representing an assignment expression in code. """left:VMVariable|str|LazyCodeStatementright:VMVariable|str|LazyCodeStatement
[docs]defc_str(self)->str:""" Convert the assignment expression to a C-style string representation. """returnf"{self.left} = {self.right}"
[docs]@dataclassclassLine(LazyCodeStatement):""" A class representing a single line of code. """text:str
[docs]classBlock(LazyCodeStatement):""" A class representing a block of code, which consists of multiple statements. """statements:list[LazyCodeStatement|str]
[docs]@dataclassclassComment(Line):""" A class representing a comment in code. """other:LazyCodeStatement|None=Nonedef__radd__(self,other:LazyCodeStatement)->LazyCodeStatement:self.other=otherreturnself
[docs]@dataclassclassCallExpr(LazyCodeStatement):""" A class representing a function or method call expression. """obj:str|VMVariable|Nonefunc:strargs:list[str|VMVariable]|None=Noneis_ptr:bool=False
[docs]defc_str(self)->str:""" Return the function call expression in C-style syntax. """args_str=", ".join(map(str,self.argsor[]))args_formatted=f"({args_str})"ifself.obj:returnf"{self.obj}{'->'ifself.is_ptrelse'.'}{self.func}{args_formatted}"returnf"{self.func}{args_formatted}"
[docs]classCode:""" A class representing a collection of code statements and variable declarations. Args: vm (VM): The virtual machine containing variable information. """__statements_:list[LazyCodeStatement]
[docs]deflines(self):""" Generate lines of code including variable declarations and statements. Yields: str: The lines of code, including variable declarations and statements. """iflen(self.__variables_)>0:forvarinself.__variables_.values():yieldf"{var.type}{var.name};"forstmtinself.__statements_:yieldstmt.c_str()