diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-07-07 16:07:15 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-07-07 16:09:49 -0400 |
| commit | 85c54f4c22897972025d76502b9e305541ec5a6e (patch) | |
| tree | 754479d1800e086eefa1c6369d8f5abce90df251 /spec/evaluate.md | |
| parent | a9d01bc7c2be4b66595af69d53ff015bf5da2023 (diff) | |
Specify headers (still too much hand-waving)
Diffstat (limited to 'spec/evaluate.md')
| -rw-r--r-- | spec/evaluate.md | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/spec/evaluate.md b/spec/evaluate.md index 03edc813..d4e345d9 100644 --- a/spec/evaluate.md +++ b/spec/evaluate.md @@ -2,14 +2,28 @@ This page describes the semantics of the code constructs whose grammar is given Here we assume that the referent of each identifier, or equivalently the connections between identifiers, have been identified according to the [scoping rules](scope.md). -A `PROGRAM` or `BRACED` block is a list of `STMT`s (for `BRACED`, 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. +### Programs and blocks -One additional rule for `BRACED` blocks makes it easier to define ambivalent functions. Each such block that contains `๐จ` at the top level is parsed normally to give a dyadic function, but is also parsed a second time with all instances of `๐จ` replaced by `ยท` to give a monadic function (as the only effect is to change some instances of `arg` to `nothing`, this can be achieved efficiently by annotating parts of the AST that depend on `๐จ` as conditionally-nothing). When called, the dyadic function is used if both arguments are given and the monadic one is used if there is only one. This applies to modifiers and compositions written with `๐จ` as well, where the choice of which version to use is made when the derived function is called. +The result of parsing a valid BQN program is a `PROGRAM`, and the program is run by evaluating this term. + +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. + +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. + +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. + +If there is no left argument, but the `BODY` contains `๐จ` at the top level, then it is conceptually re-parsed with `๐จ` replaced by `ยท` to give a monadic version before application. As the only effect when this re-parsed form is valid is to change some instances of `arg` to `nothing`, this can be achieved efficiently by annotating parts of the AST that depend on `๐จ` as conditionally-nothing. However, it also causes an error if `๐จ` is used as an operand or list element, where `nothing` is not allowed by the grammar. + +### 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. *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. +### 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. Rules in the table below are function and operator evaluation. |
