Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented basic classes #2775

Merged
merged 7 commits into from
Jul 16, 2024
Merged

Conversation

tanay-man
Copy link
Contributor

@tanay-man tanay-man commented Jul 11, 2024

Implemented self, member functions, and calling init.

@Thirumalai-Shaktivel @certik

Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good to me!

Great job.

@Thirumalai-Shaktivel can you please also review this? If it looks good, go ahead and merge.

@tanay-man
Copy link
Contributor Author

tanay-man commented Jul 15, 2024

Added Assignment of StructConstructor and related test. Working on deepcopying objects:
Example:

obj: clss = clss()
obj2: clss = obj

Related issue : #2775

@@ -0,0 +1,44 @@
from lpython import i32
class Character:
def __init__(self:"Character", name:str, health:i32, attack_power:i32):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have __new__ here?

Copy link
Contributor Author

@tanay-man tanay-man Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The __init__ in LPython has a similar signature to the the __new__in Cpython but it performs the functionality of the __init__ function. This doesn't control the memory allocation of the object.

@namannimmo10
Copy link
Collaborator

Looks great, thank you. Are you planning to introduce ClassVar too?

Copy link
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as well, thank you!

Comment on lines +3353 to +3354
virtual void visit_init_body (const AST::FunctionDef_t &/*x*/) = 0;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not used, you can remove this.

ASR::down_cast<ASR::Variable_t>(st)->m_type)->m_derived_type;
std::string call_name = "__init__";
ASR::symbol_t* call_sym = get_struct_member(der, call_name, x.base.base.loc);
tmp_vec.push_back(make_call_helper(al, call_sym,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This modifies the meaning of the code, I mean, we have to handle this in the ASR pass.
The generated ASR must be the same as the original source code.
This can be done in the upcoming PR.

Copy link
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example

x: test = test(1, 3)

The ASR should be generated as

x: test
x = test(1, 3) # StructConstructor

But, PR generates the following

x: test
x__init__(x, 1, 3) # SubroutineCall

Let's handle this in the ASR pass

@tanay-man
Copy link
Contributor Author

Looks great, thank you. Are you planning to introduce ClassVar too?

I do plan on implementing the ClassVars but they are a lower priority right now.

@Thirumalai-Shaktivel
Copy link
Collaborator

I'm merging this.

@Thirumalai-Shaktivel Thirumalai-Shaktivel enabled auto-merge (squash) July 16, 2024 16:11
@Thirumalai-Shaktivel Thirumalai-Shaktivel merged commit 9e8da7d into lcompilers:main Jul 16, 2024
15 checks passed
@tanay-man tanay-man deleted the self branch July 24, 2024 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants