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
|
<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 identifiers or 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'>'"</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='Paren'>()</span></code></td>
<td>Expression grouping</td>
</tr>
<tr>
<td><code><span class='Gets'>←</span></code></td>
<td><a href="#assignment">Define</a></td>
</tr>
<tr>
<td><code><span class='Gets'>⇐</span></code></td>
<td><a href="#exports">Export</a></td>
</tr>
<tr>
<td><code><span class='Gets'>↩</span></code></td>
<td><a href="#assignment">Change</a></td>
</tr>
<tr>
<td><code><span class='Gets'>→</span></code></td>
<td><a href="block.html#returns">Return</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-notation">List</a> (rank-1 array)</td>
</tr>
<tr>
<td><code><span class='Ligature'>‿</span></code></td>
<td><a href="#list-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='Value'>:</span></code></td>
<td><a href="block.html#block-headers">Block header</a></td>
</tr>
<tr>
<td><code><span class='Value'>;</span></code></td>
<td><a href="block.html#multiple-bodies">Block body separator</a></td>
</tr>
<tr>
<td><code><span class='Value'>?</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 <code><span class='Comment'>#</span></code> that is not part of a string and continues to the end of the line.</p>
<h2 id="constants"><a class="header" href="#constants">Constants</a></h2>
<p>BQN has single-token notation for numbers, strings, and characters.</p>
<p>Numbers allow the typical decimal notation with <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> for scientific notation (or <code><span class='Function'>E</span></code>, as numeric notation is case-insensitive). <code><span class='Number'>∞</span></code> and <code><span class='Number'>π</span></code> may be used as special numeric values. If complex numbers are supported, then they can be written with the components separated by <code><span class='Value'>i</span></code>. However, no BQN to date supports complex numbers.</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 are written with double quotes <code><span class='String'>""</span></code>, and characters with single quotes <code><span class='String'>''</span></code> with a single character in between. A double quote within a string can be escaped by writing it twice; if two string literals are next to each other, they must be separated by a space. In contrast, character literals do not use escapes, as the length is already known.</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'>"str"</span> <span class='Separator'>⋄</span> <span class='String'>"s't""r"</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'>'"'</span> <span class='Bracket'>⟩</span> <span class='Comment'># "" is an escape
</span>⟨ 3 5 1 1 1 ⟩
<span class='Function'>≡</span><span class='Modifier'>¨</span> <span class='Bracket'>⟨</span> <span class='String'>"a"</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>The null character (code point 0) has a dedicated literal representation <code><span class='String'>@</span></code>. This character can be used 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>. The character can also be entered as a character literal, but this will display differently in various editors and some tools may have trouble with a file directly containing a null, so it is best to use <code><span class='String'>@</span></code> instead.</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. Functions can be applied to subjects or grouped into trains, while modifiers 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></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='Value'>?</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 roles (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>Assignment 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.</p>
<h2 id="lists-and-blocks"><a class="header" href="#lists-and-blocks">Lists and blocks</a></h2>
<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 function. 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-notation"><a class="header" href="#list-notation">List notation</a></h3>
<p><em><a href="arrayrepr.html#list-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. If one of the elements is a compound expression, then it will need to be enclosed in parentheses.</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 be used to group expressions or define functions and modifiers. The contents are simply a sequence of expressions, where each is evaluated and the result of the last is returned in order to evaluate the block. This result can have any value, and its syntactic role in the calling context is determined by the normal rules: functions return subjects and modifiers return functions. Blocks have <a href="lexical.html">lexical scope</a>.</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. 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>. If no special names are present the block is an <em>immediate block</em> and is evaluated as soon as it appears, with the result having a subject role.</p>
<p>A modifier can be evaluated twice: once when passed operands and again when the resulting function is passed arguments. If it contains <code><span class='Value'>𝕨</span></code> or <code><span class='Value'>𝕩</span></code>, the first evaluation simply remembers the operands, and the contents will be executed only on the second evaluation, when the arguments are available. If it doesn't contain these, then the contents are executed on the first evaluation and the result is treated as a function.</p>
|