Skip to content

Commit 24a03ad

Browse files
Swaminath ShijuSwaminath Shiju
authored andcommitted
Added set creation from list
1 parent 7eb2bea commit 24a03ad

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8789,9 +8789,24 @@ 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
}
8797+
ASR::expr_t *arg = args[0].m_value;
8798+
ASR::ttype_t *type = ASRUtils::expr_type(arg);
8799+
if(ASR::is_a<ASR::ListConstant_t>(*arg)) {
8800+
ASR::ListConstant_t* list = ASR::down_cast<ASR::ListConstant_t>(arg);
8801+
ASR::expr_t **m_args = list->m_args;
8802+
size_t n_args = list->n_args;
87938803

8794-
throw SemanticError("set is only used for an empty set for now.", x.base.base.loc);
8804+
tmp = ASR::make_SetConstant_t(al, x.base.base.loc, m_args, n_args,
8805+
ASRUtils::expr_type(assign_asr_target));
8806+
return ;
8807+
}
8808+
throw SemanticError("set() accepts only list for now, got " +
8809+
ASRUtils::type_to_str(type) + " type.", x.base.base.loc);
87958810
} else if( call_name == "deepcopy" ) {
87968811
parse_args(x, args);
87978812
if( args.size() != 1 ) {

0 commit comments

Comments
 (0)