Because the world wasn't weird enough already I took the liberty to
implement goto in picolisp. This works by using a "trampoline" based on
'throw and 'catch, each goto makes the evaluation "bounce" and continue
from the point of the next label until it falls trough.

(de goto (L)
   (throw 'goto L) )

(de prog+ "Prg"
   (use ("Thunk" "Ret" "L")
      (setq "Thunk" "Prg" "Ret" NIL)
      (while
         (catch 'goto
            (setq "Ret" (run "Thunk")) )
         (setq "L" @
            "Thunk" (seek '[(X) (= (car X) "L")] "Prg") ) )
      "Ret") )
(====)

Labels should be atoms (though they can be anything that can be
compared with = and would have no side effect when evaluated.)

A couple examples:

Nested loop:

(prog+
   (zero I J)
 loop:
   (prinl I "," J)
   (when (< (inc 'I) 4)
      (goto 'loop:) )
   (when (< (inc 'J) 4)
      (zero I)
      (goto 'loop:) ) ) 

Infinite loop:

(prog+
   10 (prinl "cyborg_ar is a genius!")
   20 (goto 10) )

"Computed" goto:

(prog+
   (goto 'ask:)
 err:
   (prinl "^JSorry, i quite didn't get that...")
 ask:
   (prinl "Choose a number from 0-3:")
   (goto (or (min (format (line)) 4) 'err:))
 0 (prinl "You chose zero.") (goto 'end:)
 1 (prinl "You chose one.") (goto 'end:)
 2 # Fall trough 
 3 (prinl "You chose two or three.") (goto 'end:)
 4 (goto 'err:)
 end:
   (prinl "Bye!") )

Cheers,
José

PS: I leave the implementation of COMEFROM as an exercise for the
reader.
--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to