aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/syntax.html
blob: 431156d34943f6bfe16a05849be9be19340a5148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<head>
  <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
  <link href="../style.css" rel="stylesheet"/>
  <title>BQN: Syntax overview</title>
</head>
<div class="nav">(<a href="https://github.com/mlochbaum/BQN">github</a>) / <a href="../index.html">BQN</a> / <a href="index.html">doc</a></div>
<h1 id="syntax-overview"><a class="header" href="#syntax-overview">Syntax overview</a></h1>
<p>BQN syntax consists of expressions where computation is done, with a little organizing structure around them like assignment, functions, and list notation. Expressions are where the programmer is in control, so the design tries to do as much as possible with them before introducing special syntax.</p>
<h2 id="special-glyphs"><a class="header" href="#special-glyphs">Special glyphs</a></h2>
<p>The following glyphs are used for BQN syntax. <a href="primitive.html">Primitives</a> (built-in functions and modifiers) are not listed in this table, and have their own page. Digits, characters, and the underscore <code><span class='Modifier2'>_</span></code> are used for numbers and variable names.</p>
<table>
<thead>
<tr>
<th>Glyph(s)</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Comment'>#</span></code></td>
<td><a href="#comments">Comment</a></td>
</tr>
<tr>
<td><code><span class='String'>'&quot;</span></code></td>
<td><a href="#constants">Character or string literal</a></td>
</tr>
<tr>
<td><code><span class='String'>@</span></code></td>
<td><a href="#constants">Null character</a></td>
</tr>
<tr>
<td><code><span class='Number'>¯∞π</span></code></td>
<td><a href="#constants">Used in numeric literals</a></td>
</tr>
<tr>
<td><code><span class='Nothing'>·</span></code></td>
<td><a href="expression.html#nothing">Nothing</a></td>
</tr>
<tr>
<td><code><span class='Paren'>()</span></code></td>
<td><a href="expression.html#parentheses">Expression grouping</a></td>
</tr>
<tr>
<td><code><span class='Gets'></span></code></td>
<td><a href="expression.html#assignment">Define</a></td>
</tr>
<tr>
<td><code><span class='Gets'></span></code></td>
<td><a href="namespace.html#exports">Export</a></td>
</tr>
<tr>
<td><code><span class='Gets'></span></code></td>
<td><a href="expression.html#assignment">Change</a></td>
</tr>
<tr>
<td><code><span class='Separator'>⋄,</span></code> or newline</td>
<td>Statement or element <a href="#separators">separator</a></td>
</tr>
<tr>
<td><code><span class='Bracket'>⟨⟩</span></code></td>
<td><a href="#list-and-array-notation">List</a></td>
</tr>
<tr>
<td><code><span class='Bracket'>[]</span></code></td>
<td><a href="#list-and-array-notation">Array</a></td>
</tr>
<tr>
<td><code><span class='Ligature'></span></code></td>
<td><a href="#list-and-array-notation">Strand</a> (lightweight list syntax)</td>
</tr>
<tr>
<td><code><span class='Brace'>{}</span></code></td>
<td><a href="#blocks">Block</a> such as a function definition</td>
</tr>
<tr>
<td><code><span class='Head'>:</span></code></td>
<td><a href="block.html#block-headers">Block header</a></td>
</tr>
<tr>
<td><code><span class='Head'>;</span></code></td>
<td><a href="block.html#multiple-bodies">Block body separator</a></td>
</tr>
<tr>
<td><code><span class='Head'>?</span></code></td>
<td><a href="block.html#predicates">Predicate</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕨</span><span class='Function'>𝕎</span></code></td>
<td><a href="#blocks">Left argument</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕩</span><span class='Function'>𝕏</span></code></td>
<td><a href="#blocks">Right argument</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕤</span><span class='Function'>𝕊</span></code></td>
<td><a href="#blocks">Function self-reference</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕗</span><span class='Function'>𝔽</span></code></td>
<td><a href="#blocks">Left operand of a modifier</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕘</span><span class='Function'>𝔾</span></code></td>
<td><a href="#blocks">Right operand of a 2-modifier</a></td>
</tr>
<tr>
<td><code><span class='Value'>𝕣</span></code></td>
<td><a href="#blocks">Modifier self-reference</a></td>
</tr>
</tbody>
</table>
<h2 id="comments"><a class="header" href="#comments">Comments</a></h2>
<p>A comment starts with a <code><span class='Comment'>#</span></code> that isn't part of a character or string literal, and continues to the end of the line.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=JyMnIC0gMSAgI1RoaXMgaXMgdGhlIGNvbW1lbnQ=">↗️</a><pre>    <span class='String'>'#'</span> <span class='Function'>-</span> <span class='Number'>1</span>  <span class='Comment'>#This is the comment
</span>'"'
</pre>
<h2 id="constants"><a class="header" href="#constants">Constants</a></h2>
<p>BQN has single-token notation for numbers, strings, and characters.</p>
<p><a href="types.html#numbers">Numbers</a> are written as decimals, allowing <code><span class='Number'>¯</span></code> for the negative sign (because <code><span class='Function'>-</span></code> is a function) and <code><span class='Value'>e</span></code> or <code><span class='Function'>E</span></code> for scientific notation. They must have digits before and after the decimal point (so, <code><span class='Number'>0.5</span></code> instead of <code><span class='Number'>.5</span></code>), and any exponent must be an integer. Two special numbers <code><span class='Number'></span></code> and <code><span class='Number'>π</span></code> are supported, possibly with a minus sign. If complex numbers are supported (no implementation to date has them), then they can be written with the components separated by <code><span class='Value'>i</span></code> or <code><span class='Function'>I</span></code>.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oIMKvz4Ag4ouEIDAuNSDii4QgNWXCrzEg4ouEIDEuNUUzIOKLhCDiiJ4g4p+pICAgIyBBIGxpc3Qgb2YgbnVtYmVycw==">↗️</a><pre>    <span class='Bracket'></span> <span class='Number'>¯π</span> <span class='Separator'></span> <span class='Number'>0.5</span> <span class='Separator'></span> <span class='Number'>5e¯1</span> <span class='Separator'></span> <span class='Number'>1.5E3</span> <span class='Separator'></span> <span class='Number'></span> <span class='Bracket'></span>   <span class='Comment'># A list of numbers
</span>⟨ ¯3.141592653589793 0.5 0.5 1500 ∞ ⟩
</pre>
<p>Strings—lists of characters—are written with double quotes <code><span class='String'>&quot;&quot;</span></code>, and <a href="types.html#characters">characters</a> with single quotes <code><span class='String'>''</span></code> with a single character in between. Only one character ever needs to be escaped: a double quote in a string is written twice. So <code><span class='String'>&quot;&quot;&quot;&quot;</span></code> is a one-character string of <code><span class='String'>&quot;</span></code>, and if two string literals are next to each other, they have to be separated by a space. Character literals don't have even one escape, as the length is already known. Other than the double quote, character and string literals can contain anything: newlines, null characters, or any other Unicode.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omgwqgg4p+oICJzdHIiIOKLhCAicyd0IiJyIiDii4QgJ2MnIOKLhCAnJycg4ouEICciJyDin6kgICAjICIiIGlzIGFuIGVzY2FwZQoK4omhwqgg4p+oICJhIiDii4QgJ2EnIOKfqSAgICMgQSBzdHJpbmcgaXMgYW4gYXJyYXkgYnV0IGEgY2hhcmFjdGVyIGlzbid0">↗️</a><pre>    <span class='Function'></span><span class='Modifier'>¨</span> <span class='Bracket'></span> <span class='String'>&quot;str&quot;</span> <span class='Separator'></span> <span class='String'>&quot;s't&quot;&quot;r&quot;</span> <span class='Separator'></span> <span class='String'>'c'</span> <span class='Separator'></span> <span class='String'>'''</span> <span class='Separator'></span> <span class='String'>'&quot;'</span> <span class='Bracket'></span>   <span class='Comment'># &quot;&quot; is an escape
</span>⟨ 3 5 1 1 1 ⟩

    <span class='Function'></span><span class='Modifier'>¨</span> <span class='Bracket'></span> <span class='String'>&quot;a&quot;</span> <span class='Separator'></span> <span class='String'>'a'</span> <span class='Bracket'></span>   <span class='Comment'># A string is an array but a character isn't
</span>⟨ 1 0 ⟩
</pre>
<p>But including a null character in your source code is probably not a great idea for other reasons. The null character (code point 0) has a dedicated literal representation <code><span class='String'>@</span></code>. Null can be used with <a href="arithmetic.html#character-arithmetic">character arithmetic</a> to directly convert between characters and numeric code points, which among many other uses allows tricky characters to be entered by code point: for example, a non-breaking space is <code><span class='String'>@</span><span class='Function'>+</span><span class='Number'>160</span></code>.</p>
<h2 id="expressions"><a class="header" href="#expressions">Expressions</a></h2>
<p><em><a href="expression.html">Full documentation</a></em></p>
<p>BQN expressions are composed of subjects, functions, and modifiers, with parentheses to group parts into subexpressions. <a href="ops.html#functions">Functions</a> can be applied to subjects or grouped into <a href="train.html">trains</a>, while <a href="ops.html#modifiers">modifiers</a> can be applied to subjects or functions. The most important kinds of application are:</p>
<table>
<thead>
<tr>
<th>example</th>
<th>left</th>
<th>main</th>
<th>right</th>
<th>output</th>
<th>name</th>
<th>binding</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Function'></span> <span class='Number'>10</span></code></td>
<td><code><span class='Value'>w</span><span class='Head'>?</span></code></td>
<td><code><span class='Function'>F</span></code></td>
<td><code><span class='Value'>x</span></code></td>
<td>Subject</td>
<td>Function</td>
<td>RtL, looser</td>
</tr>
<tr>
<td><code><span class='Function'>+</span> <span class='Function'></span> <span class='Function'>-</span></code></td>
<td><code><span class='Function'>F</span><span class='Head'>?</span></code></td>
<td><code><span class='Function'>G</span></code></td>
<td><code><span class='Function'>H</span></code></td>
<td>Function</td>
<td>Train</td>
<td></td>
</tr>
<tr>
<td><code><span class='Function'>×</span><span class='Modifier'>´</span></code></td>
<td><code><span class='Function'>F</span></code></td>
<td><code><span class='Modifier'>_m</span></code></td>
<td></td>
<td>Function</td>
<td>1-Modifier</td>
<td>LtR, tighter</td>
</tr>
<tr>
<td><code><span class='Number'>2</span><span class='Modifier2'></span><span class='Function'>|</span></code></td>
<td><code><span class='Function'>F</span></code></td>
<td><code><span class='Modifier2'>_c_</span></code></td>
<td><code><span class='Function'>G</span></code></td>
<td>Function</td>
<td>2-Modifier</td>
<td></td>
</tr>
</tbody>
</table>
<p>The four <a href="expression.html#syntactic-role">roles</a> (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's <a href="types.html">type</a> doesn't have to correspond to its role, and can even change from one evaluation to another. An expression's role is determined entirely by its source code, so it's fixed.</p>
<p><a href="expression.html#assignment">Assignment</a> arrows <code><span class='Gets'></span></code>, <code><span class='Gets'></span></code>, and <code><span class='Gets'></span></code> store expression results in variables: <code><span class='Gets'></span></code> and <code><span class='Gets'></span></code> create new variables while <code><span class='Gets'></span></code> modifies existing ones. The general format is <code><span class='Function'>Name</span> <span class='Gets'></span> <span class='Function'>Value</span></code>, where the two sides have the same role. Additionally, <code><span class='Value'>lhs</span> <span class='Function'>F</span><span class='Gets'></span> <span class='Value'>rhs</span></code> is a shortened form of <code><span class='Value'>lhs</span> <span class='Gets'></span> <span class='Value'>lhs</span> <span class='Function'>F</span> <span class='Value'>rhs</span></code> and <code><span class='Value'>lhs</span> <span class='Function'>F</span><span class='Gets'></span></code> expands to <code><span class='Value'>lhs</span> <span class='Gets'></span> <span class='Function'>F</span> <span class='Value'>lhs</span></code>.</p>
<p>The double arrow <code><span class='Gets'></span></code> is used for functionality relating to <a href="namespace.html">namespaces</a>. It has a few purposes: exporting assignment <code><span class='Value'>name</span><span class='Gets'></span><span class='Value'>value</span></code>, plain export <code><span class='Value'>name</span><span class='Gets'></span></code>, and aliasing <code><span class='Bracket'></span><span class='Value'>alias</span><span class='Gets'></span><span class='Value'>field</span><span class='Bracket'></span><span class='Gets'></span><span class='Value'>namespace</span></code>. A block that uses it for export returns a namespace rather than the result of its last statement. The other namespace-related bit of syntax is field access <code><span class='Value'>ns.field</span></code>.</p>
<h2 id="arrays-and-blocks"><a class="header" href="#arrays-and-blocks">Arrays and blocks</a></h2>
<p>Arrays and code blocks can both be represented as sequences of expressions in source code. There are paired bracket representations, using <code><span class='Bracket'>⟨⟩</span></code> for lists, <code><span class='Bracket'>[]</span></code> for arrays, and <code><span class='Brace'>{}</span></code> for blocks, as well as a shortcut &quot;stranding&quot; notation using <code><span class='Ligature'></span></code> for lists.</p>
<h3 id="separators"><a class="header" href="#separators">Separators</a></h3>
<p>The characters <code><span class='Separator'></span></code> and <code><span class='Separator'>,</span></code> and newline are completely interchangeable and are used to separate expressions. An expression might be an element in a list or a line in a block. Empty sections—those that consist only of whitespace—are ignored. This means that any number of separators can be used between expressions, and that leading and trailing separators are also allowed. The expressions are evaluated in text order: left to right and top to bottom.</p>
<h3 id="list-and-array-notation"><a class="header" href="#list-and-array-notation">List and array notation</a></h3>
<p><em><a href="arrayrepr.html#array-literals">Full documentation</a></em></p>
<p>Lists (1-dimensional arrays) are enclosed in angle brackets <code><span class='Bracket'>⟨⟩</span></code>, with the results of the expressions in between being the list's elements. Lists of two elements or more can also be written with the ligature character <code><span class='Ligature'></span></code>. This character has higher binding strength than any part of an expression except <code><span class='Value'>.</span></code> for namespace field access. If one of the elements is a compound expression, then it will need to be enclosed in parentheses.</p>
<p>Arrays, or at least non-empty ones with rank 1 or more, can be written with square brackets <code><span class='Bracket'>[]</span></code>. These work just like angle brackets but <a href="couple.html#merge-and-array-theory">merge</a> the elements so that they form cells of the result.</p>
<h3 id="blocks"><a class="header" href="#blocks">Blocks</a></h3>
<p><em><a href="block.html">Full documentation</a></em></p>
<p>Blocks are written with curly braces <code><span class='Brace'>{}</span></code> and can have a subject, function, or modifier role. The contents are any number of bodies separated by <code><span class='Head'>;</span></code>. Each body is a sequence of expressions to be evaluated in order, possibly with a header, followed by <code><span class='Head'>:</span></code>, that sets the type and describes expected inputs. A body runs in its own environment according to the rules of <a href="lexical.html">lexical scoping</a>. The result is either a <a href="namespace.html">namespace</a>, if the body used <code><span class='Gets'></span></code>, or the result of the last expression.</p>
<p>The special names <code><span class='Value'>𝕨</span></code> and <code><span class='Value'>𝕩</span></code>, which stand for arguments, and <code><span class='Value'>𝕗</span></code> and <code><span class='Value'>𝕘</span></code>, which stand for operands, are available inside curly braces. Like ordinary names, the lowercase forms indicate subjects and the uppercase forms <code><span class='Function'>𝕎𝕏𝔽𝔾</span></code> indicate functions. If it has no header, the type and syntactic role of the block is determined by its contents: a 2-modifier contains <code><span class='Value'>𝕘</span></code>, a 1-modifier contains <code><span class='Value'>𝕗</span></code> but not <code><span class='Value'>𝕘</span></code>, and a function contains neither but does have one of <code><span class='Value'>𝕨𝕩𝕤</span><span class='Function'>𝕎𝕏𝕊</span></code>. The last option is an immediate block, which has a subject role and runs as soon as it's encountered.</p>