Skip to content

Implement class #391

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

Closed
wants to merge 1 commit into from
Closed

Implement class #391

wants to merge 1 commit into from

Conversation

Smit-create
Copy link
Collaborator

@Smit-create Smit-create marked this pull request as draft April 18, 2022 13:30
@certik
Copy link
Contributor

certik commented Apr 19, 2022

Thanks for working on this.

Comment on lines +1496 to +1501
tmp = ASR::make_ClassType_t(al, x.base.base.loc, current_scope,
x.m_name, ASR::abiType::Source, ASR::accessType::Public);
tmp = ASR::make_Class_t(al, x.base.base.loc,
ASR::down_cast<ASR::symbol_t>(tmp), nullptr, 0);
ASR::symbol_t * t = ASR::down_cast<ASR::symbol_t>(tmp);
parent_scope->scope[x.m_name] = t;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@certik Does this look good?

On Python code:

class Foo:
    def __init__(self, p):
        return p

ASR:

Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/thebigbool/repos/lpython/src/bin/lpython.cpp", line 755
    return emit_asr(arg_file, runtime_library_dir,
  File "/Users/thebigbool/repos/lpython/src/bin/lpython.cpp", line 169
    std::cerr << diagnostics.render(input, lm, compiler_options);
  File "/Users/thebigbool/repos/lpython/src/libasr/diagnostics.cpp", line 71
    out += render_diagnostic(d, input, lm,
  File "/Users/thebigbool/repos/lpython/src/libasr/diagnostics.cpp", line 134
    populate_spans(d, lm, input);
  File "/Users/thebigbool/repos/lpython/src/libasr/diagnostics.cpp", line 121
    populate_span(s, lm, input);
  File "/Users/thebigbool/repos/lpython/src/libasr/diagnostics.cpp", line 113
    LFORTRAN_ASSERT(s.source_code.size() > 0)
AssertFailed: s.source_code.size() > 0

Also, what is the difference between Class and ClassProcedure Nodes in ASR?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a bug in printing an error message, I just created an issue #398 to fix that.

Yes, I think that is a constructor. Fortran does not have constructors in this sense, so I would tackle those a little bit later. We should add whatever we need into ASR, but first let's get something like this working:

class Foo:
    def __init__(self: Foo, a: i32, b: i32, c: i32):
        self.a: i32 = a
        self.b: i32 = b
        self.c: i32 = c

    def get_a(self: Foo) -> i32:
        return self.a

I am not quite sure where you should annotate the variables inside the class, whether how I did it, or some other way.

The other thing we need are structs, which we can represent already in ASR, we just need some syntax in Python, possibly the "data classes" or whatever it is called:

@dataclass # or whatever it is called
class MyStruct:
    a: i32
    b: f64
    c: str

And it works like a struct.

Copy link
Contributor

Choose a reason for hiding this comment

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

To annotate that a is an instance of the class Foo, we can do:

a: Foo
a = Foo()

Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not quite sure where you should annotate the variables inside the class, whether how I did it, or some other way.

The way you did it seems fine.

tmp = ASR::make_ClassType_t(al, x.base.base.loc, current_scope,
x.m_name, ASR::abiType::Source, ASR::accessType::Public);
tmp = ASR::make_Class_t(al, x.base.base.loc,
ASR::down_cast<ASR::symbol_t>(tmp), nullptr, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

The Class ASR node is used for a variable type, so in here we should just remove it.

@czgdp1807
Copy link
Collaborator

I implemented dataclass recently in #607. So I think this can be closed.

@czgdp1807 czgdp1807 closed this Jun 19, 2022
@certik
Copy link
Contributor

certik commented Jun 19, 2022

Well, dataclasses are implemented, but not regular classes. But we can do it later, that's a lower priority.

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.

3 participants