diff options
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/evaluate.md | 2 | ||||
| -rw-r--r-- | spec/grammar.md | 38 | ||||
| -rw-r--r-- | spec/inferred.md | 2 | ||||
| -rw-r--r-- | spec/scope.md | 8 |
4 files changed, 26 insertions, 24 deletions
diff --git a/spec/evaluate.md b/spec/evaluate.md index c991e3c6..fbc97087 100644 --- a/spec/evaluate.md +++ b/spec/evaluate.md @@ -18,7 +18,7 @@ A `PROGRAM` or `BODY` is a list of `STMT`s, which are evaluated in program order A block consists of several `BODY` terms, some of which may have an accompanying header describing accepted inputs and how they are processed. An immediate block `brImm` can only have one `BODY`, and is evaluated by evaluating it. Other types of blocks don't evaluate any `BODY` immediately, but instead return a function or modifier that obtains its result by evaluating a particular `BODY`. The `BODY` is identified and evaluated once the block has received enough inputs (operands or arguments), which for modifiers can take one or two calls: if two calls are required, then on the first call the operands are simply stored and no code is evaluated yet. The stored values can be accessed by equality checking, or `β’Decompose` if defined. Two calls are required if there is more than one `BODY` term, if the `BODY` contains the special names `π¨π©π€πππ`, or if its header specifies arguments (the header-body combination is a `_mCase` or `_cCase_`). Otherwise only one is required. -To evaluate a block when enough inputs have been received, first the correct case must be identified. To do this, first each special case (`FCase`, `_mCase`, or `_cCase_`), excluding `FCase` nodes containing `UndoHead`, is checked in order to see if its arguments are strucurally compatible with the given arguments. That is, is `headW` is a `subject`, there must be a left argument matching that structure, and if `headX` is a `subject`, the right argument must match that structure. This means that `π¨` not only matches any left argument but also no argument. The test for compatibility is the same as for multiple assignment described below, except that the header may contain constants, which must match the corresponding part of the given argument. If no special case matches, then an appropriate general case (`FMain`, `_mMain`, or `_cMain_`) is used: if there are two, the first is used with no left argument and the second with a left argument; if there are one, it is always used, and if there are none, an error results. +To evaluate a block when enough inputs have been received, first the correct case must be identified. To do this, first each special case (`I_CASE` or `A_CASE`), excluding `A_CASE` nodes whose `ARG_HEAD` contains `"βΌ"`, is checked in order to see if its arguments are strucurally compatible with the given arguments. That is, is `headW` is a `subject`, there must be a left argument matching that structure, and if `headX` is a `subject`, the right argument must match that structure. This means that `π¨` not only matches any left argument but also no argument. The test for compatibility is the same as for multiple assignment described below, except that the header may contain constants, which must match the corresponding part of the given argument. If no special case matches, then an appropriate general `CASE` is used: if there are two, the first is used with no left argument and the second with a left argument; if there are one, it is always used, and if there are none, an error results. When a predicate `"?"` is evaluated, it may change the choice of case. The associated `EXPR` is evaluated and its result is checked. If it's not one of the numbers `0` or `1`, an error results. If it's `1`, evaluation of the `BODY` continues as usual. If it's `0`, evaluation is stopped and the next compatible `BODY` term is evaluated using the block's original inputs. diff --git a/spec/grammar.md b/spec/grammar.md index 5167dac9..7e4ca950 100644 --- a/spec/grammar.md +++ b/spec/grammar.md @@ -17,10 +17,10 @@ A program is a list of statements. Almost all statements are expressions. Namesp Here we define the "atomic" forms of functions and modifiers, which are either single tokens or enclosed in paired symbols. Stranded lists with `βΏ`, which binds more tightly than any form of execution, are also included. ANY = atom | Func | _mod1 | _mod2_ - _mod2_ = ( atom "." )? _c_ | _cl_ | "(" _m1Expr_ ")" | _brMod2_ - _mod1 = ( atom "." )? _m | _ml | "(" _m2Expr ")" | _brMod1 - Func = ( atom "." )? F | Fl | "(" FuncExpr ")" | BrFunc - atom = ( atom "." )? s | sl | "(" subExpr ")" | brSub | list + _mod2_ = ( atom "." )? _c_ | _cl_ | "(" _m1Expr_ ")" | _blMod2_ + _mod1 = ( atom "." )? _m | _ml | "(" _m2Expr ")" | _blMod1 + Func = ( atom "." )? F | Fl | "(" FuncExpr ")" | BlFunc + atom = ( atom "." )? s | sl | "(" subExpr ")" | blSub | list list = "β¨" β? ( ( EXPR β )* EXPR β? )? "β©" subject = atom | ANY ( "βΏ" ANY )+ @@ -86,31 +86,33 @@ There are some extra possibilities for a header that specifies arguments. As a s | headW? IMM_HEAD "βΌ"? headX | headW IMM_HEAD "Λ" "βΌ" headX | FuncName "Λ"? "βΌ" - | CaseHead - CaseHead = sl | "(" subExpr ")" | brSub | list # subject, + | xHead + xHead = sl | "(" subExpr ")" | blSub | list # subject, | ANY ( "βΏ" ANY )+ # but not s A braced block contains bodies, which are lists of statements, separated by semicolons and possibly preceded by headers, which are separated from the body with a colon. A non-final expression can be made into a predicate by following it with the separator-like `?`. Multiple bodies allow different handling for various cases, which are pattern-matched by headers. A block can have any number of bodies with headers. After these there can be bodies without headersβup to one for an immediate block and up to two for a block with arguments. If a block with arguments has one such body, it's ambivalent, but two of them refer to the monadic and dyadic cases. BODY = β? ( STMT β | EXPR β? "?" β? )* STMT β? - IMM_BLK = "{" ( β? IMM_HEAD β? ":" BODY ";" )* BODY? "}" - ARG_BLK = "{" ( β? ARG_HEAD β? ":" BODY ";" )* ( BODY ( ";" BODY )? )? "}" - BLOCK = IMM_BLOCK | ARG_BLOCK - brSub = "{" ( β? s ":" )? BODY "}" - BrFunc = BLOCK - _brMod1 = BLOCK - _brMod2_ = BLOCK + CASE = BODY + I_CASE = β? IMM_HEAD β? ":" BODY + A_CASE = β? ARG_HEAD β? ":" BODY + IMM_BLK = "{" ( I_CASE ";" )* ( I_CASE | CASE ) "}" + ARG_BLK = "{" ( A_CASE ";" )* ( A_CASE | CASE ( ";" CASE )? ) "}" + blSub = "{" ( β? s β? ":" )? BODY "}" + BlFunc = ARG_BLK + _blMod1 = IMM_BLK | ARG_BLK + _blMod2_ = IMM_BLK | ARG_BLK Three additional rules apply to blocks, allowing the ambiguous grammar above to be disambiguated. They are shown in the table below. First, each block type allows the special names in its row to be used as the given token types within `BODY` terms (not headers). Except for the spaces labelled "None", each of these four columns is cumulative, so that a given entry also includes all the entries above it. Second, a block can't contain one of the tokens from the "label" column of a different row. Third, each `BrFunc`, `_brMod1`, and `_brMod2_` term *must* contain one of the names on, and not above, the corresponding row (including the "label" column). | Term | `s` | `F` | `_m` | `_c_` | label |--------------------|--------|--------|---------|----------|------- -| `brSub`, `PROGRAM` | None | None | None | None | None -| `BrFunc` | `π¨π©π€` | `πππ` | | | `FuncLab` -| `_brMod1` | `ππ£` | `π½` | `_π£` | | `Mod1Lab` -| `_brMod2_` | `π` | `πΎ` | None | `_π£_` | `Mod2Lab` +| `blSub`, `PROGRAM` | None | None | None | None | None +| `BlFunc` | `π¨π©π€` | `πππ` | | | `FuncLab` +| `_blMod1` | `ππ£` | `π½` | `_π£` | | `Mod1Lab` +| `_blMod2_` | `π` | `πΎ` | None | `_π£_` | `Mod2Lab` -The rules for special name can be expressed in BNF by making many copies of all expression rules above. For each "level", or row in the table, a new version of every rule should be made that allows that level but not higher ones, and another version should be made that requires exactly that level. The values themselves should be included in `s`, `F`, `_m`, and `_c_` for these copies. Then the "allowed" rules are made simply by replacing the terms they contain (excluding `brSub` and so on) with the same "allowed" versions, and "required" rules are constructed using both "allowed" and "required" rules. For every part of a production rule, an alternative should be created that requires the relevant name in that part while allowing it in the others. For example, `( subject | nothing )? Derv arg` would be transformed to +The rules for special names can be expressed in BNF by making many copies of all expression rules above. For each "level", or row in the table, a new version of every rule should be made that allows that level but not higher ones, and another version should be made that requires exactly that level. The values themselves should be included in `s`, `F`, `_m`, and `_c_` for these copies. Then the "allowed" rules are made simply by replacing the terms they contain (excluding `blSub` and so on) with the same "allowed" versions, and "required" rules are constructed using both "allowed" and "required" rules. For every part of a production rule, an alternative should be created that requires the relevant name in that part while allowing it in the others. For example, `( subject | nothing )? Derv arg` would be transformed to arg_req1 = subExpr_req1 | ( subject_req1 | nothing_req1 ) Derv_allow1 arg_allow1 diff --git a/spec/inferred.md b/spec/inferred.md index 847bd83c..5ed454b7 100644 --- a/spec/inferred.md +++ b/spec/inferred.md @@ -164,7 +164,7 @@ Inverses of other modifiers and derived functions or modifiers obtained from the ### Undo headers -An `UndoHead` header specifies how a block function acts when undone. Like ordinary headers, undo headers are searched for a match when a block function `F` is undone, or when `FΛ` is undone with two arguments (including the two modifier cases `π½βk` and `π½πΎk` from the previous section). An `UndoHead` without `"Λ"` matches the `FβΌ` case while one with `"Λ"` matches the `FΛβΌ` case. The left and right arguments are matched to `headW` and `headX` as with ordinary headers, and the first matching case is evaluated to give the result of the Undo-derived function. +An `ARG_HEAD` header containing `"βΌ"` specifies how a block function acts when undone. Like ordinary headers, undo headers are searched for a match when a block function `F` is undone, or when `FΛ` is undone with two arguments (including the two modifier cases `π½βk` and `π½πΎk` from the previous section). An `ARG_HEAD` without `"Λ"` matches the `FβΌ` case while one with `"Λ"` matches the `FΛβΌ` case. The left and right arguments are matched to `headW` and `headX` as with ordinary headers, and the first matching case is evaluated to give the result of the Undo-derived function. ## Under diff --git a/spec/scope.md b/spec/scope.md index 893348fb..d03ef29d 100644 --- a/spec/scope.md +++ b/spec/scope.md @@ -10,7 +10,7 @@ A running BQN program manipulates variables during its [execution](evaluate.md), In this section the concept of an identifier's definition, a possibly different instance of that identifier, is specified. The definition determines when identifiers refer to the "same thing". In concrete terms, identifiers with the same definition all manipulate the same variable in a particular instance of the definition's containing scope. -A *scope* is a `PROGRAM`, `brSub`, `FCase`, `FMain`, `_mCase`, `_mMain`, `_cCase_`, `_cMain_`, or `brNS` node as defined by the BQN [grammar](grammar.md). An *identifier instance* is an `s`, `F`, `_m`, or `_c_` node; its *containing scope* is the "smallest" scope that contains itβthe scope that contains the identifier but not any other scopes containing the identifier. An identifier instance is *defined* when it is contained in the left hand side of an `β` assignment expression, that is, the leftmost component of one of the five grammatical rules with `ASGN`, provided that the `ASGN` node is `"β"` or `"β"`, or in a scope header, that is, a component immediately preceding `":"`. Each identifier instance in a valid BQN program corresponds to exactly one such defined identifier, called its *definition*, and two instances are considered to refer to the same identifier if they have the same definition. +A *scope* is a `PROGRAM`, `blSub`, `CASE`, `I_CASE`, or `A_CASE` node as defined by the BQN [grammar](grammar.md). An *identifier instance* is an `s`, `F`, `_m`, or `_c_` node; its *containing scope* is the "smallest" scope that contains itβthe scope that contains the identifier but not any other scopes containing the identifier. An identifier instance is *defined* when it is contained in the left hand side of an `β` assignment expression, that is, the leftmost component of one of the four grammatical rules with `ASGN`, provided that the `ASGN` node is `"β"` or `"β"`, or in a scope header, that is, `IMM_HEAD`, `ARG_HEAD`, or the `s` term in `blSub`. Each identifier instance in a valid BQN program corresponds to exactly one such defined identifier, called its *definition*, and two instances are considered to refer to the same identifier if they have the same definition. Two identifier instances have the *same name* if their tokens, as strings, match after removing all underscores `_` and ignoring case (so that the letters a to z are equal to their uppercase equivalents A to Z for this comparison). However, instances with the same name are not necessarily the same identifier, as they must also have the same definition. A defined identifier is a *potential definition* of another identifier instance if the two have the same name, and either: - The defined identifier's containing scope contains the other identifier's containing scope, or @@ -18,15 +18,15 @@ Two identifier instances have the *same name* if their tokens, as strings, match - The two identifiers are the same instance (a defined variable is its own definition). The definition for an identifier is chosen from the potential definitions based on their containing scopes: it is the one whose containing scope does not contain or match the containing scope of any other potential definition. If for any identifier there is no definition, then the program is not valid and results in an error. This can occur if the identifier has no potential definition, and also if two potential definitions appear in the same scope. In fact, under this scheme it is never valid to make two definitions with the same name at the top level of a single scope, because both definitions would be potential definitions for the one that comes second in program order. Both definitions have the same containing scope, and any potential definition must contain or match this scope, so no potential definition can be selected. -The definition of *program order* for identifier tokens follows the order of BQN [execution](evaluate.md). It corresponds to the order of a particular traversal of the abstract syntax tree for a program. To find the relative ordering of two identifiers in a program, we consider the highest-depth node that they both belong to; in this node they must occur in different components, or that component would be a higher-depth node containing both of them. In most nodes, the program order goes from right to left: components further to the right come earlier in program order. The exceptions are `PROGRAM`, `BODY`, `NS_BODY`, `list`, `subject` (for stranding), and body case (`FCase`, `_mCase`, `_cCase_`, `FMain`, `_mMain`, `_cMain_`, `brSub`, `BrFunc`, `_brMod1`, and `_brMod2_`) nodes, in which program order goes in the opposite order, from left to right (some assignment target nodes also contain lists or strands, but their ordering is irrelevant because if two identifiers with the same name appear in such a list, then it can't be a definition). +The definition of *program order* for identifier tokens follows the order of BQN [execution](evaluate.md). It corresponds to the order of a particular traversal of the abstract syntax tree for a program. To find the relative ordering of two identifiers in a program, we consider the highest-depth node that they both belong to; in this node they must occur in different components, or that component would be a higher-depth node containing both of them. In most nodes, the program order goes from right to left: components further to the right come earlier in program order. The exceptions are `PROGRAM`, `BODY`, `list`, `subject` (for stranding), and body structure (`I_CASE`, `A_CASE`, `IMM_BLK`, `ARG_BLK`, and `blSub`) nodes, in which program order goes in the opposite order, from left to right (some assignment target nodes also contain lists or strands, but their ordering is irrelevant because if two identifiers with the same name appear in such a list, then it can't be a definition). -A subject label is the `s` term in a `brSub` node. As part of a header, it can serve as the definition for an identifier. However, it's defined to be a syntax error if another instance of this identifier appears, except in a `Return` node (which cannot access its value). +A subject label is the `s` term in a `blSub` node. As part of a header, it can serve as the definition for an identifier. However, it's defined to be a syntax error if another instance of this identifier appears. ### Special names Special names such as `π©` or `π£` refer to variables, but have no definition and do not use scoping. Instead, they always refer to the immediately enclosing scope, and are defined automatically when the block is evaluated. -The six special names are `π¨π©πππ€π£`, and the tokens `πππ½πΎπ`, `_π£`, and `_π£_` are alternate spellings of these names as described in the [tokenization rules](token.md). Special names may be modified with `β©` assignment but cannot appear as the target of other kinds of assignment. Two special names represent the same identifier if they are the same name and appear in the same body. The initial value these names have is defined by the [evaluation rules](evaluate.md); the grammar for blocks ensures that all special names used in a block will be defined (possibly as the special value `Β·` in the case of `π¨`). +The six special names are `π¨π©πππ€π£`, and the tokens `πππ½πΎπ`, `_π£`, and `_π£_` are alternate spellings of these names as described in the [tokenization rules](token.md). Special names may be modified with `β©` assignment but cannot appear as the target of other kinds of assignment. Two special names represent the same identifier if they are the same name and appear in the same body (more precisely, the set of `BODY` nodes that contains each is the same). The initial value these names have is defined by the [evaluation rules](evaluate.md); the grammar for blocks ensures that all special names used in a block will be defined (possibly as the special value `Β·` in the case of `π¨`). ### Imports and exports |
