Skip to content

Commit 9f12bb1

Browse files
authored
Add deref literal syntax to the reader (#146)
1 parent 43d7bae commit 9f12bb1

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

basilisp/reader.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
_APPLY = symbol.symbol('apply', ns='basilisp.core')
4848
_CONCAT = symbol.symbol('concat', ns='basilisp.core')
49+
_DEREF = symbol.symbol('deref', ns='basilisp.core')
4950
_HASH_MAP = symbol.symbol('hash-map', ns='basilisp.core')
5051
_HASH_SET = symbol.symbol('hash-set', ns='basilisp.core')
5152
_LIST = symbol.symbol('list', ns='basilisp.core')
@@ -710,6 +711,14 @@ def _read_unquote(ctx: ReaderContext) -> LispForm:
710711
return llist.l(_UNQUOTE, next_form)
711712

712713

714+
def _read_deref(ctx: ReaderContext) -> LispForm:
715+
"""Read a derefed form from the input stream."""
716+
start = ctx.reader.advance()
717+
assert start == "@"
718+
next_form = _read_next(ctx)
719+
return llist.l(_DEREF, next_form)
720+
721+
713722
def _read_regex(ctx: ReaderContext) -> Pattern:
714723
"""Read a regex reader macro from the input stream."""
715724
s = _read_str(ctx)
@@ -826,6 +835,8 @@ def _read_next(ctx: ReaderContext) -> LispForm: # noqa: C901
826835
return _read_syntax_quoted(ctx)
827836
elif token == '~':
828837
return _read_unquote(ctx)
838+
elif token == '@':
839+
return _read_deref(ctx)
829840
elif token == '':
830841
return __EOF
831842
else:

tests/reader_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,12 @@ def test_function_reader_macro():
667667
read_str_first("#app/ermagrd [1 2 3]")
668668

669669

670+
def test_deref():
671+
assert read_str_first("@s") == llist.l(reader._DEREF, sym.symbol('s'))
672+
assert read_str_first("@ns/s") == llist.l(reader._DEREF, sym.symbol('s', ns='ns'))
673+
assert read_str_first("@(atom {})") == llist.l(reader._DEREF, llist.l(sym.symbol('atom'), lmap.Map.empty()))
674+
675+
670676
def test_regex_reader_literal():
671677
assert read_str_first('#"hi"') == langutil.regex_from_str("hi")
672678
assert read_str_first('#"\s"') == langutil.regex_from_str(r"\s")

0 commit comments

Comments
 (0)