% -*-LaTeX-*- % TECOEXPR.LTX.15, 28-May-86 13:43:53, Edit by BEEBE % This is part of TECO.LTX \TECO{} is essentially an expression language; every \TECO{} function returns 0, 1, or 2 values which become arguments to the next function executed. It is somewhat unusual in that the expressions are not restricted to numeric values; they can also be strings. Numeric expressions are constructed from arithmetic and bitwise operators acting on terms which can be numeric variables, constants, or value-returning function references. Some examples using numeric constants are \T{3*5}, \T{3+12}, \T{27-12}, \T{47/3}, \T{63\&15}, \T{32\CARET{}8\#37}, \T{7|8}, \T{\CC{}O\&15}, all of which evaluate to \T{15}. Examples with variables and functions are \T{Q0*Q..L+3}, \T{-1,0A-\CC{}B}, and \T{@:S/foo/+1}. String expressions can be given in function arguments, as in \T{@S/foo/}, but pre-comma and post-comma arguments can be produced only by function return, the most notable being \T{:Gq}, \T{:I*}, \T{FP}, \T{X*}, and \T{n\BS{}}, which return strings from a Q-register, the \TECO{} program, the keyboard, the edit buffer, and a number, respectively. The \TECO{} interpreter is essentially nothing but a loop over the \TECO{} program, with a call to the expression evaluator in each iteration. Because \TECO{} is parsed left to right, one character at a time, with no lookahead, numeric expressions are handled the same way, and \TECO{} arithmetic and bitwise operators therefore have equal precedence and guaranteed left-to-right evaluation. This is unusual in programming languages, with Ken Iverson's {\sc apl} being the other major established example, although it evaluates from right to left. When a particular evaluation order must be enforced, parentheses come to the rescue; they cause evaluation of the functions inside the parentheses to be effectively completed before outside operations are acted upon. That way, although \T{2 + 3 * 5} reduces to \T{25}, writing it as \T{2 + (3 * 5)} will produce the value \T{17} expected from most other programming languages. Parentheses are more general, however, because they can be combined with the \X{comma} operator to cause evaluation of two or more subexpressions,\footnote{ \ETECO{} restricts parenthesized lists to no more than two subexpressions, but it became clear during the development of this \TECO{} that there is no reason to do so.} with the values returned by the closing parenthesis being the rightmost two subexpressions. This is analogous to comma expressions in \C{}. What the comma operator does is nothing more than move the current post-comma argument to the pre-comma argument, and empty the post-comma argument. Any pending numeric operation acts only on the post-comma argument, and leaves any pre-comma argument intact. Here are some examples: \begin{flushleft} \begin{tabular}{rl} \T{(,)} & returns nothing\\ \T{(1,)} & returns \T{1,}\\ \T{(,2)} & returns \T{,2}\\ \T{(1,2)} & returns \T{1,2}\\ \T{1,(2)} & returns \T{1,2}\\ \T{2(1,)} & returns \T{1,2}\\ \T{(1,2,3,4)} & returns \T{3,4}\\ \T{((1,2),(3,4))} & returns \T{3,4}\\ \T{3+(1,2)} & returns \T{1,5}\\ \T{3*(1,2)} & returns \T{1,6}\\ \T{3-(,2)} & returns \T{,1}\\ \T{3*(2)} & returns \T{6}\\ \T{3,@:I*/foo/} & returns \T{3,foo} \end{tabular} \end{flushleft} In case you find these confusing, let us walk through the evaluation of \T{3+(1,2)}. First \T{3} is recognized and becomes the new post-comma argument. Next, \T{+} is met and the evaluator is called to get the value of the next expression which will be added to the post-comma argument. It finds the left parenthesis, so it saves the current pre- and post-comma arguments before beginning evaluation of the parenthesized list. Then \T{1} becomes a new post-comma argument, after which the comma moves it to the pre-comma argument, and \T{2} becomes the post-comma argument. Finally, the left parenthesis terminates the expression list, the pending \T{3} is added to the post-comma argument, \T{2}, giving a new post-comma argument \T{5}, and the existing pre-comma argument is left intact, so the final result is \T{1,5}. The evaluator was called six times in the process, and the calls inside the parentheses were recursive. The \X{colon} and \X{at-sign} \X{modifier}s are saved when the open parenthesis is met, then cleared. When the close parenthesis is met, the saved modifiers are merged with their current settings. They will be set on exit from the close parenthesis if they were on when either of the parentheses were met.