Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added set creation from list #2822

Merged
merged 4 commits into from
Mar 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions integration_tests/test_set_from_list.py
Original file line number Diff line number Diff line change
@@ -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()
28 changes: 26 additions & 2 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
@@ -8789,9 +8789,33 @@ 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);
}

throw SemanticError("set is only used for an empty set for now.", 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<ASR::ListConstant_t>(*arg)) {
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<ASR::ListConstant_t>(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 ) {
Loading