Skip to content

Commit

Permalink
ASR -> CPython: Add DictConstant visitor (lcompilers#2693)
Browse files Browse the repository at this point in the history
* Add `DictConstant` visitor

* Remove check for `Dict`

The function is not required due to lcompilers@ad404f9

* Improve visitor

* Remove stray newline

* Tests: Add tests and update references
  • Loading branch information
kmr-srbh authored May 9, 2024
1 parent bdd9ad5 commit e378b4e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/libasr/codegen/asr_to_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,25 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor<ASRToLpythonVisitor>
s = r;
}

void visit_DictConstant(const ASR::DictConstant_t &x) {
LCOMPILERS_ASSERT(x.n_keys == x.n_values);
std::string r = "";
r += "{";
for (size_t i = 0; i < x.n_keys; i++) {
visit_expr(*x.m_keys[i]);
r += s;
r += ": ";
visit_expr(*x.m_values[i]);
r += s;
if (i < x.n_keys - 1) {
r += ", ";
}
}
r += "}";

s = r;
}

// An aggregate visitor for `ListConstant`, `TupleConstant` & `SetConstant`
void visit_AggregateConstant(size_t n_args, ASR::expr_t** m_args,
std::string opening_braces, std::string closing_braces) {
Expand Down Expand Up @@ -617,7 +636,6 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor<ASRToLpythonVisitor>
void visit_SetConstant(const ASR::SetConstant_t &x) {
visit_AggregateConstant(x.n_elements, x.m_elements, "{", "}");
}

};

Result<std::string> asr_to_python(Allocator& al, ASR::TranslationUnit_t &asr,
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/python-test_aggregate_constants-26c89d6.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "python-test_aggregate_constants-26c89d6",
"cmd": "lpython --no-color --show-python {infile}",
"infile": "tests/test_aggregate_constants.py",
"infile_hash": "e4f25c9787536c0ecc60a1d53b4bca5f250e2a4f3646b45565867e86",
"infile_hash": "6e225037304a9a1222b4c356b8cd1ffc5ed4a58bb7d6916c242c7b53",
"outfile": null,
"outfile_hash": null,
"stdout": "python-test_aggregate_constants-26c89d6.stdout",
"stdout_hash": "813b11b4ee92df0eebccb3031a1b73355957795a72b487115093c3ce",
"stdout_hash": "3d5fa791404534643f6f2a096b2bc1561cf6c03a78d060b79df4f17b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
30 changes: 30 additions & 0 deletions tests/reference/python-test_aggregate_constants-26c89d6.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def __main__global_init():
my_second_set = {1.100000, 2.500000, 6.800000}
my_third_set = {"a", "b", "a", "c"}
my_fourth_set = {(1, "a"), (2, "b"), (3, "c")}
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"}
my_fourth_dict = {1: (1.200000, 4.500000), 2: (3.600000, 9.200000)}
my_fifth_dict = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
my_sixth_dict = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
def __main__global_stmts():
print(my_first_list)
print(my_second_list)
Expand All @@ -28,19 +34,31 @@ def __main__global_stmts():
print(my_second_set)
print(my_third_set)
print(my_fourth_set)
print(my_first_dict)
print(my_second_dict)
print(my_third_dict)
print(my_fourth_dict)
print(my_fifth_dict)
print(my_sixth_dict)
fn()
def fn():
my_fifth_dict: dict[str, list[i32]]
my_fifth_list: list[set[str]]
my_first_dict: dict[str, i32]
my_first_list: list[i32]
my_first_set: set[i32]
my_first_tuple: tuple[i32, str, f64]
my_fourth_dict: dict[i32, tuple[f64, f64]]
my_fourth_list: list[list[f64]]
my_fourth_set: set[tuple[i32, str]]
my_fourth_tuple: tuple[set[str], str]
my_second_dict: dict[i32, f64]
my_second_list: list[str]
my_second_set: set[f64]
my_second_tuple: tuple[tuple[i32, str], str]
my_sixth_dict: dict[str, set[i32]]
my_sixth_list: list[tuple[i32, str]]
my_third_dict: dict[str, str]
my_third_list: list[list[i32]]
my_third_set: set[str]
my_third_tuple: tuple[list[str], str]
Expand Down Expand Up @@ -72,3 +90,15 @@ def fn():
print(my_third_set)
my_fourth_set = {(1, "a"), (2, "b"), (3, "c")}
print(my_fourth_set)
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)
my_fourth_dict = {1: (1.200000, 4.500000), 2: (3.600000, 9.200000)}
print(my_fourth_dict)
my_fifth_dict = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
print(my_fifth_dict)
my_sixth_dict = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
print(my_sixth_dict)
38 changes: 38 additions & 0 deletions tests/test_aggregate_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@
my_fourth_set: set[tuple[i32, str]] = {(1, "a"), (2, "b"), (3, "c")}
print(my_fourth_set)

# Dictionary
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)

my_fourth_dict: dict[i32, tuple[f64, f64]] = {1: (1.2, 4.5), 2: (3.6, 9.2)}
print(my_fourth_dict)

my_fifth_dict: dict[str, list[i32]] = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
print(my_fifth_dict)

my_sixth_dict: dict[str, set[i32]] = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
print(my_sixth_dict)

# Test codegen for local scope
def fn():
# List
Expand Down Expand Up @@ -94,4 +113,23 @@ def fn():
my_fourth_set: set[tuple[i32, str]] = {(1, "a"), (2, "b"), (3, "c")}
print(my_fourth_set)

# Dictionary
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)

my_fourth_dict: dict[i32, tuple[f64, f64]] = {1: (1.2, 4.5), 2: (3.6, 9.2)}
print(my_fourth_dict)

my_fifth_dict: dict[str, list[i32]] = {"list1": [1, 2, 3], "list2": [4, 5, 6]}
print(my_fifth_dict)

my_sixth_dict: dict[str, set[i32]] = {"set1": {1, 2, 1}, "set2": {4, 1, 2}}
print(my_sixth_dict)

fn()

0 comments on commit e378b4e

Please sign in to comment.