From 24a03add4a3f138b583873f8291e840c5e2c5ee2 Mon Sep 17 00:00:00 2001 From: Swaminath Shiju Date: Fri, 7 Feb 2025 10:36:28 +0530 Subject: [PATCH 1/3] Added set creation from list --- integration_tests/test_set_from_list.py | 14 ++++++++++++++ src/lpython/semantics/python_ast_to_asr.cpp | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 integration_tests/test_set_from_list.py diff --git a/integration_tests/test_set_from_list.py b/integration_tests/test_set_from_list.py new file mode 100644 index 0000000000..e06b4e40c1 --- /dev/null +++ b/integration_tests/test_set_from_list.py @@ -0,0 +1,14 @@ +from lpython import i32 + + +def test_set(): + s: set[i32] + s = set([1, 2, 2, 2, -1, 1, 1, 3]) + assert len(s) == 4 + + s2: set[str] + s2 = set(["a", "b", "b", "abc", "a"]) + assert len(s2) == 3 + + +test_set() diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 63c87dbe4a..3ac83b6a97 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -8789,9 +8789,24 @@ we will have to use something else. tmp = nullptr; } return ; + } else if (args.size() > 1) { + throw SemanticError("set() accepts only 1 argument for now, got " + + std::to_string(args.size()) + " arguments instead.", + x.base.base.loc); } + ASR::expr_t *arg = args[0].m_value; + ASR::ttype_t *type = ASRUtils::expr_type(arg); + if(ASR::is_a(*arg)) { + ASR::ListConstant_t* list = ASR::down_cast(arg); + ASR::expr_t **m_args = list->m_args; + size_t n_args = list->n_args; - throw SemanticError("set is only used for an empty set for now.", x.base.base.loc); + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, m_args, n_args, + ASRUtils::expr_type(assign_asr_target)); + return ; + } + throw SemanticError("set() accepts only list for now, got " + + ASRUtils::type_to_str(type) + " type.", x.base.base.loc); } else if( call_name == "deepcopy" ) { parse_args(x, args); if( args.size() != 1 ) { From 41f03e9df457e00eeee7375a113e271acef44517 Mon Sep 17 00:00:00 2001 From: swaminshiju Date: Mon, 10 Feb 2025 13:11:07 +0530 Subject: [PATCH 2/3] Fixed type mismatch --- integration_tests/CMakeLists.txt | 1 + src/lpython/semantics/python_ast_to_asr.cpp | 31 +++++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 5ad8a0074d..2c360fd51d 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -592,6 +592,7 @@ RUN(NAME test_set_len LABELS cpython llvm llvm_jit) RUN(NAME test_set_add LABELS cpython llvm llvm_jit) RUN(NAME test_set_remove LABELS cpython llvm llvm_jit) RUN(NAME test_set_discard LABELS cpython llvm llvm_jit) +RUN(NAME test_set_from_list LABELS cpython llvm llvm_jit) RUN(NAME test_set_clear LABELS cpython llvm) RUN(NAME test_set_pop LABELS cpython llvm) RUN(NAME test_global_set LABELS cpython llvm llvm_jit) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 3ac83b6a97..2ad5b3390a 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -8790,23 +8790,32 @@ we will have to use something else. } return ; } else if (args.size() > 1) { - throw SemanticError("set() accepts only 1 argument for now, got " + + throw SemanticError("set accepts only 1 argument for now, got " + std::to_string(args.size()) + " arguments instead.", x.base.base.loc); } + if ( assign_asr_target == nullptr ) { + throw SemanticError("set from list cannot be called without target type for now", x.base.base.loc); + } ASR::expr_t *arg = args[0].m_value; ASR::ttype_t *type = ASRUtils::expr_type(arg); - if(ASR::is_a(*arg)) { - ASR::ListConstant_t* list = ASR::down_cast(arg); - ASR::expr_t **m_args = list->m_args; - size_t n_args = list->n_args; - - tmp = ASR::make_SetConstant_t(al, x.base.base.loc, m_args, n_args, - ASRUtils::expr_type(assign_asr_target)); - return ; - } - throw SemanticError("set() accepts only list for now, got " + + if(!ASR::is_a(*arg)) { + throw SemanticError("set accepts only list for now, got " + ASRUtils::type_to_str(type) + " type.", x.base.base.loc); + } + ASR::ListConstant_t* list = ASR::down_cast(arg); + ASR::expr_t **m_args = list->m_args; + size_t n_args = list->n_args; + ASR::ttype_t* value_type = ASRUtils::get_contained_type(type); + ASR::ttype_t* target_type = ASRUtils::get_contained_type(ASRUtils::expr_type(assign_asr_target)); + if (!ASRUtils::check_equal_type(target_type, value_type)){ + std::string ltype = ASRUtils::type_to_str_python(target_type); + std::string rtype = ASRUtils::type_to_str_python(value_type); + throw SemanticError("type mismatch ('" + ltype + "' and '" + rtype + "')", x.base.base.loc); + } + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, m_args, n_args, + ASRUtils::expr_type(assign_asr_target)); + return ; } else if( call_name == "deepcopy" ) { parse_args(x, args); if( args.size() != 1 ) { From 9f3c21e403165b820095ce78b2811bef1680007c Mon Sep 17 00:00:00 2001 From: swamishiju Date: Wed, 12 Mar 2025 17:56:06 +0530 Subject: [PATCH 3/3] Update src/lpython/semantics/python_ast_to_asr.cpp Fixed error message Co-authored-by: Mohammed Ubaid Shaikh --- src/lpython/semantics/python_ast_to_asr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 2ad5b3390a..fac917eaf4 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -8800,7 +8800,7 @@ we will have to use something else. ASR::expr_t *arg = args[0].m_value; ASR::ttype_t *type = ASRUtils::expr_type(arg); if(!ASR::is_a(*arg)) { - throw SemanticError("set accepts only list for now, got " + + throw SemanticError("set accepts only list constant for now, got " + ASRUtils::type_to_str(type) + " type.", x.base.base.loc); } ASR::ListConstant_t* list = ASR::down_cast(arg);