diff options
Diffstat (limited to 'docs/spec/evaluate.html')
| -rw-r--r-- | docs/spec/evaluate.html | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/docs/spec/evaluate.html b/docs/spec/evaluate.html index 50e42796..8c99de10 100644 --- a/docs/spec/evaluate.html +++ b/docs/spec/evaluate.html @@ -18,12 +18,14 @@ <p>When a predicate <code><span class='String'>"?"</span></code> is evaluated, the associated <code><span class='Function'>EXPR</span></code> is evaluated and its result is checked. If it's not one of the numbers <code><span class='Number'>0</span></code> or <code><span class='Number'>1</span></code>, an error results. If it's <code><span class='Number'>1</span></code>, evaluation of the <code><span class='Function'>BODY</span></code> continues as usual. If it's <code><span class='Number'>0</span></code>, evaluation is stopped and the next compatible <code><span class='Function'>BODY</span></code> term is evaluated using the block's original inputs.</p> <p>If there is no left argument, but the <code><span class='Function'>BODY</span></code> contains <code><span class='Value'>๐จ</span></code> or <code><span class='Function'>๐</span></code> at the top level, then it is conceptually re-parsed with <code><span class='Value'>๐จ</span></code> replaced by <code><span class='Nothing'>ยท</span></code> to give a monadic version before application; this modifies the syntax tree by replacing some instances of <code><span class='Value'>subject</span></code>, <code><span class='Value'>arg</span></code>, or <code><span class='Function'>Operand</span></code> with <code><span class='Value'>nothing</span></code>. The token <code><span class='Function'>๐</span></code> is not allowed in this case and causes an error. Re-parsing <code><span class='Value'>๐จ</span></code> can also cause an error if it's used as an operand or list element, where <code><span class='Value'>nothing</span></code> is not allowed by the grammar. Note that these errors must not appear if the block is always called with two arguments. True re-parsing is not required, as the same effect can also be achieved dynamically by treating <code><span class='Nothing'>ยท</span></code> as a value and checking for it during execution. If it's used as a left argument, then the function should instead be called with no left argument (and similarly in trains); if it's used as a right argument, then the function and its left argument are evaluated but rather than calling the function <code><span class='Nothing'>ยท</span></code> is "returned" immediately; and if it's used in another context then it causes an error.</p> <h3 id="assignment"><a class="header" href="#assignment">Assignment</a></h3> -<p>An <em>assignment</em> is one of the four rules containing <code><span class='Function'>ASGN</span></code>. It is evaluated by first evaluating the right-hand-side <code><span class='Value'>subExpr</span></code>, <code><span class='Function'>FuncExpr</span></code>, <code><span class='Modifier'>_m1Expr</span></code>, or <code><span class='Modifier2'>_m2Exp_</span></code> 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 sets it equal to the result. For subjects, <em>destructuring assignment</em> is performed when an <code><span class='Value'>lhs</span></code> is <code><span class='Value'>lhsList</span></code> or <code><span class='Value'>lhsStr</span></code>. Destructuring assignment is performed recursively by assigning right-hand-side values to the left-hand-side targets, with single-identifier assignment as the base case. The target <code><span class='String'>"ยท"</span></code> is also possible in place of a <code><span class='Function'>NAME</span></code>, and performs no assignment.</p> -<p>The right-hand-side value, here called <code><span class='Value'>v</span></code>, in destructuring assignment must be a list (rank 1 array) or namespace. If it's a list, then each <code><span class='Function'>LHS_ENTRY</span></code> node must be an <code><span class='Function'>LHS_ELT</span></code>. The left-hand side is treated as a list of <code><span class='Value'>lhs</span></code> targets, and matched to <code><span class='Value'>v</span></code> element-wise, with an error if the two lists differ in length. If <code><span class='Value'>v</span></code> is a namespace, then the left-hand side must be an <code><span class='Value'>lhsStr</span></code> where every <code><span class='Function'>LHS_ATOM</span></code> is an <code><span class='Function'>NAME</span></code>, or an <code><span class='Value'>lhsList</span></code> where every <code><span class='Function'>LHS_ENTRY</span></code> is an <code><span class='Function'>NAME</span></code> or <code><span class='Value'>lhs</span> <span class='String'>"โ"</span> <span class='Function'>NAME</span></code>, so that it can be considered a list of <code><span class='Function'>NAME</span></code> nodes some of which are also associated with <code><span class='Value'>lhs</span></code> nodes. To perform the assignment, the value of each name is obtained from the namespace <code><span class='Value'>v</span></code>, giving an error if <code><span class='Value'>v</span></code> does not define that name. The value is assigned to the <code><span class='Value'>lhs</span></code> node if present (which may be a destructuring assignment or simple subject assignment), and otherwise assigned to the same <code><span class='Function'>NAME</span></code> node used to get it from <code><span class='Value'>v</span></code>.</p> +<p>An <em>assignment</em> is one of the four rules containing <code><span class='Function'>ASGN</span></code>. It is evaluated by first evaluating the right-hand-side <code><span class='Value'>subExpr</span></code>, <code><span class='Function'>FuncExpr</span></code>, <code><span class='Modifier'>_m1Expr</span></code>, or <code><span class='Modifier2'>_m2Exp_</span></code> 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 sets it equal to the result. For subjects, <em>destructuring assignment</em> is performed when an <code><span class='Value'>lhs</span></code> is <code><span class='Value'>lhsList</span></code>, <code><span class='Value'>lhsStr</span></code>, or <code><span class='Value'>lhsArray</span></code>. Destructuring assignment is performed recursively by assigning right-hand-side values to the left-hand-side targets, with single-identifier assignment as the base case. The target <code><span class='String'>"ยท"</span></code> is also possible in place of a <code><span class='Function'>NAME</span></code>, and performs no assignment.</p> +<p>In assignment to <code><span class='Value'>lhsList</span></code> or <code><span class='Value'>lhsStr</span></code>, the right-hand-side value, here called <code><span class='Value'>v</span></code>, must be a list (rank 1 array) or namespace. If it's a list, then each <code><span class='Function'>LHS_ENTRY</span></code> node must be an <code><span class='Function'>LHS_ELT</span></code>. The left-hand side is treated as a list of <code><span class='Value'>lhs</span></code> targets, and matched to <code><span class='Value'>v</span></code> element-wise, with an error if the two lists differ in length. If <code><span class='Value'>v</span></code> is a namespace, then the left-hand side must be an <code><span class='Value'>lhsStr</span></code> where every <code><span class='Function'>LHS_ATOM</span></code> is an <code><span class='Function'>NAME</span></code>, or an <code><span class='Value'>lhsList</span></code> where every <code><span class='Function'>LHS_ENTRY</span></code> is an <code><span class='Function'>NAME</span></code> or <code><span class='Value'>lhs</span> <span class='String'>"โ"</span> <span class='Function'>NAME</span></code>, so that it can be considered a list of <code><span class='Function'>NAME</span></code> nodes some of which are also associated with <code><span class='Value'>lhs</span></code> nodes. To perform the assignment, the value of each name is obtained from the namespace <code><span class='Value'>v</span></code>, giving an error if <code><span class='Value'>v</span></code> does not define that name. The value is assigned to the <code><span class='Value'>lhs</span></code> node if present (which may be a destructuring assignment or simple subject assignment), and otherwise assigned to the same <code><span class='Function'>NAME</span></code> node used to get it from <code><span class='Value'>v</span></code>.</p> +<p>Assignment to <code><span class='Value'>lhsArray</span></code> destructures the major cells of right-hand-side value <code><span class='Value'>v</span></code>, which must be an array of rank at least 1. The number of cells in <code><span class='Value'>v</span></code> is its length <code><span class='Value'>l</span></code>, that is, the first element of its shape. The shape of each is the shape of <code><span class='Value'>v</span></code> without its first element, and the cell ravels are formed by splitting <code><span class='Value'>v</span></code>'s ravel evenly into <code><span class='Value'>l</span></code> sections. Besides this difference in how <code><span class='Value'>v</span></code> is divided, assignment behaves the same way as assignment of a list <code><span class='Value'>v</span></code> to <code><span class='Value'>lhsList</span></code>.</p> <p>A destructuring assignment is performed in program order, or equivalently index order, with each sub-assignment fully completed before beginning the next (a depth-first order). Thus if an assignment with <code><span class='Gets'>โฉ</span></code> encounters an error but it's caught with <code><span class='Modifier2'>โ</span></code>, some of the assignment may have already been performed, changing variable values.</p> <p><em>Modified assignment</em> is the subject assignment rule <code><span class='Value'>lhs</span> <span class='Function'>Derv</span> <span class='String'>"โฉ"</span> <span class='Value'>subExpr</span><span class='Head'>?</span></code>. In this case, <code><span class='Value'>lhs</span></code> is evaluated as if it were a <code><span class='Value'>subExpr</span></code> (the syntax is a subset of <code><span class='Value'>subExpr</span></code>), and passed as an argument to <code><span class='Function'>Derv</span></code>. The full application is <code><span class='Value'>lhs</span> <span class='Function'>Derv</span> <span class='Value'>subExpr</span></code>, if <code><span class='Value'>subExpr</span></code> is given, and <code><span class='Function'>Derv</span> <span class='Value'>lhs</span></code> otherwise. Its value is assigned to <code><span class='Value'>lhs</span></code>, and is also the result of the modified assignment expression.</p> <h3 id="expressions"><a class="header" href="#expressions">Expressions</a></h3> -<p>We now give rules for evaluating an <code><span class='Value'>atom</span></code>, <code><span class='Function'>Func</span></code>, <code><span class='Modifier'>_mod1</span></code> or <code><span class='Modifier2'>_mod2_</span></code> expression (the possible options for <code><span class='Function'>ANY</span></code>). A literal or primitive <code><span class='Value'>sl</span></code>, <code><span class='Function'>Fl</span></code>, <code><span class='Modifier'>_ml</span></code>, or <code><span class='Modifier2'>_cl_</span></code> has a fixed value defined by the specification (<a href="literal.html">literals</a> and <a href="primitive.html">built-ins</a>). An identifier <code><span class='Value'>s</span></code>, <code><span class='Function'>F</span></code>, <code><span class='Modifier'>_m</span></code>, or <code><span class='Modifier2'>_c_</span></code>, if not preceded by <code><span class='Value'>atom</span> <span class='String'>"."</span></code>, must have an associated variable due to the scoping rules, and returns this variable's value, or causes an error if it has not yet been set. If it is preceded by <code><span class='Value'>atom</span> <span class='String'>"."</span></code>, then the <code><span class='Value'>atom</span></code> node is evaluated first; its value must be a namespace, and the result is the value of the identifier's name in the namespace, or an error if the name is undefined. A parenthesized expression such as <code><span class='String'>"("</span> <span class='Modifier'>_modExpr</span> <span class='String'>")"</span></code> simply returns the result of the interior expression. A block is defined by the evaluation of the statements it contains after all parameters are accepted, as described above. Finally, a list <code><span class='String'>"โจ"</span> <span class='Separator'>โ</span><span class='Head'>?</span> <span class='Paren'>(</span> <span class='Paren'>(</span> <span class='Function'>EXPR</span> <span class='Separator'>โ</span> <span class='Paren'>)</span><span class='Value'>*</span> <span class='Function'>EXPR</span> <span class='Separator'>โ</span><span class='Head'>?</span> <span class='Paren'>)</span><span class='Head'>?</span> <span class='String'>"โฉ"</span></code> or <code><span class='Function'>ANY</span> <span class='Paren'>(</span> <span class='String'>"โฟ"</span> <span class='Function'>ANY</span> <span class='Paren'>)</span><span class='Function'>+</span></code> 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.</p> +<p>We now give rules for evaluating an <code><span class='Value'>atom</span></code>, <code><span class='Function'>Func</span></code>, <code><span class='Modifier'>_mod1</span></code> or <code><span class='Modifier2'>_mod2_</span></code> expression (the possible options for <code><span class='Function'>ANY</span></code>). A literal or primitive <code><span class='Value'>sl</span></code>, <code><span class='Function'>Fl</span></code>, <code><span class='Modifier'>_ml</span></code>, or <code><span class='Modifier2'>_cl_</span></code> has a fixed value defined by the specification (<a href="literal.html">literals</a> and <a href="primitive.html">built-ins</a>). An identifier <code><span class='Value'>s</span></code>, <code><span class='Function'>F</span></code>, <code><span class='Modifier'>_m</span></code>, or <code><span class='Modifier2'>_c_</span></code>, if not preceded by <code><span class='Value'>atom</span> <span class='String'>"."</span></code>, must have an associated variable due to the scoping rules, and returns this variable's value, or causes an error if it has not yet been set. If it is preceded by <code><span class='Value'>atom</span> <span class='String'>"."</span></code>, then the <code><span class='Value'>atom</span></code> node is evaluated first; its value must be a namespace, and the result is the value of the identifier's name in the namespace, or an error if the name is undefined. A parenthesized expression such as <code><span class='String'>"("</span> <span class='Modifier'>_modExpr</span> <span class='String'>")"</span></code> simply returns the result of the interior expression. A block is defined by the evaluation of the statements it contains after all parameters are accepted, as described above.</p> +<p>A list <code><span class='String'>"โจ"</span> <span class='Separator'>โ</span><span class='Head'>?</span> <span class='Paren'>(</span> <span class='Paren'>(</span> <span class='Function'>EXPR</span> <span class='Separator'>โ</span> <span class='Paren'>)</span><span class='Value'>*</span> <span class='Function'>EXPR</span> <span class='Separator'>โ</span><span class='Head'>?</span> <span class='Paren'>)</span><span class='Head'>?</span> <span class='String'>"โฉ"</span></code> or <code><span class='Function'>ANY</span> <span class='Paren'>(</span> <span class='String'>"โฟ"</span> <span class='Function'>ANY</span> <span class='Paren'>)</span><span class='Function'>+</span></code> 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. The square bracket notation <code><span class='String'>"["</span> <span class='Separator'>โ</span><span class='Head'>?</span> <span class='Paren'>(</span> <span class='Function'>EXPR</span> <span class='Separator'>โ</span> <span class='Paren'>)</span><span class='Value'>*</span> <span class='Function'>EXPR</span> <span class='Separator'>โ</span><span class='Head'>?</span> <span class='String'>"]"</span></code> evaluates expressions in the same way, but makes them into major cells of an array instead of elements. The result is identical to applying the <a href="primitive.html">primitive</a> function Merge (<code><span class='Function'>></span></code>) to a list of the expression results.</p> <p>Rules in the table below are function and modifier evaluation.</p> <table> <thead> |
