[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[no subject]

I have a simple question for you T buffs:

In my work on program transformations I find loops with multiple exits very
useful. These are loops which can only be terminated by the execution of an
"exit" statement, of the form exit(n) where n is an integer (not a variable
or an expression). Execution of such a statement causes immediate termination
of the "n" enclosing loops with execution continuing after the nth loop. The
statement exit(0) is therefore a null statement.
Wishing to implement this in T, I came up with the following syntax:

(define-syntax (loop . body)
    `(let ((tv (catch exit (iterate again () ,@body (again)))))
      (if (= 1 tv)
          (exit (-1+ tv)))))

The iterate statement implements the loop by tail recursion of the
parameterless procedure "again". This is enclosed in a "catch" form which
catches the execution of any exit and records the "terminal value" in
variable tv. If tv > 1 then we must terminate the next enclosing loop also,
and this is achieved by executing (exit -1+ tv) - which will presumably be
caught by another enclosing loop.

This implementation works (apart from (exit 0) which is not ignored).
Exiting with a larger value of n than there are enclosing loops is an error,
with this implementation it causes termination of the invokation of T
(there is a certain logic about that!) since that is the normal effect of
an (exit) call.

I am wondering how efficient this is in practice, normally one would
translate an exit into a simple goto, there seems to be a problem with
generating the labels and deciding which label to jump to. How would you
define this?


JANET: martin@uk.ac.dur.easby    BITNET: martin%dur.easby@ac.uk
UUCP:  ...!mcvax!ukc!easby!martin