From a2e29c7b2f28d9355f773269b67a6c08983fb14d Mon Sep 17 00:00:00 2001 From: advik Date: Sun, 14 Jul 2024 00:43:53 +0530 Subject: [PATCH 1/2] Add `set()` for creating an empty set --- integration_tests/test_set_constructor.py | 15 +++++++++++++++ src/lpython/semantics/python_ast_to_asr.cpp | 14 ++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 integration_tests/test_set_constructor.py diff --git a/integration_tests/test_set_constructor.py b/integration_tests/test_set_constructor.py new file mode 100644 index 0000000000..f62edad127 --- /dev/null +++ b/integration_tests/test_set_constructor.py @@ -0,0 +1,15 @@ +def test_empty_set(): + a: set[i32] = set() + assert len(a) == 0 + a.add(2) + a.remove(2) + a.add(3) + assert a.pop() == 3 + + b: set[str] = set() + + assert len(b) == 0 + b.add('a') + b.remove('a') + b.add('b') + assert b.pop() == 3 diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 4909e0461b..dd61ef6fdf 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -8622,6 +8622,20 @@ we will have to use something else. ASRUtils::type_to_str(type) + " type.", x.base.base.loc); } return; + } else if( call_name == "set" ) { + parse_args(x, args); + if (args.size() == 0) { + if( assign_asr_target != nullptr ) { + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, nullptr, 0, + ASRUtils::expr_type(assign_asr_target)); + } + else { + tmp = nullptr; + } + return ; + } + + throw SemanticError("set is only used for an empty set for now.", x.base.base.loc); } else if( call_name == "deepcopy" ) { parse_args(x, args); if( args.size() != 1 ) { From bab03bab57a71d409db76d050200b71730bed7c3 Mon Sep 17 00:00:00 2001 From: advik Date: Fri, 19 Jul 2024 18:51:24 +0530 Subject: [PATCH 2/2] Call test function --- integration_tests/test_set_constructor.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/integration_tests/test_set_constructor.py b/integration_tests/test_set_constructor.py index f62edad127..497819dbb7 100644 --- a/integration_tests/test_set_constructor.py +++ b/integration_tests/test_set_constructor.py @@ -13,3 +13,5 @@ def test_empty_set(): b.remove('a') b.add('b') assert b.pop() == 3 + +test_empty_set()