Skip to content

Commit e378b4e

Browse files
authored
ASR -> CPython: Add DictConstant visitor (#2693)
* Add `DictConstant` visitor * Remove check for `Dict` The function is not required due to ad404f9 * Improve visitor * Remove stray newline * Tests: Add tests and update references
1 parent bdd9ad5 commit e378b4e

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

src/libasr/codegen/asr_to_python.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,25 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor<ASRToLpythonVisitor>
590590
s = r;
591591
}
592592

593+
void visit_DictConstant(const ASR::DictConstant_t &x) {
594+
LCOMPILERS_ASSERT(x.n_keys == x.n_values);
595+
std::string r = "";
596+
r += "{";
597+
for (size_t i = 0; i < x.n_keys; i++) {
598+
visit_expr(*x.m_keys[i]);
599+
r += s;
600+
r += ": ";
601+
visit_expr(*x.m_values[i]);
602+
r += s;
603+
if (i < x.n_keys - 1) {
604+
r += ", ";
605+
}
606+
}
607+
r += "}";
608+
609+
s = r;
610+
}
611+
593612
// An aggregate visitor for `ListConstant`, `TupleConstant` & `SetConstant`
594613
void visit_AggregateConstant(size_t n_args, ASR::expr_t** m_args,
595614
std::string opening_braces, std::string closing_braces) {
@@ -617,7 +636,6 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor<ASRToLpythonVisitor>
617636
void visit_SetConstant(const ASR::SetConstant_t &x) {
618637
visit_AggregateConstant(x.n_elements, x.m_elements, "{", "}");
619638
}
620-
621639
};
622640

623641
Result<std::string> asr_to_python(Allocator& al, ASR::TranslationUnit_t &asr,

tests/reference/python-test_aggregate_constants-26c89d6.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "python-test_aggregate_constants-26c89d6",
33
"cmd": "lpython --no-color --show-python {infile}",
44
"infile": "tests/test_aggregate_constants.py",
5-
"infile_hash": "e4f25c9787536c0ecc60a1d53b4bca5f250e2a4f3646b45565867e86",
5+
"infile_hash": "6e225037304a9a1222b4c356b8cd1ffc5ed4a58bb7d6916c242c7b53",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "python-test_aggregate_constants-26c89d6.stdout",
9-
"stdout_hash": "813b11b4ee92df0eebccb3031a1b73355957795a72b487115093c3ce",
9+
"stdout_hash": "3d5fa791404534643f6f2a096b2bc1561cf6c03a78d060b79df4f17b",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/python-test_aggregate_constants-26c89d6.stdout

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ def __main__global_init():
1313
my_second_set = {1.100000, 2.500000, 6.800000}
1414
my_third_set = {"a", "b", "a", "c"}
1515
my_fourth_set = {(1, "a"), (2, "b"), (3, "c")}
16+
my_first_dict = {"a": 1, "b": 2, "c": 3}
17+
my_second_dict = {1: 1.330000, 2: 2.330000, 3: 3.330000}
18+
my_third_dict = {"a": "A", "b": "B", "c": "C"}
19+
my_fourth_dict = {1: (1.200000, 4.500000), 2: (3.600000, 9.200000)}
20+
my_fifth_dict = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
21+
my_sixth_dict = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
1622
def __main__global_stmts():
1723
print(my_first_list)
1824
print(my_second_list)
@@ -28,19 +34,31 @@ def __main__global_stmts():
2834
print(my_second_set)
2935
print(my_third_set)
3036
print(my_fourth_set)
37+
print(my_first_dict)
38+
print(my_second_dict)
39+
print(my_third_dict)
40+
print(my_fourth_dict)
41+
print(my_fifth_dict)
42+
print(my_sixth_dict)
3143
fn()
3244
def fn():
45+
my_fifth_dict: dict[str, list[i32]]
3346
my_fifth_list: list[set[str]]
47+
my_first_dict: dict[str, i32]
3448
my_first_list: list[i32]
3549
my_first_set: set[i32]
3650
my_first_tuple: tuple[i32, str, f64]
51+
my_fourth_dict: dict[i32, tuple[f64, f64]]
3752
my_fourth_list: list[list[f64]]
3853
my_fourth_set: set[tuple[i32, str]]
3954
my_fourth_tuple: tuple[set[str], str]
55+
my_second_dict: dict[i32, f64]
4056
my_second_list: list[str]
4157
my_second_set: set[f64]
4258
my_second_tuple: tuple[tuple[i32, str], str]
59+
my_sixth_dict: dict[str, set[i32]]
4360
my_sixth_list: list[tuple[i32, str]]
61+
my_third_dict: dict[str, str]
4462
my_third_list: list[list[i32]]
4563
my_third_set: set[str]
4664
my_third_tuple: tuple[list[str], str]
@@ -72,3 +90,15 @@ def fn():
7290
print(my_third_set)
7391
my_fourth_set = {(1, "a"), (2, "b"), (3, "c")}
7492
print(my_fourth_set)
93+
my_first_dict = {"a": 1, "b": 2, "c": 3}
94+
print(my_first_dict)
95+
my_second_dict = {1: 1.330000, 2: 2.330000, 3: 3.330000}
96+
print(my_second_dict)
97+
my_third_dict = {"a": "A", "b": "B", "c": "C"}
98+
print(my_third_dict)
99+
my_fourth_dict = {1: (1.200000, 4.500000), 2: (3.600000, 9.200000)}
100+
print(my_fourth_dict)
101+
my_fifth_dict = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
102+
print(my_fifth_dict)
103+
my_sixth_dict = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
104+
print(my_sixth_dict)

tests/test_aggregate_constants.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@
4747
my_fourth_set: set[tuple[i32, str]] = {(1, "a"), (2, "b"), (3, "c")}
4848
print(my_fourth_set)
4949

50+
# Dictionary
51+
my_first_dict: dict[str, i32] = {"a": 1, "b": 2, "c": 3}
52+
print(my_first_dict)
53+
54+
my_second_dict: dict[i32, f64] = {1: 1.33, 2: 2.33, 3: 3.33}
55+
print(my_second_dict)
56+
57+
my_third_dict: dict[str, str] = {"a": "A", "b": "B", "c": "C"}
58+
print(my_third_dict)
59+
60+
my_fourth_dict: dict[i32, tuple[f64, f64]] = {1: (1.2, 4.5), 2: (3.6, 9.2)}
61+
print(my_fourth_dict)
62+
63+
my_fifth_dict: dict[str, list[i32]] = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
64+
print(my_fifth_dict)
65+
66+
my_sixth_dict: dict[str, set[i32]] = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
67+
print(my_sixth_dict)
68+
5069
# Test codegen for local scope
5170
def fn():
5271
# List
@@ -94,4 +113,23 @@ def fn():
94113
my_fourth_set: set[tuple[i32, str]] = {(1, "a"), (2, "b"), (3, "c")}
95114
print(my_fourth_set)
96115

116+
# Dictionary
117+
my_first_dict: dict[str, i32] = {"a": 1, "b": 2, "c": 3}
118+
print(my_first_dict)
119+
120+
my_second_dict: dict[i32, f64] = {1: 1.33, 2: 2.33, 3: 3.33}
121+
print(my_second_dict)
122+
123+
my_third_dict: dict[str, str] = {"a": "A", "b": "B", "c": "C"}
124+
print(my_third_dict)
125+
126+
my_fourth_dict: dict[i32, tuple[f64, f64]] = {1: (1.2, 4.5), 2: (3.6, 9.2)}
127+
print(my_fourth_dict)
128+
129+
my_fifth_dict: dict[str, list[i32]] = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
130+
print(my_fifth_dict)
131+
132+
my_sixth_dict: dict[str, set[i32]] = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
133+
print(my_sixth_dict)
134+
97135
fn()

0 commit comments

Comments
 (0)