lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 158 forks source link

ASR -> CPython: Add `DictConstant` visitor #2693

Closed kmr-srbh closed 4 months ago

kmr-srbh commented 4 months ago

Global scope

from lpython import i32, f64

my_first_dict: dict[str, i32] = {"a": 1, "b": 2, "c": 3}
print(my_first_dict)

my_second_dict: dict[i32, f64] = {1: 1.33, 2: 2.33, 3: 3.33}
print(my_second_dict)

my_third_dict: dict[str, str] = {"a": "A", "b": "B", "c": "C"}
print(my_third_dict)
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py --show-python
def __main__global_init():
    my_first_dict = {"a": 1, "b": 2, "c": 3}
    my_second_dict = {1: 1.330000, 2: 2.330000, 3: 3.330000}
    my_third_dict = {"a": "A", "b": "B", "c": "C"}
def __main__global_stmts():
    print(my_first_dict)
    print(my_second_dict)
    print(my_third_dict)

Local scope

from lpython import i32, f64

def f():
    my_first_dict: dict[str, i32] = {"a": 1, "b": 2, "c": 3}
    print(my_first_dict)

    my_second_dict: dict[i32, f64] = {1: 1.33, 2: 2.33, 3: 3.33}
    print(my_second_dict)

    my_third_dict: dict[str, str] = {"a": "A", "b": "B", "c": "C"}
    print(my_third_dict)

f()
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py --show-python
def __main__global_stmts():
    f()
def f():
    my_first_dict: dict[str, i32]
    my_second_dict: dict[i32, f64]
    my_third_dict: dict[str, str]
    my_first_dict = {"a": 1, "b": 2, "c": 3}
    print(my_first_dict)
    my_second_dict = {1: 1.330000, 2: 2.330000, 3: 3.330000}
    print(my_second_dict)
    my_third_dict = {"a": "A", "b": "B", "c": "C"}
    print(my_third_dict)

TODO

nikabot commented 4 months ago

See: https://github.com/lcompilers/lpython/pull/2690#discussion_r1595612282

kmr-srbh commented 4 months ago

@Shaikh-Ubaid I think we should wait until #2690 is merged. We can then test for set, tuple and list values in dictionary and have the test in the same file. Shall we?