You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
backend code generation when python programm cotains class #2815
I have a question regarding the compilation of a Python program using LPython. If a Python program contains any class or a class like the following:
class student:
def __init__(self: InOut['student'], name: str, age: i32):
self.name : str = name
self.age : i32 = age
Does this imply that after compilation, the code can only generate C++ code, and not C code? I would appreciate an explanation of why that might be the case. Thank you!
@ljq-0814 LPython translates the source code into an intermediary form called an Abstract Semantic Representation (ASR). This form contains all the information related to the code, including type semantics. It is done inside python_ast_to_asr.cpp. You can see the generated ASR using the --show-asr flag.
We then apply some optimization passes to this initial ASR. The final ASR is passed over to the backend for further translation. The backend translator asr_to_*.cpp visits the ASR nodes and handles it appropriately. In C, the closest resemblance to a class is struct. We handle this case similarly. You can see the final transformed code after the backend visitor using the appropriate --show-* option (see --help). Currently, our implementation of OOP is basic. The above code gets translated to the following in the C backend:
Activity
kmr-srbh commentedon Nov 10, 2024
@ljq-0814 LPython translates the source code into an intermediary form called an Abstract Semantic Representation (ASR). This form contains all the information related to the code, including type semantics. It is done inside
python_ast_to_asr.cpp. You can see the generated ASR using the--show-asrflag.The above code gets translated to:
We then apply some optimization passes to this initial ASR. The final ASR is passed over to the backend for further translation. The backend translator
asr_to_*.cppvisits the ASR nodes and handles it appropriately. In C, the closest resemblance to a class isstruct. We handle this case similarly. You can see the final transformed code after the backend visitor using the appropriate--show-*option (see--help). Currently, our implementation of OOP is basic. The above code gets translated to the following in the C backend: