lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.37k stars 157 forks source link

Add support for item access from `Const` data-structures #2579

Closed kmr-srbh closed 2 months ago

kmr-srbh commented 4 months ago

Fixes #2578 The following changes were made:

Fix

Dictionary

d: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3}
print(d["a"])
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
1

List

l: Const[list[i32]] = [1, 2, 3, 4, 5]
print(l[0])
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
1

Tuple

t: Const[tuple[i32, i32, i32, i32, i32]] = (1, 2, 3, 4, 5)
print(t[0])
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
semantic error: 'Const' not required as tuples are already immutable
  --> ./examples/example.py:10:4
   |
10 | t: Const[tuple[i32, i32, i32, i32, i32]] = (1, 2, 3, 4, 5)
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

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

String

s: Const[str] = "Hello, LPython!"
print(s[0])
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
semantic error: 'Const' not required as strings are already immutable
  --> ./examples/example.py:13:4
   |
13 | s: Const[str] = "Hello, LPython!"
   |    ^^^^^^^^^^ 

Note: Please report unclear or confusing messages as bugs at
https://github.com/lcompilers/lpython/issues.
Thirumalai-Shaktivel commented 4 months ago

Please mark this PR ready for review once it is ready.

kmr-srbh commented 4 months ago

@Shaikh-Ubaid @Thirumalai-Shaktivel there is a considerable degree of redundancy here, like for dictionary and lists. I have to implement slicing on Const list and the problem will arise there too. What is the fix? Functions?

Thirumalai-Shaktivel commented 3 months ago

What is the status of this PR?

The CI seems to fail, please mark this PR ready for review once it is ready.

kmr-srbh commented 3 months ago

What is the status of this PR?

The CI seems to fail, please mark this PR ready for review once it is ready.

@Thirumalai-Shaktivel this PR is ready. The only blocker is the issue stated here - https://github.com/lcompilers/lpython/pull/2567#issuecomment-1987333638. Could you please look into it?

kmr-srbh commented 2 months ago

@Shaikh-Ubaid , type_get_past_const was removed in the new additions from LFortran. What do we do here now? Use get_contained_type()?

Shaikh-Ubaid commented 2 months ago

@Shaikh-Ubaid , type_get_past_const was removed in the new additions from LFortran. What do we do here now? Use get_contained_type()?

Simply remove the call to type_get_past_const(). Cont_t node no more exists. You need not use anything.

kmr-srbh commented 2 months ago

@Shaikh-Ubaid as the Const node was removed, this works out-of-the-box now. I have reverted all the previous unnecessary changes. The tests and semantic error for creating Const str and Const tuple have been retained. This is ready.

A related issue is the failing slicing on Const list and Const str. I will open an issue for it.