lpython icon indicating copy to clipboard operation
lpython copied to clipboard

Class TODO list

Open certik opened this issue 1 year ago • 3 comments

  • [ ] use existing class support in ASR, hook it in AST->ASR, add tests:
    • [ ] use lfortran integration_tests/class_01.f90 --show-asr to learn how ASR works for classes (see what is supported in class_*.f90
    • [ ] create similar tests in LPython, and create the same ASR
  • [ ] investigate how CPython declares member variables for classes
  • [ ] identify what is missing (I think inheritance and some other things)
  • [ ] implement those

certik avatar May 03 '24 16:05 certik

Hey @tanay-man, you can start with a very simple code and try to compile that using LPython. Example

class Test():
    i = 5

x = Test()
print(x.i)
$ python test.py
5

This needs to be statically typed to be compiled by LPython, So, we can do

from lpython import i32

class Test():
    i: i32 = 5

x: Test = Test()
print(x.i)
$ python test.py
5

Make sure that every LPython code must also be compiled using CPython. Now, you can use this code to start making the initial implementation in LPython. LPython throws the following error:

$ lpython test.py 
semantic error: Only dataclass-decorated classes and Enum subclasses are supported.
  --> test.py:12:1 - 13:11
   |
12 |    class Test():
   |    ^^^^^^^^^^^^^...
...
   |
13 |     i: i32 = 5
   | ...^^^^^^^^^^^ 


Note: Please report unclear or confusing messages as bugs at
https://github.com/lcompilers/lpython/issues.

Try to analyse the AST and then start handling this in the ASR (Sematics phase)

Thirumalai-Shaktivel avatar May 24 '24 14:05 Thirumalai-Shaktivel

The above code is working. The StructType node of the ASR is used for the implementation of classes. According to the documentation, the members declared in the class body are static (common to all instances of the class) and those declared inside the constructor are specific to the instances (object members). This is not true for the current implementation.

tanay-man avatar May 28 '24 06:05 tanay-man

@tanay-man, now we have some initial implementation. You can start testing by adding some more tests, also port some tests from lfortran, like class_03, Inheritance, ...

Thirumalai-Shaktivel avatar Jul 16 '24 16:07 Thirumalai-Shaktivel