Skip to content

Commit 22ea159

Browse files
swamishijuSwaminath Shijuubaidsk
authored
Added set creation from list (#2822)
* Added set creation from list * Fixed type mismatch * Update src/lpython/semantics/python_ast_to_asr.cpp Fixed error message Co-authored-by: Mohammed Ubaid Shaikh <shaikhubaid769@gmail.com> --------- Co-authored-by: Swaminath Shiju <swaminathshiju@Swaminaths-MacBook-Air.local> Co-authored-by: Mohammed Ubaid Shaikh <shaikhubaid769@gmail.com>
1 parent 8883d14 commit 22ea159

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ RUN(NAME test_set_len LABELS cpython llvm llvm_jit)
592592
RUN(NAME test_set_add LABELS cpython llvm llvm_jit)
593593
RUN(NAME test_set_remove LABELS cpython llvm llvm_jit)
594594
RUN(NAME test_set_discard LABELS cpython llvm llvm_jit)
595+
RUN(NAME test_set_from_list LABELS cpython llvm llvm_jit)
595596
RUN(NAME test_set_clear LABELS cpython llvm)
596597
RUN(NAME test_set_pop LABELS cpython llvm)
597598
RUN(NAME test_global_set LABELS cpython llvm llvm_jit)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from lpython import i32
2+
3+
4+
def test_set():
5+
s: set[i32]
6+
s = set([1, 2, 2, 2, -1, 1, 1, 3])
7+
assert len(s) == 4
8+
9+
s2: set[str]
10+
s2 = set(["a", "b", "b", "abc", "a"])
11+
assert len(s2) == 3
12+
13+
14+
test_set()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8789,9 +8789,33 @@ we will have to use something else.
87898789
tmp = nullptr;
87908790
}
87918791
return ;
8792+
} else if (args.size() > 1) {
8793+
throw SemanticError("set accepts only 1 argument for now, got " +
8794+
std::to_string(args.size()) + " arguments instead.",
8795+
x.base.base.loc);
87928796
}
8793-
8794-
throw SemanticError("set is only used for an empty set for now.", x.base.base.loc);
8797+
if ( assign_asr_target == nullptr ) {
8798+
throw SemanticError("set from list cannot be called without target type for now", x.base.base.loc);
8799+
}
8800+
ASR::expr_t *arg = args[0].m_value;
8801+
ASR::ttype_t *type = ASRUtils::expr_type(arg);
8802+
if(!ASR::is_a<ASR::ListConstant_t>(*arg)) {
8803+
throw SemanticError("set accepts only list constant for now, got " +
8804+
ASRUtils::type_to_str(type) + " type.", x.base.base.loc);
8805+
}
8806+
ASR::ListConstant_t* list = ASR::down_cast<ASR::ListConstant_t>(arg);
8807+
ASR::expr_t **m_args = list->m_args;
8808+
size_t n_args = list->n_args;
8809+
ASR::ttype_t* value_type = ASRUtils::get_contained_type(type);
8810+
ASR::ttype_t* target_type = ASRUtils::get_contained_type(ASRUtils::expr_type(assign_asr_target));
8811+
if (!ASRUtils::check_equal_type(target_type, value_type)){
8812+
std::string ltype = ASRUtils::type_to_str_python(target_type);
8813+
std::string rtype = ASRUtils::type_to_str_python(value_type);
8814+
throw SemanticError("type mismatch ('" + ltype + "' and '" + rtype + "')", x.base.base.loc);
8815+
}
8816+
tmp = ASR::make_SetConstant_t(al, x.base.base.loc, m_args, n_args,
8817+
ASRUtils::expr_type(assign_asr_target));
8818+
return ;
87958819
} else if( call_name == "deepcopy" ) {
87968820
parse_args(x, args);
87978821
if( args.size() != 1 ) {

0 commit comments

Comments
 (0)