From 9e4c9cdf38785c0166e515ef6726fa672e07065e Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Thu, 9 May 2024 21:56:12 +0530 Subject: [PATCH 1/5] Add an aggregate visitor for `ListConstant`, `TupleConstant` and `SetConstant` --- src/libasr/codegen/asr_to_python.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libasr/codegen/asr_to_python.cpp b/src/libasr/codegen/asr_to_python.cpp index 204880be21..396b38ee58 100644 --- a/src/libasr/codegen/asr_to_python.cpp +++ b/src/libasr/codegen/asr_to_python.cpp @@ -619,6 +619,34 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor 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) { + std::string r = ""; + r += opening_braces; + for (size_t i = 0; i < n_args; i++) { + this->visit_expr(*m_args[i]); + r.append(s); + if (i < n_args - 1) { + r.append(", "); + } + } + r += closing_braces; + s = r; + } + + void visit_ListConstant(const ASR::ListConstant_t &x) { + visit_AggregateConstant(x.n_args, x.m_args, "[", "]"); + } + + void visit_TupleConstant(const ASR::TupleConstant_t &x) { + visit_AggregateConstant(x.n_elements, x.m_elements, "(", ")"); + } + + void visit_SetConstant(const ASR::SetConstant_t &x) { + visit_AggregateConstant(x.n_elements, x.m_elements, "{", "}"); + } + }; Result asr_to_python(Allocator& al, ASR::TranslationUnit_t &asr, From 1c8a7e657b40e2977b94d96c21bfd91dc8e5a7ba Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Thu, 9 May 2024 22:00:14 +0530 Subject: [PATCH 2/5] Add type annotations for `Tuple` and `Set` variables --- src/libasr/codegen/asr_to_python.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libasr/codegen/asr_to_python.cpp b/src/libasr/codegen/asr_to_python.cpp index 396b38ee58..e2ce6b7963 100644 --- a/src/libasr/codegen/asr_to_python.cpp +++ b/src/libasr/codegen/asr_to_python.cpp @@ -150,6 +150,15 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor } case ASR::ttypeType::Logical : { r = "bool"; break; + } case ASR::ttypeType::List : { + r = ASRUtils::type_to_str_python(t); + break; + } case ASR::ttypeType::Tuple : { + r = ASRUtils::type_to_str_python(t); + break; + }case ASR::ttypeType::Set : { + r = ASRUtils::type_to_str_python(t); + break; } default : { throw LCompilersException("The type `" + ASRUtils::type_to_str_python(t) + "` is not handled yet"); From e70a3f8e3f53ce57ed3e9e4f98d1e1dd6d980ce8 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Thu, 9 May 2024 22:27:23 +0530 Subject: [PATCH 3/5] Tests: Add tests and update references --- tests/data_structure_visitor.py | 97 +++++++++++++++++++ ...python-data_structure_visitor-47e953a.json | 13 +++ ...thon-data_structure_visitor-47e953a.stdout | 74 ++++++++++++++ tests/tests.toml | 4 + 4 files changed, 188 insertions(+) create mode 100644 tests/data_structure_visitor.py create mode 100644 tests/reference/python-data_structure_visitor-47e953a.json create mode 100644 tests/reference/python-data_structure_visitor-47e953a.stdout diff --git a/tests/data_structure_visitor.py b/tests/data_structure_visitor.py new file mode 100644 index 0000000000..76f203a7be --- /dev/null +++ b/tests/data_structure_visitor.py @@ -0,0 +1,97 @@ +from lpython import i32, f64 + +# Test codegen for global scope + +# List +my_first_list: list[i32] = [1, 2, 3 , 4] +print(my_first_list) + +my_second_list: list[str] = ["a", "b", "c", "d"] +print(my_second_list) + +my_third_list: list[list[i32]] = [[1, 2], [3, 4], [5, 6]] +print(my_third_list) + +my_fourth_list: list[list[f64]] = [[1.0, 2.2], [3.6, 4.9], [5.1, 6.3]] +print(my_fourth_list) + +my_fifth_list: list[set[str]] = [{"a", "b"}, {"c", "d"}] +print(my_fifth_list) + +my_sixth_list: list[tuple[i32, str]] = [(1, "a"), (2, "b")] +print(my_sixth_list) + +# Tuple +my_first_tuple: tuple[i32, str, f64] = (1, "hello", 2.4) +print(my_first_tuple) + +my_second_tuple: tuple[tuple[i32, str], str] = ((1, "hello"), "world") +print(my_second_tuple) + +my_third_tuple: tuple[list[str], str] = (["hello", "world"], "world") +print(my_third_tuple) + +my_fourth_tuple: tuple[set[str], str] = ({"hello", "world"}, "world") +print(my_fourth_tuple) + +# Set +my_first_set: set[i32] = {1, 2, 3, 2, 4} +print(my_first_set) + +my_second_set: set[f64] = {1.1, 2.5, 6.8} +print(my_second_set) + +my_third_set: set[str] = {"a", "b", "a", "c"} +print(my_third_set) + +my_fourth_set: set[tuple[i32, str]] = {(1, "a"), (2, "b"), (3, "c")} +print(my_fourth_set) + +# Test codegen for local scope +def fn(): + # List + my_first_list: list[i32] = [1, 2, 3 , 4] + print(my_first_list) + + my_second_list: list[str] = ["a", "b", "c", "d"] + print(my_second_list) + + my_third_list: list[list[i32]] = [[1, 2], [3, 4], [5, 6]] + print(my_third_list) + + my_fourth_list: list[list[f64]] = [[1.0, 2.2], [3.6, 4.9], [5.1, 6.3]] + print(my_fourth_list) + + my_fifth_list: list[set[str]] = [{"a", "b"}, {"c", "d"}] + print(my_fifth_list) + + my_sixth_list: list[tuple[i32, str]] = [(1, "a"), (2, "b")] + print(my_sixth_list) + + # Tuple + my_first_tuple: tuple[i32, str, f64] = (1, "hello", 2.4) + print(my_first_tuple) + + my_second_tuple: tuple[tuple[i32, str], str] = ((1, "hello"), "world") + print(my_second_tuple) + + my_third_tuple: tuple[list[str], str] = (["hello", "world"], "world") + print(my_third_tuple) + + my_fourth_tuple: tuple[set[str], str] = ({"hello", "world"}, "world") + print(my_fourth_tuple) + + # Set + my_first_set: set[i32] = {1, 2, 3, 2, 4} + print(my_first_set) + + my_second_set: set[f64] = {1.1, 2.5, 6.8} + print(my_second_set) + + my_third_set: set[str] = {"a", "b", "a", "c"} + print(my_third_set) + + my_fourth_set: set[tuple[i32, str]] = {(1, "a"), (2, "b"), (3, "c")} + print(my_fourth_set) + +fn() \ No newline at end of file diff --git a/tests/reference/python-data_structure_visitor-47e953a.json b/tests/reference/python-data_structure_visitor-47e953a.json new file mode 100644 index 0000000000..b9d5ebab54 --- /dev/null +++ b/tests/reference/python-data_structure_visitor-47e953a.json @@ -0,0 +1,13 @@ +{ + "basename": "python-data_structure_visitor-47e953a", + "cmd": "lpython --no-color --show-python {infile}", + "infile": "tests/data_structure_visitor.py", + "infile_hash": "e4f25c9787536c0ecc60a1d53b4bca5f250e2a4f3646b45565867e86", + "outfile": null, + "outfile_hash": null, + "stdout": "python-data_structure_visitor-47e953a.stdout", + "stdout_hash": "813b11b4ee92df0eebccb3031a1b73355957795a72b487115093c3ce", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/python-data_structure_visitor-47e953a.stdout b/tests/reference/python-data_structure_visitor-47e953a.stdout new file mode 100644 index 0000000000..102dd1e4d0 --- /dev/null +++ b/tests/reference/python-data_structure_visitor-47e953a.stdout @@ -0,0 +1,74 @@ +def __main__global_init(): + my_first_list = [1, 2, 3, 4] + my_second_list = ["a", "b", "c", "d"] + my_third_list = [[1, 2], [3, 4], [5, 6]] + my_fourth_list = [[1.000000, 2.200000], [3.600000, 4.900000], [5.100000, 6.300000]] + my_fifth_list = [{"a", "b"}, {"c", "d"}] + my_sixth_list = [(1, "a"), (2, "b")] + my_first_tuple = (1, "hello", 2.400000) + my_second_tuple = ((1, "hello"), "world") + my_third_tuple = (["hello", "world"], "world") + my_fourth_tuple = ({"hello", "world"}, "world") + my_first_set = {1, 2, 3, 2, 4} + 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")} +def __main__global_stmts(): + print(my_first_list) + print(my_second_list) + print(my_third_list) + print(my_fourth_list) + print(my_fifth_list) + print(my_sixth_list) + print(my_first_tuple) + print(my_second_tuple) + print(my_third_tuple) + print(my_fourth_tuple) + print(my_first_set) + print(my_second_set) + print(my_third_set) + print(my_fourth_set) + fn() +def fn(): + my_fifth_list: list[set[str]] + my_first_list: list[i32] + my_first_set: set[i32] + my_first_tuple: tuple[i32, str, f64] + my_fourth_list: list[list[f64]] + my_fourth_set: set[tuple[i32, str]] + my_fourth_tuple: tuple[set[str], str] + my_second_list: list[str] + my_second_set: set[f64] + my_second_tuple: tuple[tuple[i32, str], str] + my_sixth_list: list[tuple[i32, str]] + my_third_list: list[list[i32]] + my_third_set: set[str] + my_third_tuple: tuple[list[str], str] + my_first_list = [1, 2, 3, 4] + print(my_first_list) + my_second_list = ["a", "b", "c", "d"] + print(my_second_list) + my_third_list = [[1, 2], [3, 4], [5, 6]] + print(my_third_list) + my_fourth_list = [[1.000000, 2.200000], [3.600000, 4.900000], [5.100000, 6.300000]] + print(my_fourth_list) + my_fifth_list = [{"a", "b"}, {"c", "d"}] + print(my_fifth_list) + my_sixth_list = [(1, "a"), (2, "b")] + print(my_sixth_list) + my_first_tuple = (1, "hello", 2.400000) + print(my_first_tuple) + my_second_tuple = ((1, "hello"), "world") + print(my_second_tuple) + my_third_tuple = (["hello", "world"], "world") + print(my_third_tuple) + my_fourth_tuple = ({"hello", "world"}, "world") + print(my_fourth_tuple) + my_first_set = {1, 2, 3, 2, 4} + print(my_first_set) + my_second_set = {1.100000, 2.500000, 6.800000} + print(my_second_set) + my_third_set = {"a", "b", "a", "c"} + print(my_third_set) + my_fourth_set = {(1, "a"), (2, "b"), (3, "c")} + print(my_fourth_set) diff --git a/tests/tests.toml b/tests/tests.toml index 7e9b1d43d0..59f0790054 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -51,6 +51,10 @@ filename = "dictionary1.py" ast = true asr = true +[[test]] +filename = "data_structure_visitor.py" +python = true + [[test]] filename = "expr1.py" ast = true From abc1652c3dd61f0f3f68889844ed6c9fd495b30a Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Thu, 9 May 2024 22:41:46 +0530 Subject: [PATCH 4/5] Tests: Rename test and update references --- ...3a.json => python-test_aggregate_constants-26c89d6.json} | 6 +++--- ...tdout => python-test_aggregate_constants-26c89d6.stdout} | 0 ...ata_structure_visitor.py => test_aggregate_constants.py} | 0 tests/tests.toml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename tests/reference/{python-data_structure_visitor-47e953a.json => python-test_aggregate_constants-26c89d6.json} (65%) rename tests/reference/{python-data_structure_visitor-47e953a.stdout => python-test_aggregate_constants-26c89d6.stdout} (100%) rename tests/{data_structure_visitor.py => test_aggregate_constants.py} (100%) diff --git a/tests/reference/python-data_structure_visitor-47e953a.json b/tests/reference/python-test_aggregate_constants-26c89d6.json similarity index 65% rename from tests/reference/python-data_structure_visitor-47e953a.json rename to tests/reference/python-test_aggregate_constants-26c89d6.json index b9d5ebab54..56e7270619 100644 --- a/tests/reference/python-data_structure_visitor-47e953a.json +++ b/tests/reference/python-test_aggregate_constants-26c89d6.json @@ -1,11 +1,11 @@ { - "basename": "python-data_structure_visitor-47e953a", + "basename": "python-test_aggregate_constants-26c89d6", "cmd": "lpython --no-color --show-python {infile}", - "infile": "tests/data_structure_visitor.py", + "infile": "tests/test_aggregate_constants.py", "infile_hash": "e4f25c9787536c0ecc60a1d53b4bca5f250e2a4f3646b45565867e86", "outfile": null, "outfile_hash": null, - "stdout": "python-data_structure_visitor-47e953a.stdout", + "stdout": "python-test_aggregate_constants-26c89d6.stdout", "stdout_hash": "813b11b4ee92df0eebccb3031a1b73355957795a72b487115093c3ce", "stderr": null, "stderr_hash": null, diff --git a/tests/reference/python-data_structure_visitor-47e953a.stdout b/tests/reference/python-test_aggregate_constants-26c89d6.stdout similarity index 100% rename from tests/reference/python-data_structure_visitor-47e953a.stdout rename to tests/reference/python-test_aggregate_constants-26c89d6.stdout diff --git a/tests/data_structure_visitor.py b/tests/test_aggregate_constants.py similarity index 100% rename from tests/data_structure_visitor.py rename to tests/test_aggregate_constants.py diff --git a/tests/tests.toml b/tests/tests.toml index 59f0790054..dd32e96a8d 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -52,7 +52,7 @@ ast = true asr = true [[test]] -filename = "data_structure_visitor.py" +filename = "test_aggregate_constants.py" python = true [[test]] From ad404f9f874b0069e21dd79c0e2cfcea73eec906 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Thu, 9 May 2024 22:50:47 +0530 Subject: [PATCH 5/5] Remove unnecessary function `std::string get_type()` --- src/libasr/codegen/asr_to_python.cpp | 40 +--------------------------- 1 file changed, 1 insertion(+), 39 deletions(-) diff --git a/src/libasr/codegen/asr_to_python.cpp b/src/libasr/codegen/asr_to_python.cpp index e2ce6b7963..8600a03ed7 100644 --- a/src/libasr/codegen/asr_to_python.cpp +++ b/src/libasr/codegen/asr_to_python.cpp @@ -129,44 +129,6 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor } } - std::string get_type(const ASR::ttype_t *t) { - std::string r = ""; - switch (t->type) { - case ASR::ttypeType::Integer : { - r += "i"; - r += std::to_string(ASRUtils::extract_kind_from_ttype_t(t)*8); - break; - } case ASR::ttypeType::Real : { - r += "f"; - r += std::to_string(ASRUtils::extract_kind_from_ttype_t(t)*8); - break; - } case ASR::ttypeType::Complex : { - r += "c"; - r += std::to_string(ASRUtils::extract_kind_from_ttype_t(t)*8); - break; - } case ASR::ttypeType::Character : { - r = "str"; - break; - } case ASR::ttypeType::Logical : { - r = "bool"; - break; - } case ASR::ttypeType::List : { - r = ASRUtils::type_to_str_python(t); - break; - } case ASR::ttypeType::Tuple : { - r = ASRUtils::type_to_str_python(t); - break; - }case ASR::ttypeType::Set : { - r = ASRUtils::type_to_str_python(t); - break; - } default : { - throw LCompilersException("The type `" - + ASRUtils::type_to_str_python(t) + "` is not handled yet"); - } - } - return r; - } - void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { std::string r = ""; @@ -254,7 +216,7 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor std::string r = indent; r += x.m_name; r += ": "; - r += get_type(x.m_type); + r += ASRUtils::type_to_str_python(x.m_type); r += "\n"; s = r; }