diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-11-15 22:16:46 -0500 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-11-15 22:16:46 -0500 |
| commit | 25311370e215cd12018dde8bd162583dbe36d473 (patch) | |
| tree | 271c8d0119a916d37fb295edd4dffe805bd4e115 /doc | |
| parent | 831b556f20d1e6427d561c0fcb4bb0c6d592314a (diff) | |
Compress expression section in syntax summary, moving details to expression page
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/expression.md | 29 | ||||
| -rw-r--r-- | doc/syntax.md | 46 |
2 files changed, 40 insertions, 35 deletions
diff --git a/doc/expression.md b/doc/expression.md index 2ec43994..61985d4b 100644 --- a/doc/expression.md +++ b/doc/expression.md @@ -31,6 +31,10 @@ In APL, the way one part of an expression interacts with others is determined by Syntactic role is a property of an expression, not its value. To describe it in terms of English grammar, you might say "I like BQN", using "BQN" as an object, or "BQN scares me", using it as a subject. BQN itself isn't a subject or object, it's a programming language. Similarly you might write `F g`, placing `f` in a function role to apply it to `g`, or `G f` to use `f` as an argument. Maybe even in the same program, although it's unlikely. +Below, the function `{𝕎𝕩}` treats its left argument `𝕎` as a function and its right argument `𝕩` as a subject. With a list of functions, we can make a table of the square and square root of a few numbers: + + ⟨ט,√⟩ {𝕎𝕩}⌜ 1‿4‿9 + ### Role spellings The four roles are **subject**, **function**, **1-modifier**, and **2-modifier**, as shown in the table below. Each type has an associated role (with non-operation types all corresponding to subjects), and the value of an expression will often have a matching type, but it doesn't have to. @@ -78,3 +82,28 @@ Here is a table of the modifier and function application rules: | `F*` | `_c_` | | 1-Modifier | Partial application A function with an asterisk indicates that a subject can also be used. Since the role doesn't exist after parsing, function and subject spellings are indistinguishable in these positions. Modifier applications bind more tightly than functions, and associate left-to-right while functions associate right-to-left. + +### Assignment + +Another element that can be included in expressions is assignment, which is written with `←` to *define* (also called "declare" in many other languages) a variable and `↩` to *change* its definition. A variable can only be defined once within a [scope](lexical.md), and can only be changed if it has already been defined. However, it can be shadowed, meaning that it is defined again in an inner scope even though it has a definition in an outer scope already. + + x←1 ⋄ {x←2 ⋄ x↩3 ⋄ x} + x + +Assignment can be used inline in an expression, and its result is always the value being assigned. The role of the identifier used must match the value being assigned. + + 2×a←(Neg←-)3 + a + +### Exports + +The double arrow `⇐` is used to export variables from a block or program, causing the result to be a [namespace](namespace.md). There are two ways to export variables. First, `←` in the variable definition can be replaced with `⇐` to export the variable as it's defined. Second, an export statement consisting of an assignment target followed by `⇐` with nothing to the right exports the variables in the assignment target and does nothing else. Export statements can be placed anywhere in the relevant program or body, including before declaration or on the last line, and a given variable can be exported any number of times. + + ⟨alias⇐a, b, c0‿c1⇐c, b2⇐b⟩←{ + b‿c⇐ # Non-definition exports can go anywhere + a⇐2 # Define and export + b←1+a + c←b‿"str" + } + +Fields of the resulting namespace can be accessed either directly using `namespace.field` syntax, or with a destructuring assignment as shown above. This assignment's target is a list where each element specifies one of the names exported by the block and what it should be assigned to. The element can be either a single name (such as `b` above), which gives both, or a combination of the assignment target, then `⇐`, then a name. If `⇐` is never used, the names can be given as a strand with `‿`. To use `⇐` for aliases, bracket syntax `⟨⟩` is needed. Imported names can be repeated and can be spelled with any role (the role is ignored). diff --git a/doc/syntax.md b/doc/syntax.md index 20119992..76c9e269 100644 --- a/doc/syntax.md +++ b/doc/syntax.md @@ -55,46 +55,22 @@ The null character (code point 0) has a dedicated literal representation `@`. Th ## Expressions -*[More discussion](context.md)* +*[Full documentation](expression.md)* -Like APL, BQN uses four *syntactic roles* for values in expressions: -* **Subjects**, like APL arrays or J nouns -* **Functions**, or verbs in J -* **1-Modifiers**, like APL monadic operators or J adverbs -* **2-Modifiers**, like APL dyadic operators or J conjunctions. +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: -These roles work exactly like they do in APL, with functions applying to one or two subject arguments, 1-modifiers taking a single function or subject on the left, and 2-modifiers taking a function or subject on each side. +| example | left | main | right | output | name | binding +|---------|-------|-------|-------|------------|------------|--------- +| `↕ 10` | `w?` | `F` | `x` | Subject | Function | RtL, looser +| `+ ⋈ -` | `F?` | `G` | `H` | Function | Train | +| `×´` | `F` | `_m` | | Function | 1-Modifier | LtR, tighter +| `2⊸\|` | `F` | `_c_` | `G` | Function | 2-Modifier | -Unlike APL, in BQN the syntactic role of an identifier is determined purely by the way it's spelled: a lowercase first letter (`name`) makes it a subject, an uppercase first letter (`Name`) makes it a function, and underscores are used for 1-modifiers (`_name`) and 2-modifiers (`_name_`). Below, the function `{𝕎𝕩}` treats its left argument `𝕎` as a function and its right argument `𝕩` as a subject. With a list of functions, we can make a table of the square and square root of a few numbers: +The four roles (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's [type](types.md) 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. - ⟨ט,√⟩ {𝕎𝕩}⌜ 1‿4‿9 +Assignment arrows `←`, `↩`, and `⇐` store expression results in variables: `←` and `⇐` create new variables while `↩` modifies existing ones. The general format is `Name ← Value`, where the two sides have the same role. Additionally, `lhs F↩ rhs` is a shortened form of `lhs ↩ lhs F rhs` and `lhs F↩` expands to `lhs ↩ F lhs`. -BQN's built-in operations also have patterns to indicate the syntactic role: 1-modifiers (`` ˜¨˘⁼⌜´` ``) are all superscript characters, and 2-modifiers (`∘○⊸⟜⌾⊘◶⚇⎉⍟`) all have an unbroken circle (two functions `⌽⍉` have broken circles with lines through them). Every other built-in constant is a function, although the special symbols `¯`, `∞`, and `π` are used as part of numeric literal notation. - -### Assignment - -Another element that can be included in expressions is assignment, which is written with `←` to *define* (also called "declare" in many other languages) a variable and `↩` to *change* its definition. A variable can only be defined once within a [scope](lexical.md), and can only be changed if it has already been defined. However, it can be shadowed, meaning that it is defined again in an inner scope even though it has a definition in an outer scope already. - - x←1 ⋄ {x←2 ⋄ x↩3 ⋄ x} - x - -Assignment can be used inline in an expression, and its result is always the value being assigned. The role of the identifier used must match the value being assigned. - - 2×a←(Neg←-)3 - a - -### Exports - -The double arrow `⇐` is used to export variables from a block or program, causing the result to be a [namespace](namespace.md). There are two ways to export variables. First, `←` in the variable definition can be replaced with `⇐` to export the variable as it's defined. Second, an export statement consisting of an assignment target followed by `⇐` with nothing to the right exports the variables in the assignment target and does nothing else. Export statements can be placed anywhere in the relevant program or body, including before declaration or on the last line, and a given variable can be exported any number of times. - - ⟨alias⇐a, b, c0‿c1⇐c, b2⇐b⟩←{ - b‿c⇐ # Non-definition exports can go anywhere - a⇐2 # Define and export - b←1+a - c←b‿"str" - } - -Fields of the resulting namespace can be accessed either directly using `namespace.field` syntax, or with a destructuring assignment as shown above. This assignment's target is a list where each element specifies one of the names exported by the block and what it should be assigned to. The element can be either a single name (such as `b` above), which gives both, or a combination of the assignment target, then `⇐`, then a name. If `⇐` is never used, the names can be given as a strand with `‿`. To use `⇐` for aliases, bracket syntax `⟨⟩` is needed. Imported names can be repeated and can be spelled with any role (the role is ignored). +The double arrow `⇐` is used for functionality relating to [namespaces](namespace.md). It has a few purposes: exporting assignment `name⇐value`, plain export `name⇐`, and aliasing `⟨alias⇐field⟩←namespace`. A block that uses it for export returns a namespace rather than the result of its last statement. ## Lists and blocks |
