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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
<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="precedence"><a class="header" href="#precedence">Precedence</a></h2>
<p>Here's a full table of precedence for BQN's glyphs (broader than "operator precedence", as an "operator" usually just corresponds to a function). Entries at the bottom make the biggest divisions in the program, while the ones further up are subdivisions.</p>
<table>
<thead>
<tr>
<th>Level</th>
<th>Role</th>
<th>Associativity</th>
<th>Characters</th>
<th>Plus</th>
</tr>
</thead>
<tbody>
<tr>
<td>High</td>
<td>Brackets</td>
<td></td>
<td><code><span class='Paren'>()</span><span class='Bracket'>⟨⟩</span><span class='Brace'>{}</span><span class='Bracket'>[]</span></code></td>
<td></td>
</tr>
<tr>
<td></td>
<td><a href="namespace.html#imports">Field access</a></td>
<td>Left-to-right</td>
<td><code><span class='Value'>.</span></code></td>
<td></td>
</tr>
<tr>
<td></td>
<td><a href="#list-and-array-notation">Stranding</a></td>
<td>n-ary</td>
<td><code><span class='Ligature'>‿</span></code></td>
<td></td>
</tr>
<tr>
<td></td>
<td>Modifier</td>
<td>Left-to-right</td>
<td><code><span class='Modifier2'>∘⎉</span><span class='Modifier'>¨´</span></code>…</td>
<td><code><span class='Gets'>↩</span></code> in <code><span class='Function'>Fn</span><span class='Gets'>↩</span></code></td>
</tr>
<tr>
<td></td>
<td>Function</td>
<td>Right-to-left</td>
<td><code><span class='Function'>+↕⊔⍉</span></code>…</td>
<td><code><span class='Gets'>←↩⇐</span></code></td>
</tr>
<tr>
<td></td>
<td><a href="token.html#separators">Separator</a></td>
<td></td>
<td><code><span class='Separator'>⋄,</span></code> and newline</td>
<td><code><span class='Head'>?</span></code></td>
</tr>
<tr>
<td></td>
<td><a href="block.html#block-headers">Header</a></td>
<td></td>
<td><code><span class='Head'>:</span></code></td>
<td></td>
</tr>
<tr>
<td>Low</td>
<td><a href="block.html#multiple-bodies">Body</a></td>
<td></td>
<td><code><span class='Head'>;</span></code></td>
<td></td>
</tr>
</tbody>
</table>
<p>While all of BQN's grammar fits into this table somehow, it's not really the whole story because subexpressions including parentheses and blocks might behave like functions or modifiers. See <a href="#expressions">expressions</a> and <a href="#blocks">blocks</a>.</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="token.html#comments">Comment</a></td>
</tr>
<tr>
<td><code><span class='String'>'"</span></code></td>
<td><a href="token.html#characters-and-strings">Character or string literal</a></td>
</tr>
<tr>
<td><code><span class='String'>@</span></code></td>
<td><a href="token.html#characters-and-strings">Null character</a></td>
</tr>
<tr>
<td><code><span class='Number'>¯∞π</span></code></td>
<td><a href="token.html#numbers">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='Value'>.</span></code></td>
<td>Namespace <a href="namespace.html#imports">field access</a></td>
</tr>
<tr>
<td><code><span class='Separator'>⋄,</span></code> or newline</td>
<td>Statement or element <a href="token.html#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="tokens"><a class="header" href="#tokens">Tokens</a></h2>
<p><em><a href="token.html">Full documentation</a></em></p>
<p>BQN syntax is made up of tokens, which are mostly single characters. But there are a few exceptions:</p>
<ul>
<li><a href="token.html#comments">Comments</a> start with <code><span class='Comment'>#</span></code> and end at the end of the line.</li>
<li><a href="token.html#characters-and-strings">Character literals</a> start and end with <code><span class='String'>'</span></code>, and have exactly one character in between.</li>
<li><a href="token.html#characters-and-strings">String literals</a> start and end with <code><span class='String'>"</span></code>. Pairs of quotes <code><span class='String'>""</span></code> in between represent one quote character, and other characters (including <code><span class='String'>'</span></code>) represent themselves.</li>
<li><a href="token.html#numbers">Numbers</a> support decimal (<code><span class='Value'>.</span></code>) and scientific (<code><span class='Value'>e</span></code>) notation, plus <code><span class='Number'>π</span></code> and <code><span class='Number'>∞</span></code>, and use <code><span class='Number'>¯</span></code> for a minus sign.</li>
<li><a href="token.html#names">Variable names</a> allow letters, underscores, and numeric characters. They're matched case-insensitively, with a <a href="expression.html#role-spellings">spelling system</a> that determines role.</li>
</ul>
<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 "stranding" notation using <code><span class='Ligature'>‿</span></code> for lists. Elements within brackets are divided by <a href="token.html#separators">separators</a>: <code><span class='Separator'>,</span></code> or <code><span class='Separator'>⋄</span></code> or a line break.</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>
|