You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reader.rst
+26-11Lines changed: 26 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
Reader
2
2
======
3
3
4
-
In most Lisps, the reader is the component responsible for reading in thet extual representation of the program into memory as data structures.
4
+
In most Lisps, the reader is the component responsible for reading in the textual representation of the program into memory as data structures.
5
5
Lisps are typically referred to as *homoiconic*, since that representation typically matches the syntax tree exactly in memory.
6
6
This is in contrast to a non-homoiconic language such as Java or Python, which typically parses a textual program into an abstract syntax tree which captures the *meaning* of the textual program, but not necessarily the structure.
7
7
@@ -26,11 +26,11 @@ Integers
26
26
27
27
basilisp.user=> 1
28
28
1
29
-
basilisp.user=> (builtins/type 1)
29
+
basilisp.user=> (python/type 1)
30
30
<class 'int'>
31
31
basilisp.user=> 1N
32
32
1
33
-
basilisp.user=> (builtins/type 1N)
33
+
basilisp.user=> (python/type 1N)
34
34
<class 'int'>
35
35
36
36
Integers are represented using numeric ``0-9`` and may be prefixed with any number of negative signs ``-``.
@@ -45,11 +45,11 @@ Floating Point
45
45
46
46
basilisp.user=> 1.0
47
47
1.0
48
-
basilisp.user=> (builtins/type 1.0)
48
+
basilisp.user=> (python/type 1.0)
49
49
<class 'float'>
50
50
basilisp.user=> 1M
51
51
1
52
-
basilisp.user=> (builtins/type 1M)
52
+
basilisp.user=> (python/type 1M)
53
53
<class 'decimal.Decimal'>
54
54
55
55
Floating point values are represented using ``0-9`` and a trailing decimal value, separated by a ``.`` character.
@@ -65,11 +65,11 @@ Complex
65
65
66
66
basilisp.user=> 1J
67
67
1J
68
-
basilisp.user=> (builtins/type 1J)
68
+
basilisp.user=> (python/type 1J)
69
69
<class 'complex'>
70
70
basilisp.user=> 1.0J
71
71
1J
72
-
basilisp.user=> (builtins/type 1.0J)
72
+
basilisp.user=> (python/type 1.0J)
73
73
<class 'complex'>
74
74
75
75
Basilisp includes support for complex literals to match the Python VM hosts it.
@@ -87,7 +87,7 @@ Strings
87
87
""
88
88
basilisp.user=> "this is a string"
89
89
"this is a string"
90
-
basilisp.user=> (builtins/type "")
90
+
basilisp.user=> (python/type "")
91
91
<class 'str'>
92
92
93
93
Strings are denoted as a series of characters enclosed by ``"`` quotation marks.
@@ -128,11 +128,11 @@ Boolean Values
128
128
129
129
basilisp.user=> true
130
130
true
131
-
basilisp.user=> (builtins/type true)
131
+
basilisp.user=> (python/type true)
132
132
<class 'bool'>
133
133
basilisp.user=> false
134
134
false
135
-
basilisp.user=> (builtins/type false)
135
+
basilisp.user=> (python/type false)
136
136
<class 'bool'>
137
137
138
138
The special values ``true`` and ``false`` correspond to Python's ``True`` and ``False`` respectively.
@@ -146,7 +146,7 @@ nil
146
146
147
147
basilisp.user=> nil
148
148
nil
149
-
basilisp.user=> (builtins/type nil)
149
+
basilisp.user=> (python/type nil)
150
150
<class 'NoneType'>
151
151
152
152
The special value ``nil`` correspond's to Python's ``None``.
@@ -270,6 +270,8 @@ Line Comments
270
270
Line comments are specified with the ``;`` character.
271
271
All of the text to the end of the line are ignored.
272
272
273
+
For a convenience in writing shell scripts with Basilisp, the standard *NIX `shebang <https://en.wikipedia.org/wiki/Shebang_(Unix)>` (``#!``) is also treated as a single-line comment.
274
+
273
275
.. _metadata:
274
276
275
277
Metadata
@@ -304,6 +306,7 @@ Reader macros are always dispatched using the ``#`` character.
304
306
305
307
* ``#'form`` is rewritten as ``(var form)``.
306
308
* ``#_form`` causes the reader to completely ignore ``form``.
309
+
* ``#!form`` is treated as a single-line comment (like ``;form``) as a convenience to support `shebangs <https://en.wikipedia.org/wiki/Shebang_(Unix)>` at the top of Basilisp scripts.
307
310
* ``#"str"`` causes the reader to interpret ``"str"`` as a regex and return a Python `re.pattern <https://docs.python.org/3/library/re.html>`_.
308
311
* ``#(...)`` causes the reader to interpret the contents of the list as an anonymous function. Anonymous functions specified in this way can name arguments using ``%1``, ``%2``, etc. and rest args as ``%&``. For anonymous functions with only one argument, ``%`` can be used in place of ``%1``.
309
312
@@ -323,6 +326,18 @@ Basilisp supports a few builtin data readers:
323
326
* ``#inst "2018-09-14T15:11:20.253-00:00"`` yields a Python `datetime <https://docs.python.org/3/library/datetime.html#datetime-objects>`_ object.
324
327
* ``#uuid "c3598794-20b4-48db-b76e-242f4405743f"`` yields a Python `UUID <https://docs.python.org/3/library/uuid.html#uuid.UUID>`_ object.
325
328
329
+
One of the benefits of choosing Basilisp is convenient built-in Python language interop.
330
+
However, the immutable data structures of Basilisp may not always play nicely with code written for (and expecting to be used by) other Python code.
331
+
Fortunately, Basilisp includes data readers for reading Python collection literals directly from the REPL or from Basilisp source.
332
+
333
+
Python literals can be read by prefixing existing Basilisp data structures with a ``#py`` data reader tag.
334
+
Python literals use the matching syntax to the corresponding Python data type, which does not always match the syntax for the same data type in Basilisp.
335
+
336
+
* ``#py []`` produces a Python `list <https://docs.python.org/3/library/stdtypes.html#list>` type.
337
+
* ``#py ()`` produces a Python `tuple <https://docs.python.org/3/library/stdtypes.html#tuple>` type.
338
+
* ``#py {}`` produces a Python `dict <https://docs.python.org/3/library/stdtypes.html#dict>` type.
339
+
* ``#py #{}`` produces a Python `set <https://docs.python.org/3/library/stdtypes.html#set>` type.
0 commit comments