Skip to content

Commit

Permalink
Add set() for creating an empty set
Browse files Browse the repository at this point in the history
  • Loading branch information
advikkabra authored and czgdp1807 committed Jul 21, 2024
1 parent e8d244e commit a2e29c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions integration_tests/test_set_constructor.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit a2e29c7

Please sign in to comment.