Programming Language for Old Timers

by David A. Moon
February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.

Previous page   Table of Contents   Next page

Iteration Statements

The language semantics require tail recursion removal, so iteration can be accomplished simply by writing a recursive function. However, for readability several iteration statements are defined by macros. These do not have to be part of the PLOT language per se, they are a library that could have been written by a user. However it is likely that most programs use that library.

defmacro while ?test ?:loop-body => ...
defmacro until ?test ?:loop-body => ...

These repeatedly execute the body zero or more times until the test is false or true, respectively. The scope of any definition in the test includes the body. The entire statement is enclosed in a block. The result is always false unless result: appears in the body.

defmacro for { ?:for-clause & ~^ , }+ ?:loop-body => ...

A for statement begins with one or more clauses, separated by commas. The remainder of the statement is the body. The entire statement is enclosed in a block. (There is not actually a syntactic type named for-clause.)

Each clause can be an iteration clause or an endtest clause. An endtest clause specifies a test that controls whether the iteration repeats. An iteration clause specifies a variable and a set of values that it will assume in successive iterations. All iteration variables take on their values in parallel. An iteration clause can also imply an endtest.

The body is in the scope of additional "result" statements, defined by local macro definitions, that allow control over the value(s) of the for statement. The default value is false if these are not used and result: does not appear in the body.

The full details of the for statement are in the next section.


  for x in sequence, i from 0, while test?(x)
    if test2?(i) collect x

The syntactic type loop-body used in iteration statements is defined as follows:

defclass loop-body
  body is expression                    ; the main part of the body
  result is expression or false         ; result expression if any
  cleanup is expression or false        ; cleanup body if any
  exit is name or false                 ; exit function if any

defparser loop-body [ exit: ?exit-function is name ]
                    { ^ ?expr }+
                    [ result: ?result ]
                    [ cleanup: ?cleanup is block ] =>
  loop-body(if length(expr) = 1 then expr[0] else collation(expr...),
            result, cleanup, exit-function)

The syntax and semantics of a loop-body are similar but not identical to those of an ordinary body.

The expr expressions are the main part of the body. They are executed and their values are ignored by each iteration of the iteration statement. They are enclosed in a block.

If an exit-function is specified, calling that function immediately returns the arguments as the values of the whole iteration statement.

If a result expression is specified, it is evaluated when iteration terminates normally and its values are returned as the values of the whole iteration statement.

If a cleanup block is specified, it is executed and its values are ignored when control leaves the iteration statement, whether normally or abnormally.

Previous page   Table of Contents   Next page