Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdd9ad5

Browse files
authored
ASR -> CPython: Add visitors for ListConstant, TupleConstant & SetConstant (#2690)
* Add an aggregate visitor for `ListConstant`, `TupleConstant` and `SetConstant` * Add type annotations for `Tuple` and `Set` variables * Tests: Add tests and update references * Tests: Rename test and update references * Remove unnecessary function `std::string get_type()`
1 parent a8d109c commit bdd9ad5

File tree

5 files changed

+217
-30
lines changed

5 files changed

+217
-30
lines changed

src/libasr/codegen/asr_to_python.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -129,35 +129,6 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor<ASRToLpythonVisitor>
129129
}
130130
}
131131

132-
std::string get_type(const ASR::ttype_t *t) {
133-
std::string r = "";
134-
switch (t->type) {
135-
case ASR::ttypeType::Integer : {
136-
r += "i";
137-
r += std::to_string(ASRUtils::extract_kind_from_ttype_t(t)*8);
138-
break;
139-
} case ASR::ttypeType::Real : {
140-
r += "f";
141-
r += std::to_string(ASRUtils::extract_kind_from_ttype_t(t)*8);
142-
break;
143-
} case ASR::ttypeType::Complex : {
144-
r += "c";
145-
r += std::to_string(ASRUtils::extract_kind_from_ttype_t(t)*8);
146-
break;
147-
} case ASR::ttypeType::Character : {
148-
r = "str";
149-
break;
150-
} case ASR::ttypeType::Logical : {
151-
r = "bool";
152-
break;
153-
} default : {
154-
throw LCompilersException("The type `"
155-
+ ASRUtils::type_to_str_python(t) + "` is not handled yet");
156-
}
157-
}
158-
return r;
159-
}
160-
161132
void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
162133
std::string r = "";
163134

@@ -245,7 +216,7 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor<ASRToLpythonVisitor>
245216
std::string r = indent;
246217
r += x.m_name;
247218
r += ": ";
248-
r += get_type(x.m_type);
219+
r += ASRUtils::type_to_str_python(x.m_type);
249220
r += "\n";
250221
s = r;
251222
}
@@ -619,6 +590,34 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor<ASRToLpythonVisitor>
619590
s = r;
620591
}
621592

593+
// An aggregate visitor for `ListConstant`, `TupleConstant` & `SetConstant`
594+
void visit_AggregateConstant(size_t n_args, ASR::expr_t** m_args,
595+
std::string opening_braces, std::string closing_braces) {
596+
std::string r = "";
597+
r += opening_braces;
598+
for (size_t i = 0; i < n_args; i++) {
599+
this->visit_expr(*m_args[i]);
600+
r.append(s);
601+
if (i < n_args - 1) {
602+
r.append(", ");
603+
}
604+
}
605+
r += closing_braces;
606+
s = r;
607+
}
608+
609+
void visit_ListConstant(const ASR::ListConstant_t &x) {
610+
visit_AggregateConstant(x.n_args, x.m_args, "[", "]");
611+
}
612+
613+
void visit_TupleConstant(const ASR::TupleConstant_t &x) {
614+
visit_AggregateConstant(x.n_elements, x.m_elements, "(", ")");
615+
}
616+
617+
void visit_SetConstant(const ASR::SetConstant_t &x) {
618+
visit_AggregateConstant(x.n_elements, x.m_elements, "{", "}");
619+
}
620+
622621
};
623622

624623
Result<std::string> asr_to_python(Allocator& al, ASR::TranslationUnit_t &asr,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "python-test_aggregate_constants-26c89d6",
3+
"cmd": "lpython --no-color --show-python {infile}",
4+
"infile": "tests/test_aggregate_constants.py",
5+
"infile_hash": "e4f25c9787536c0ecc60a1d53b4bca5f250e2a4f3646b45565867e86",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "python-test_aggregate_constants-26c89d6.stdout",
9+
"stdout_hash": "813b11b4ee92df0eebccb3031a1b73355957795a72b487115093c3ce",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
def __main__global_init():
2+
my_first_list = [1, 2, 3, 4]
3+
my_second_list = ["a", "b", "c", "d"]
4+
my_third_list = [[1, 2], [3, 4], [5, 6]]
5+
my_fourth_list = [[1.000000, 2.200000], [3.600000, 4.900000], [5.100000, 6.300000]]
6+
my_fifth_list = [{"a", "b"}, {"c", "d"}]
7+
my_sixth_list = [(1, "a"), (2, "b")]
8+
my_first_tuple = (1, "hello", 2.400000)
9+
my_second_tuple = ((1, "hello"), "world")
10+
my_third_tuple = (["hello", "world"], "world")
11+
my_fourth_tuple = ({"hello", "world"}, "world")
12+
my_first_set = {1, 2, 3, 2, 4}
13+
my_second_set = {1.100000, 2.500000, 6.800000}
14+
my_third_set = {"a", "b", "a", "c"}
15+
my_fourth_set = {(1, "a"), (2, "b"), (3, "c")}
16+
def __main__global_stmts():
17+
print(my_first_list)
18+
print(my_second_list)
19+
print(my_third_list)
20+
print(my_fourth_list)
21+
print(my_fifth_list)
22+
print(my_sixth_list)
23+
print(my_first_tuple)
24+
print(my_second_tuple)
25+
print(my_third_tuple)
26+
print(my_fourth_tuple)
27+
print(my_first_set)
28+
print(my_second_set)
29+
print(my_third_set)
30+
print(my_fourth_set)
31+
fn()
32+
def fn():
33+
my_fifth_list: list[set[str]]
34+
my_first_list: list[i32]
35+
my_first_set: set[i32]
36+
my_first_tuple: tuple[i32, str, f64]
37+
my_fourth_list: list[list[f64]]
38+
my_fourth_set: set[tuple[i32, str]]
39+
my_fourth_tuple: tuple[set[str], str]
40+
my_second_list: list[str]
41+
my_second_set: set[f64]
42+
my_second_tuple: tuple[tuple[i32, str], str]
43+
my_sixth_list: list[tuple[i32, str]]
44+
my_third_list: list[list[i32]]
45+
my_third_set: set[str]
46+
my_third_tuple: tuple[list[str], str]
47+
my_first_list = [1, 2, 3, 4]
48+
print(my_first_list)
49+
my_second_list = ["a", "b", "c", "d"]
50+
print(my_second_list)
51+
my_third_list = [[1, 2], [3, 4], [5, 6]]
52+
print(my_third_list)
53+
my_fourth_list = [[1.000000, 2.200000], [3.600000, 4.900000], [5.100000, 6.300000]]
54+
print(my_fourth_list)
55+
my_fifth_list = [{"a", "b"}, {"c", "d"}]
56+
print(my_fifth_list)
57+
my_sixth_list = [(1, "a"), (2, "b")]
58+
print(my_sixth_list)
59+
my_first_tuple = (1, "hello", 2.400000)
60+
print(my_first_tuple)
61+
my_second_tuple = ((1, "hello"), "world")
62+
print(my_second_tuple)
63+
my_third_tuple = (["hello", "world"], "world")
64+
print(my_third_tuple)
65+
my_fourth_tuple = ({"hello", "world"}, "world")
66+
print(my_fourth_tuple)
67+
my_first_set = {1, 2, 3, 2, 4}
68+
print(my_first_set)
69+
my_second_set = {1.100000, 2.500000, 6.800000}
70+
print(my_second_set)
71+
my_third_set = {"a", "b", "a", "c"}
72+
print(my_third_set)
73+
my_fourth_set = {(1, "a"), (2, "b"), (3, "c")}
74+
print(my_fourth_set)

tests/test_aggregate_constants.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from lpython import i32, f64
2+
3+
# Test codegen for global scope
4+
5+
# List
6+
my_first_list: list[i32] = [1, 2, 3 , 4]
7+
print(my_first_list)
8+
9+
my_second_list: list[str] = ["a", "b", "c", "d"]
10+
print(my_second_list)
11+
12+
my_third_list: list[list[i32]] = [[1, 2], [3, 4], [5, 6]]
13+
print(my_third_list)
14+
15+
my_fourth_list: list[list[f64]] = [[1.0, 2.2], [3.6, 4.9], [5.1, 6.3]]
16+
print(my_fourth_list)
17+
18+
my_fifth_list: list[set[str]] = [{"a", "b"}, {"c", "d"}]
19+
print(my_fifth_list)
20+
21+
my_sixth_list: list[tuple[i32, str]] = [(1, "a"), (2, "b")]
22+
print(my_sixth_list)
23+
24+
# Tuple
25+
my_first_tuple: tuple[i32, str, f64] = (1, "hello", 2.4)
26+
print(my_first_tuple)
27+
28+
my_second_tuple: tuple[tuple[i32, str], str] = ((1, "hello"), "world")
29+
print(my_second_tuple)
30+
31+
my_third_tuple: tuple[list[str], str] = (["hello", "world"], "world")
32+
print(my_third_tuple)
33+
34+
my_fourth_tuple: tuple[set[str], str] = ({"hello", "world"}, "world")
35+
print(my_fourth_tuple)
36+
37+
# Set
38+
my_first_set: set[i32] = {1, 2, 3, 2, 4}
39+
print(my_first_set)
40+
41+
my_second_set: set[f64] = {1.1, 2.5, 6.8}
42+
print(my_second_set)
43+
44+
my_third_set: set[str] = {"a", "b", "a", "c"}
45+
print(my_third_set)
46+
47+
my_fourth_set: set[tuple[i32, str]] = {(1, "a"), (2, "b"), (3, "c")}
48+
print(my_fourth_set)
49+
50+
# Test codegen for local scope
51+
def fn():
52+
# List
53+
my_first_list: list[i32] = [1, 2, 3 , 4]
54+
print(my_first_list)
55+
56+
my_second_list: list[str] = ["a", "b", "c", "d"]
57+
print(my_second_list)
58+
59+
my_third_list: list[list[i32]] = [[1, 2], [3, 4], [5, 6]]
60+
print(my_third_list)
61+
62+
my_fourth_list: list[list[f64]] = [[1.0, 2.2], [3.6, 4.9], [5.1, 6.3]]
63+
print(my_fourth_list)
64+
65+
my_fifth_list: list[set[str]] = [{"a", "b"}, {"c", "d"}]
66+
print(my_fifth_list)
67+
68+
my_sixth_list: list[tuple[i32, str]] = [(1, "a"), (2, "b")]
69+
print(my_sixth_list)
70+
71+
# Tuple
72+
my_first_tuple: tuple[i32, str, f64] = (1, "hello", 2.4)
73+
print(my_first_tuple)
74+
75+
my_second_tuple: tuple[tuple[i32, str], str] = ((1, "hello"), "world")
76+
print(my_second_tuple)
77+
78+
my_third_tuple: tuple[list[str], str] = (["hello", "world"], "world")
79+
print(my_third_tuple)
80+
81+
my_fourth_tuple: tuple[set[str], str] = ({"hello", "world"}, "world")
82+
print(my_fourth_tuple)
83+
84+
# Set
85+
my_first_set: set[i32] = {1, 2, 3, 2, 4}
86+
print(my_first_set)
87+
88+
my_second_set: set[f64] = {1.1, 2.5, 6.8}
89+
print(my_second_set)
90+
91+
my_third_set: set[str] = {"a", "b", "a", "c"}
92+
print(my_third_set)
93+
94+
my_fourth_set: set[tuple[i32, str]] = {(1, "a"), (2, "b"), (3, "c")}
95+
print(my_fourth_set)
96+
97+
fn()

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ filename = "dictionary1.py"
5151
ast = true
5252
asr = true
5353

54+
[[test]]
55+
filename = "test_aggregate_constants.py"
56+
python = true
57+
5458
[[test]]
5559
filename = "expr1.py"
5660
ast = true

0 commit comments

Comments
 (0)