From 229e2cd2f5c78b13c483a8559dead2c8f31d8e42 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sat, 18 Jul 2020 18:26:52 -0400 Subject: Terminology changes: subject, 1/2-modifier, Box/Unbox to Enclose/Merge, blocks --- spec/evaluate.md | 40 +++++++++--------- spec/grammar.md | 116 ++++++++++++++++++++++++++--------------------------- spec/literal.md | 2 +- spec/reference.bqn | 4 +- spec/token.md | 6 +-- spec/types.md | 12 +++--- 6 files changed, 90 insertions(+), 90 deletions(-) (limited to 'spec') diff --git a/spec/evaluate.md b/spec/evaluate.md index 6442a499..a555740f 100644 --- a/spec/evaluate.md +++ b/spec/evaluate.md @@ -8,9 +8,9 @@ The result of parsing a valid BQN program is a `PROGRAM`, and the program is run A `PROGRAM` or `BODY` is a list of `STMT`s (for `BODY`, the last must be an `EXPR`, a particular kind of `STMT`), which are evaluated in program order. The statement `nothing` does nothing when evaluated, while `EXPR` evaluates some APL code and possibly assigns the results, as described below. -A block consists of several `BODY` terms, some of which may have an accompanying header describing accepted inputs and how they are processed. A value block `brVal` can only have one `BODY`, and is evaluated by evaluating the code in it. Other types of blocks do not evaluate any `BODY` immediately, but instead return a function, modifier, or operator 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 and compositions 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. 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 is a `_mCase` or `_cCase_`). Otherwise only one is required. +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 the code in it. Other types of blocks do not 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. 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_`) is checked in order to see if its arguments are strucurally compatible with the given arguments. That is, is `headW` is a `value`, there must be a left argument matching that structure, and if `headX` is a `value`, 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 (`FCase`, `_mCase`, or `_cCase_`) 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. The only remaining step before evaluating the `BODY` is to bind the inputs and other names. Special names are always bound when applicable: `𝕨𝕩𝕀` if arguments are used, `𝕨` if there is a left argument, `π•—π•˜` if operands are used, and `_𝕣` and `_𝕣_` for modifiers and combinators, respectively. Any names in the header are also bound, allowing multiple assignment for arguments. @@ -18,29 +18,29 @@ If there is no left argument, but the `BODY` contains `𝕨` at the top level, t ### Assignment -An *assignment* is one of the four rules containing `ASGN`. It is evaluated by first evaluating the right-hand-side `valExpr`, `FuncExpr`, `_modExpr`, or `_cmpExp_` expression, and then storing the result in the left-hand-side identifier or identifiers. The result of the assignment expression is the result of its right-hand side. Except for values, only a lone identifier is allowed on the left-hand side and storage is obvious. For values, *multiple assignment* with a list left-hand side is also allowed. Multiple assignment is performed recursively by assigning right-hand-side values to the left-hand-side targets, with single-identifier (`v`) assignment as the base case. When matching the right-hand side to a list left-hand side, the left hand side is treated as a list of `lhs` targets. The evaluated right-hand side must be a list (rank-1 array) of the same length, and is matched to these targets element-wise. +An *assignment* is one of the four rules containing `ASGN`. It is evaluated by first evaluating the right-hand-side `subExpr`, `FuncExpr`, `_m1Expr`, or `_m2Exp_` expression, and then storing the result in the left-hand-side identifier or identifiers. The result of the assignment expression is the result of its right-hand side. Except for subjects, only a lone identifier is allowed on the left-hand side and storage is obvious. For subjects, *multiple assignment* with a list left-hand side is also allowed. Multiple assignment is performed recursively by assigning right-hand-side values to the left-hand-side targets, with single-identifier (`s`) assignment as the base case. When matching the right-hand side to a list left-hand side, the left hand side is treated as a list of `lhs` targets. The evaluated right-hand side must be a list (rank-1 array) of the same length, and is matched to these targets element-wise. -*Modified assignment* is the value assignment rule `lhs Derv "↩" valExpr`. In this case, `lhs` should be evaluated as if it were a `valExpr` (the syntax is a subset of `valExpr`), and the result of the function application `lhs Derv valExpr` should be assigned to `lhs`, and is also the result of the modified assignment expression. +*Modified assignment* is the subject assignment rule `lhs Derv "↩" subExpr`. In this case, `lhs` should be evaluated as if it were a `subExpr` (the syntax is a subset of `subExpr`), and the result of the function application `lhs Derv subExpr` should be assigned to `lhs`, and is also the result of the modified assignment expression. ### Expressions -We now give rules for evaluating an `atom`, `Func`, `_mod` or `_comp_` expression (the possible options for `ANY`). A literal `vl`, `Fl`, `_ml`, or `_cl_` has a fixed value defined by the specification ([value literals](literal.md) and [built-ins](primitive.md)). An identifier `v`, `F`, `_m`, or `_c_` is evaluated by returning its value; because of the scoping rules it must have one when evaluated. A parenthesized expression such as `"(" _modExpr ")"` simply returns the result of the interior expression. A braced construct such as `BraceFunc` is defined by the evaluation of the statements it contains after all parameters are accepted. Finally, a list `"⟨" β‹„? ( ( EXPR β‹„ )* EXPR β‹„? )? "⟩"` or `ANY ( "β€Ώ" ANY )+` consists grammatically of a list of expressions. To evaluate it, each expression is evaluated in source order and their results are placed as elements of a rank-1 array. The two forms have identical semantics but different punctuation. +We now give rules for evaluating an `atom`, `Func`, `_mod1` or `_mod2_` expression (the possible options for `ANY`). A literal or primitive `sl`, `Fl`, `_ml`, or `_cl_` has a fixed value defined by the specification ([literals](literal.md) and [built-ins](primitive.md)). An identifier `s`, `F`, `_m`, or `_c_` is evaluated by returning its value; because of the scoping rules it must have one when evaluated. A parenthesized expression such as `"(" _modExpr ")"` simply returns the result of the interior expression. A braced construct such as `BraceFunc` is defined by the evaluation of the statements it contains after all parameters are accepted. Finally, a list `"⟨" β‹„? ( ( EXPR β‹„ )* EXPR β‹„? )? "⟩"` or `ANY ( "β€Ώ" ANY )+` consists grammatically of a list of expressions. To evaluate it, each expression is evaluated in source order and their results are placed as elements of a rank-1 array. The two forms have identical semantics but different punctuation. -Rules in the table below are function and operator evaluation. -| L | Left | Called | Right | R | Types -|-----|-------------------------|----------|---------------------|-----|----------- -| `𝕨` | `( value \| nothing )?` | `Derv` | `arg` | `𝕩` | Function, value -| `𝕗` | `Operand` | `_mod` | | | Modifier -| `𝕗` | `Operand` | `_comp_` | `( value \| Func )` | `π•˜` | Composition +Rules in the table below are function and modifier evaluation. +| L | Left | Called | Right | R | Types +|-----|---------------------------|----------|-----------------------|-----|----------- +| `𝕨` | `( subject \| nothing )?` | `Derv` | `arg` | `𝕩` | Function, subject +| `𝕗` | `Operand` | `_mod1` | | | 1-Modifier +| `𝕗` | `Operand` | `_mod2_` | `( subject \| Func )` | `π•˜` | 2-Modifier -In each case the constituent expressions are evaluated in reverse source order: Right, then Called, then Left. Then the expression's result is obtained by calling the Called value on its parameters. A left argument of `nothing` is not used as a parameter, leaving only a right argument in that case. The data type of the Called value must be appropriate to the expression type, as indicated in the "Types" column. For function application, a value type (number, character, or array) is allowed. It is called simply by returning itself. Although the arguments are ignored in this case, they are still evaluated. A braced construct is evaluated by binding the parameter names given in columns L and R to the corresponding values. Then if all parameter levels present have been bound, its body is evaluated to give the result of application. +In each case the constituent expressions are evaluated in reverse source order: Right, then Called, then Left. Then the expression's result is obtained by calling the Called value on its parameters. A left argument of `nothing` is not used as a parameter, leaving only a right argument in that case. The type of the Called value must be appropriate to the expression type, as indicated in the "Types" column. For function application, a data type (number, character, or array) is allowed. It is called simply by returning itself. Although the arguments are ignored in this case, they are still evaluated. A braced construct is evaluated by binding the parameter names given in columns L and R to the corresponding values. Then if all parameter levels present have been bound, its body is evaluated to give the result of application. -The following rules derive new functions or operators from existing ones. -| Left | Center | Right | Result -|------------|-----------|---------------------|-------------- -| | `_comp_` | `( value \| Func )` | `{𝔽 _C_ R}` -| `Operand` | `_comp_` | | `{L _C_ 𝔽}` -| `Operand` | `Derv` | `Fork` | `{(𝕨L𝕩)C(𝕨R𝕩)}` -| `nothing?` | `Derv` | `Fork` | `{ C(𝕨R𝕩)}` +The following rules derive new functions or modifiers from existing ones. +| Left | Center | Right | Result +|------------|-----------|-----------------------|-------------- +| | `_mod2_` | `( subject \| Func )` | `{𝔽 _C_ R}` +| `Operand` | `_mod2_` | | `{L _C_ 𝔽}` +| `Operand` | `Derv` | `Fork` | `{(𝕨L𝕩)C(𝕨R𝕩)}` +| `nothing?` | `Derv` | `Fork` | `{ C(𝕨R𝕩)}` -As with applications, all expressions are evaluated in reverse source order before doing anything else. Then a result is formed without calling the center value. Its value in BQN is given in the rightmost column, using `L`, `C`, and `R` for the results of the expressions in the left, center, and right columns, respectively. For the first two rules (*partial application*), the given operand is bound to the composition: the result is a modifier that, when called, calls the center composition with the bound operand on the same side it appeared on and the new operand on the remaining side. A *train* is a function that, when called, calls the right-hand function on all arguments, then the left-hand function, and calls the center function with these results as arguments. In a composition partial application, the result will fail when applied if the center value does not have the composition type, and in a fork, it will fail if any component has a modifier or composition type (that is, cannot be applied as a function). BQN implementations are not required to check for these types when forming the result of these expressions, but may give an error on formation even if the result will never be applied. +As with applications, all expressions are evaluated in reverse source order before doing anything else. Then a result is formed without calling the center value. Its value in BQN is given in the rightmost column, using `L`, `C`, and `R` for the results of the expressions in the left, center, and right columns, respectively. For the first two rules (*partial application*), the given operand is bound to the 2-modifier: the result is a 1-modifier that, when called, calls the center 2-modifier with the bound operand on the same side it appeared on and the new operand on the remaining side. A *train* is a function that, when called, calls the right-hand function on all arguments, then the left-hand function, and calls the center function with these results as arguments. In a modifier partial application, the result will fail when applied if the center value does not have the 2-modifier type, and in a fork, it will fail if any component has a modifier type (that is, cannot be applied as a function). BQN implementations are not required to check for these types when forming the result of these expressions, but may give an error on formation even if the result will never be applied. diff --git a/spec/grammar.md b/spec/grammar.md index fe20e103..58544ce8 100644 --- a/spec/grammar.md +++ b/spec/grammar.md @@ -1,40 +1,40 @@ BQN's grammar is given below. Terms are defined in a [BNF](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form) variant. However, handling special names properly is possible but difficult in BNF, so they are explained in text along with the braced block grammar. -The symbols `v`, `F`, `_m`, and `_c_` are identifier tokens with value, function, modifier, and composition classes respectively. Similarly, `vl`, `Fl`, `_ml`, and `_cl_` refer to value literals (numeric and character literals, or primitives) of those classes. While names in the BNF here follow the identifier naming scheme, this is informative only: syntactic classes are no longer used after parsing and cannot be inspected in a running program. +The symbols `s`, `F`, `_m`, and `_c_` are identifier tokens with subject, function, 1-modifier, and 2-modifier classes respectively. Similarly, `sl`, `Fl`, `_ml`, and `_cl_` refer to literals and primitives of those classes. While names in the BNF here follow the identifier naming scheme, this is informative only: syntactic classes are no longer used after parsing and cannot be inspected in a running program. A program is a list of statements. Almost all statements are expressions. However, explicit definitions and valueless results stemming from `Β·`, or `𝕨` in a monadic brace function, can be used as statements but not expressions. PROGRAM = β‹„? ( ( STMT β‹„ )* STMT β‹„? )? STMT = EXPR | DEF | nothing β‹„ = ( "β‹„" | "," | \n )+ - EXPR = valExpr | FuncExpr | _modExpr | _cmpExp_ + EXPR = subExpr | FuncExpr | _m1Expr | _m2Expr_ -Here we define the "atomic" forms of functions and operators, which are either single tokens or enclosed in paired symbols. Stranded vectors with `β€Ώ`, which binds more tightly than any form of execution, are also included. +Here we define the "atomic" forms of functions and modifiers, which are either single tokens or enclosed in paired symbols. Stranded vectors with `β€Ώ`, which binds more tightly than any form of execution, are also included. - ANY = atom | Func | _mod | _comp_ - _comp_ = _c_ | _cl_ | "(" _cmpExp_ ")" | _brComp_ - _mod = _m | _ml | "(" _modExpr ")" | _brMod - Func = F | Fl | "(" FuncExpr ")" | BrFunc - atom = v | vl | "(" valExpr ")" | brVal | list + ANY = atom | Func | _mod1 | _mod2_ + _mod2_ = _c_ | _cl_ | "(" _m1Expr_ ")" | _brMod2_ + _mod1 = _m | _ml | "(" _m2Expr ")" | _brMod1 + Func = F | Fl | "(" FuncExpr ")" | BrFunc + atom = s | sl | "(" subExpr ")" | brSub | list list = "⟨" β‹„? ( ( EXPR β‹„ )* EXPR β‹„? )? "⟩" - value = atom | ANY ( "β€Ώ" ANY )+ + subject = atom | ANY ( "β€Ώ" ANY )+ -Starting at the highest-order objects, modifiers and compositions have fairly simple syntax. In most cases the syntax for `←` and `↩` is the same, but only `↩` can be used for modified assignment. +Starting at the highest-order objects, modifiers have fairly simple syntax. In most cases the syntax for `←` and `↩` is the same, but only `↩` can be used for modified assignment. ASGN = "←" | "↩" - _cmpExp_ = _comp_ - | _c_ ASGN _cmpExp_ - _modExpr = _mod - | _comp_ ( value | Func ) # Right partial application - | Operand _comp_ # Left partial application - | _m ASGN _modExpr + _m2Expr_ = _mod2_ + | _c_ ASGN _m2Expr_ + _m1Expr = _mod1 + | _mod2_ ( subject | Func ) # Right partial application + | Operand _mod2_ # Left partial application + | _m ASGN _m1Expr -Functions can be formed by fully applying operators or as trains. Operators are left-associative, so that the left operand (`Operand`) can include operators but the right operand (`value | Func`) cannot. Trains are right-associative, but bind less tightly than operators. Assignment is not allowed in the top level of a train: it must be parenthesized. +Functions can be formed by fully applying modifiers or as trains. modifiers are left-associative, so that the left operand (`Operand`) can include modifier applications but the right operand (`subject | Func`) cannot. Trains are right-associative, but bind less tightly than modifiers. Assignment is not allowed in the top level of a train: it must be parenthesized. Derv = Func - | Operand _mod - | Operand _comp_ ( value | Func ) - Operand = value + | Operand _mod1 + | Operand _mod2_ ( subject | Func ) + Operand = subject | Derv Fork = Derv | Operand Derv Fork # 3-train @@ -44,65 +44,65 @@ Functions can be formed by fully applying operators or as trains. Operators are FuncExpr = Train | F ASGN FuncExpr -Value expressions are complicated by the possibility of list assignment. We also define nothing-statements, which have very similar syntax to value expressions but do not permit assignment. +Subject expressions are complicated by the possibility of list assignment. We also define nothing-statements, which have very similar syntax to subject expressions but do not permit assignment. - arg = valExpr - | ( value | nothing )? Derv arg + arg = subExpr + | ( subject | nothing )? Derv arg nothing = "Β·" - | ( value | nothing )? Derv nothing - LHS_ANY = lhsValue | F | _m | _c_ + | ( subject | nothing )? Derv nothing + LHS_ANY = lhsSub | F | _m | _c_ LHS_ATOM = LHS_ANY | "(" lhsStr ")" LHS_ELT = LHS_ANY | lhsStr - lhsValue = v + lhsSub = s | "⟨" β‹„? ( ( LHS_ELT β‹„ )* LHS_ELT β‹„? )? "⟩" lhsStr = LHS_ATOM ( "β€Ώ" LHS_ATOM )+ - lhs = lhsValue | lhsStr - valExpr = arg - | lhs ASGN valExpr - | lhs Derv "↩" valExpr # Modified assignment + lhs = lhsSub | lhsStr + subExpr = arg + | lhs ASGN subExpr + | lhs Derv "↩" subExpr # Modified assignment -A header looks like a name for the thing being headed, or its application to inputs (possibly twice in the case of modifiers and compositions). As with assignment, it is restricted to a simple form with no extra parentheses. The full list syntax is allowed for arguments. As a special rule, a monadic function header specifically can omit the function when the argument is not just a name (as this would conflict with a value label). The following cases define only headers with arguments, which are assumed to be special cases; there can be any number of these. Headers without arguments can only refer to the general caseβ€”note that operands are not pattern matchedβ€”so there can be at most two of these kinds of headers, indicating the monadic and dyadic cases. +A header looks like a name for the thing being headed, or its application to inputs (possibly twice in the case of modifiers). As with assignment, it is restricted to a simple form with no extra parentheses. The full list syntax is allowed for arguments. As a special rule, a monadic function header specifically can omit the function when the argument is not just a name (as this would conflict with a subject label). The following cases define only headers with arguments, which are assumed to be special cases; there can be any number of these. Headers without arguments can only refer to the general caseβ€”note that operands are not pattern matchedβ€”so there can be at most two of these kinds of headers, indicating the monadic and dyadic cases. - headW = value | "𝕨" - headX = value | "𝕩" + headW = subject | "𝕨" + headX = subject | "𝕩" HeadF = F | "𝕗" | "𝔽" HeadG = F | "π•˜" | "𝔾" - ModH1 = HeadF ( _m | "_𝕣" ) - CmpH1 = HeadF ( _c_ | "_𝕣_" ) HeadG + Mod1H1 = HeadF ( _m | "_𝕣" ) + Mod2H1 = HeadF ( _c_ | "_𝕣_" ) HeadG FuncHead = headW? ( F | "π•Š" ) headX - | vl | "(" valExpr ")" | brVal | list # value, - | ANY ( "β€Ώ" ANY )+ # but not v - _modHead = headW? ModH1 headX - _cmpHed_ = headW? CmpH1 headX + | sl | "(" subExpr ")" | brSub | list # subject, + | ANY ( "β€Ώ" ANY )+ # but not s + _m1Head = headW? Mod1H1 headX + _m2Head_ = headW? Mod2H1 headX -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. Multiple bodies allow different handling for various cases, which are pattern-matched by headers. For a value block there are no inputs, so there can only be one possible case and one body. Functions and operators allow any number of "matched" bodies, with headers that have arguments, followed by at most two "main" bodies with either no headers or headers without arguments. If there is one main body, it is ambivalent, but two main bodies refer to the monadic and dyadic cases. +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. Multiple bodies allow different handling for various cases, which are pattern-matched by headers. For an immediate block there are no inputs, so there can only be one possible case and one body. Functions and modifiers allow any number of "matched" bodies, with headers that have arguments, followed by at most two "main" bodies with either no headers or headers without arguments. If there is one main body, it is ambivalent, but two main bodies refer to the monadic and dyadic cases. BODY = β‹„? ( STMT β‹„ )* EXPR β‹„? FCase = β‹„? FuncHead ":" BODY - _mCase = β‹„? _modHead ":" BODY - _cCase_ = β‹„? _cmpHed_ ":" BODY - FMain = ( β‹„? F ":" )? BODY - _mMain = ( β‹„? ( _m | ModH1 ) ":" )? BODY - _cMain_ = ( β‹„? ( _c_ | CmpH1 ) ":" )? BODY - brVal = "{" ( β‹„? v ":" )? BODY "}" + _mCase = β‹„? _m1Head ":" BODY + _cCase_ = β‹„? _m2Head_ ":" BODY + FMain = ( β‹„? F ":" )? BODY + _mMain = ( β‹„? ( _m | Mod1H1 ) ":" )? BODY + _cMain_ = ( β‹„? ( _c_ | Mod2H1 ) ":" )? BODY + brSub = "{" ( β‹„? s ":" )? BODY "}" BrFunc = "{" ( FCase ";" )* ( FCase | FMain ( ";" FMain )? ) "}" - _brMod = "{" ( _mCase ";" )* ( _mCase | _mMain ( ";" _mMain )? ) "}" - _brComp_ = "{" ( _cCase_ ";" )* ( _cCase_ | _cMan_ ( ";" _cMan_ )? ) "}" + _brMod1 = "{" ( _mCase ";" )* ( _mCase | _mMain ( ";" _mMain )? ) "}" + _brMod2_ = "{" ( _cCase_ ";" )* ( _cCase_ | _cMan_ ( ";" _cMan_ )? ) "}" -Two additional rules apply to blocks, based on the special name associations in the table below. First, each block allows the special names in its column to be used as the given token types within `BODY` terms (not headers). Except for the spaces labelled "None", each column is cumulative and a given entry also includes all the entries above it. Second, for `BrFunc`, `_brMod`, and `_brComp_` terms, if no header is given, then at least one `BODY` term in it *must* contain one of the names on, and not above, the corresponding row. Otherwise the syntax would be ambiguous, since for example a simple `"{" BODY "}"` sequence could have any type. +Two additional rules apply to blocks, based on the special name associations in the table below. First, each block allows the special names in its column to be used as the given token types within `BODY` terms (not headers). Except for the spaces labelled "None", each column is cumulative and a given entry also includes all the entries above it. Second, for `BrFunc`, `_brMod1`, and `_brMod2_` terms, if no header is given, then at least one `BODY` term in it *must* contain one of the names on, and not above, the corresponding row. Otherwise the syntax would be ambiguous, since for example a simple `"{" BODY "}"` sequence could have any type. -| Term | `v` | `F` | `_m` | `_c_` | other +| Term | `s` | `F` | `_m` | `_c_` | other |--------------------|--------|--------|---------|----------|------- -| `brVal`, `PROGRAM` | None | None | None | None | +| `brSub`, `PROGRAM` | None | None | None | None | | `BrFunc` | `𝕨𝕩𝕀` | `π•Žπ•π•Š` | | | `";"` -| `_brMod` | `𝕗𝕣` | `𝔽` | `_𝕣` | | -| `_brComp_` | `π•˜` | `𝔾` | None | `_𝕣_` | +| `_brMod1` | `𝕗𝕣` | `𝔽` | `_𝕣` | | +| `_brMod2_` | `π•˜` | `𝔾` | None | `_𝕣_` | -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 `v`, `F`, `_m`, and `_c_` for these copies. Then the "allowed" rules are made simply by replacing the terms they contain (excluding `brVal` 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, `( value | nothing )? Derv arg` would be transformed to +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 - arg_req1 = valExpr_req1 - | ( value_req1 | nothing_req1 ) Derv_allow1 arg_allow1 - | ( value_allow1 | nothing_allow1 )? Derv_req1 arg_allow1 - | ( value_allow1 | nothing_allow1 )? Derv_allow1 arg_req1 + arg_req1 = subExpr_req1 + | ( subject_req1 | nothing_req1 ) Derv_allow1 arg_allow1 + | ( subject_allow1 | nothing_allow1 )? Derv_req1 arg_allow1 + | ( subject_allow1 | nothing_allow1 )? Derv_allow1 arg_req1 Quite tedious. The explosion of rules is partly due to the fact that the brace-typing rule falls into a weaker class of grammars than the other rules. Most of BQN is [deterministic context-free](https://en.wikipedia.org/wiki/Deterministic_context-free_grammar) but brace-typing is not, only context-free. Fortunately brace typing does not introduce the parsing difficulties that can be present in a general context-free grammar, and it can easily be performed in linear time: after [scanning](token.md) but before parsing, move through the source code maintaining a stack of the current top-level set of braces. Whenever a colon or special name is encountered, annotate that set of braces to indicate that it is present. When a closing brace is encountered and the top brace is popped off the stack, the type is needed if there was no colon, and can be found based on which names were present. One way to present this information to the parser is to replace the brace tokens with new tokens that indicate the type. diff --git a/spec/literal.md b/spec/literal.md index 0f22909f..937363e0 100644 --- a/spec/literal.md +++ b/spec/literal.md @@ -1,4 +1,4 @@ -A *literal* is a single [token](token.md) that indicates a fixed character, number, or array. While literals indicate data of a value type, [primitives](primitive.md) indicate data of a function type: function, modifier, or composition. +A *literal* is a single [token](token.md) that indicates a fixed character, number, or array. While literals indicate values of a data type, [primitives](primitive.md) indicate values of an operation type: function, 1-modifier, or 2-modifier. Two types of literal deal with text. As the source code is considered to be a sequence of unicode code points ("characters"), and these code points are also used for BQN's character [data type](types.md), the representation of a text literal is very similar to its value. In a text literal, the newline character is always represented using the ASCII line feed character, code point 10. A *character literal* is enclosed with single quotes `'` and its value is identical to the single character between them. A *string literal* is enclosed in double quotes `"`, and any double quotes between them must come in pairs, as a lone double quote marks the end of the literal. The value of a string literal is a rank-1 array whose elements are the characters in between the enclosing quotes, after replacing each pair of double quotes with only one such quote. diff --git a/spec/reference.bqn b/spec/reference.bqn index 2072f96a..bba39aef 100644 --- a/spec/reference.bqn +++ b/spec/reference.bqn @@ -176,7 +176,7 @@ Depth←IsArrayβ—Ά0β€Ώ{1+0⌈´DepthΒ¨β₯Šπ•©} #⌜ # LAYER 4: Operators -> ↩ Unbox ⊘ > +> ↩ Merge ⊘ > ≍ ← >∘Pair βŽ‰ ← _rankOp_ βš‡ ← _depthOp_ @@ -188,7 +188,7 @@ DropV← {βŠ‘βŸœπ•©Β¨π•¨+↕𝕨-Λœβ‰ π•©} Cell ← DropVβŸœβ‰’ Pair ← {βŸ¨π•©βŸ©} ⊘ {βŸ¨π•¨,π•©βŸ©} -Unbox←(0<β‰ βˆ˜β₯Š)β—ΆβŠ’β€Ώ{ +Merge←(0<β‰ βˆ˜β₯Š)β—ΆβŠ’β€Ώ{ cβ†β‰’βŠ‘π•© ! ∧´β₯Š(c≑≒)¨𝕩 π•©βŠ‘βŸœToArrayΛœβŒœβ†•c diff --git a/spec/token.md b/spec/token.md index 8315c9c2..3c2235eb 100644 --- a/spec/token.md +++ b/spec/token.md @@ -6,7 +6,7 @@ A BQN *character literal* consists of a single character between single quotes, A comment consists of the hash character `#` and any following text until (not including) the next newline character. The initial `#` must not be part of a string literal started earlier. Comments are ignored entirely and do not form tokens. -Identifiers and numeric literals share the same token formation rule. These tokens are formed from the *numeric characters* `Β―βˆžΟ€.0123456789` and *alphabetic characters* `_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ` and the oddball `𝕣`. Any sequence of these characters adjacent to each other forms a single token, which is a *numeric literal* if it begins with a numeric character and an *identifier* if it begins with an alphabetic character. Numeric literals are also subject to [numeric literal rules](literal.md), which specify which numeric literals are valid and which numbers they represent. If the token contains `𝕣` it must be either `𝕣`, `_𝕣`, or `_𝕣_` and is considered a special name (see below). As the value taken by this identifier can only be a modifier or composition, the uppercase character `ℝ` is not allowed. +Identifiers and numeric literals share the same token formation rule. These tokens are formed from the *numeric characters* `Β―βˆžΟ€.0123456789` and *alphabetic characters* `_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ` and the oddball `𝕣`. Any sequence of these characters adjacent to each other forms a single token, which is a *numeric literal* if it begins with a numeric character and an *identifier* if it begins with an alphabetic character. Numeric literals are also subject to [numeric literal rules](literal.md), which specify which numeric literals are valid and which numbers they represent. If the token contains `𝕣` it must be either `𝕣`, `_𝕣`, or `_𝕣_` and is considered a special name (see below). As the value taken by this identifier can only be a modifier, the uppercase character `ℝ` is not allowed. Following this step, the whitespace characters space and tab are ignored, and do not form tokens. Only these whitespace characters, and the newline character, which does form a token, are allowed. @@ -15,8 +15,8 @@ Otherwise, a single character forms a token. Only the specified set of character | Class | Characters |-----------------------|------------ | Primitive Function | `+-Γ—Γ·β‹†βˆšβŒŠβŒˆ\|¬∧∨<>β‰ =≀β‰₯β‰‘β‰’βŠ£βŠ’β₯ŠβˆΎβ‰β†‘β†“β†•βŒ½β‰/β‹β’βŠβŠ‘βŠβŠ’βˆŠβ·βŠ”` -| Primitive Modifier | `` ˜˘¨⌜⁼´` `` -| Primitive Composition | `βˆ˜β—‹βŠΈβŸœβŒΎβŠ˜β—ΆβŽ‰βš‡βŸ` +| Primitive 1-Modifier | `` ˜˘¨⌜⁼´` `` +| Primitive 1-Modifier | `βˆ˜β—‹βŠΈβŸœβŒΎβŠ˜β—ΆβŽ‰βš‡βŸ` | Special name | `π•¨π•©π•—π•˜π•€π•Žπ•π”½π”Ύπ•Š` | Punctuation | `←↩→(){}βŸ¨βŸ©β€Ώβ‹„,` and newline diff --git a/spec/types.md b/spec/types.md index fa3f311e..0d142630 100644 --- a/spec/types.md +++ b/spec/types.md @@ -3,15 +3,15 @@ BQN programs manipulate data of six types: - Number - Array - Function -- Modifier -- Composition +- 1-Modifier +- 2-Modifier -Of these, the first three are considered *value types* and the remaining three *function types*. We first describe the much simpler function types; the remainder of this page will be dedicated to the value types. A member of any function type accepts some number of *inputs* and either returns a *result* or causes an error; inputs and the result are data of any type. When a function is given inputs (*called*), it may produce side effects before returning, such as manipulating variables and calling other functions within its scope, or performing I/O. +Of these, the first three are considered *data types* and the remaining three *operation types*. We first describe the operation types; the remainder of this page will be dedicated to the data types. A member of any operation type accepts some number of *inputs* and either returns a *result* or causes an error; inputs and the result are values of any type. When a function is given inputs (*called*), it may produce side effects before returning, such as manipulating variables and calling other functions within its scope, or performing I/O. - A *function* takes one (monadic call) or two (dyadic call) *arguments*. -- A *modifier* takes one *operand*. -- A *composition* takes two *operands*. +- A *1-modifier* takes one *operand*. +- A *2-modifier* takes two *operands*. -To begin the value types, a *character* is a [Unicode](https://en.wikipedia.org/wiki/Unicode) code point, that is, its value is a non-negative integer within the ranges defined by Unicode (however, it is distinct from this number as a BQN value). Characters are ordered by this numeric value. BQN deals with code points as abstract entities and does not use encodings such as UTF-8 or UTF-16. +To begin the data types, a *character* is a [Unicode](https://en.wikipedia.org/wiki/Unicode) code point, that is, its value is a non-negative integer within the ranges defined by Unicode (however, it is distinct from this number as a BQN value). Characters are ordered by this numeric value. BQN deals with code points as abstract entities and does not expose encodings such as UTF-8 or UTF-16. The precise type of a *number* may vary across BQN implementations or instances. A *real number* is a member of some supported subset of the [extended real numbers](https://en.wikipedia.org/wiki/Extended_real_number_line), that is, the real numbers and positive or negative infinity. Some system must be defined for rounding an arbitrary real number to a member of this subset, and the basic arithmetic operations add, subtract, multiply, divide, and natural exponent (base *e*) are defined by performing these operations on exact real values and rounding the result. The Power function (dyadic `⋆`) is also used but need not be exactly rounded. A *complex number* is a value with two real number *components*, a *real part* and an *imaginary part*. A BQN implementation can either support real numbers only, or complex numbers. -- cgit v1.2.3