Skip to content

Commit

Permalink
Support set literal unpacking
Browse files Browse the repository at this point in the history
Resolves   #695.
  • Loading branch information
evhub committed Dec 30, 2022
1 parent e5d86ef commit a4f286e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion coconut/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
def set_to_tuple(tokens):
"""Converts set literal tokens to tuples."""
internal_assert(len(tokens) == 1, "invalid set maker tokens", tokens)
if "comp" in tokens or "list" in tokens:
if "list" in tokens or "comp" in tokens or "testlist_star_expr" in tokens:
return "(" + tokens[0] + ")"
elif "test" in tokens:
return "(" + tokens[0] + ",)"
Expand Down
7 changes: 4 additions & 3 deletions coconut/compiler/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,9 +1172,10 @@ class Grammar(object):
set_m = fixto(CaselessLiteral("m"), "m")
set_letter = set_s | set_f | set_m
setmaker = Group(
addspace(new_namedexpr_test + comp_for)("comp")
| new_namedexpr_testlist_has_comma("list")
| new_namedexpr_test("test"),
(new_namedexpr_test + FollowedBy(rbrace))("test")
| (new_namedexpr_testlist_has_comma + FollowedBy(rbrace))("list")
| addspace(new_namedexpr_test + comp_for + FollowedBy(rbrace))("comp")
| (testlist_star_namedexpr + FollowedBy(rbrace))("testlist_star_expr"),
)
set_literal_ref = lbrace.suppress() + setmaker + rbrace.suppress()
set_letter_literal_ref = combine(set_letter + lbrace.suppress()) + Optional(setmaker) + rbrace.suppress()
Expand Down
4 changes: 3 additions & 1 deletion coconut/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
VERSION = "2.1.1"
VERSION_NAME = "The Spanish Inquisition"
# False for release, int >= 1 for develop
DEVELOP = 24
DEVELOP = 25
ALPHA = False # for pre releases rather than post releases

# -----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -138,6 +138,8 @@ def __reversed__(self):
return _coconut.reversed(self._xrange)
def __len__(self):
return _coconut.len(self._xrange)
def __bool__(self):
return _coconut.bool(self._xrange)
def __contains__(self, elem):
return elem in self._xrange
def __getitem__(self, index):
Expand Down
2 changes: 2 additions & 0 deletions coconut/tests/src/cocotest/agnostic/main.coco
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,8 @@ def main_test() -> bool:
assert m{1, 1, 2} >= m{1, 1, 2} > m{1, 2} > m{} >= m{}
assert m{1} != {1:1, 2:0}
assert not (m{1} == {1:1, 2:0})
assert s{1, 2, *(2, 3, 4), *(4, 5)} == s{1, 2, 3, 4, 5}
assert m{1, 2, *(2, 3, 4), *(4, 5)} == m{1, 2, 2, 3, 4, 4, 5}
return True

def test_asyncio() -> bool:
Expand Down

0 comments on commit a4f286e

Please sign in to comment.