HEADER OF SWITCH SWITCH CELL | DOSWITCH CFA of SWITCH | SWITCH LINK First case/null ---> SWITCH LINK Next case/null ---> | XT Default COMPARATOR Value to match | SWITCHES LINK Next switch/null __ XT Word to run if matched V | V
.SWITCH xt -- FORTH SYSTEMPrint out all the conditions defined for this switch.Using SEE -< name >- on a switch has the same effect.Conditions are listed default first followed by the others with in the order they are found i.e. the later they are defined the earlier they are in the list.
.SWITCHES FORTH SYSTEMPrint out all the defined switches.The more recently a switch has been defined the earlier it comes in the list.
:SWITCH xt -<name>- -- FORTH SYSTEMDefine a switch -<name>- that executes the procedure whose xt is on the stack ( with the input argument on top of the stack ) if no matching condition has been added to the switch and open it for adding conditions.
RUN: head n -<words ;>- -- head FORTH SYSTEMAdd a condition to the currently open switch structure that runs the following forth words up to ; if the value n is passed to the switch. An error occurs if head is not the head of the currently open switch, or no switch is open.
RUNS head n -<word>- -- head FORTH SYSTEMAdd a condition to the currently open switch structure that runs -<word>- if the value n is passed to the switch. An error occurs if head is not the head of the currently open switch, or no switch is open.
SWITCH: -<name>- -<words ;>- -- head FORTH SYSTEMDefine a switch -<name>- that executes the following forth words up to ; if no matching condition has been added to the switch and open it for adding conditions.The forth words can call the switch -<name>- as a recursive call either with -<name>- or RECURSE.
SWITCH] head -- FORTH SYSTEMClose the currently open switch structure. An error occurs if head is not the head of the currently open switch.
[+SWITCH -<switch>- -- head FORTH SYSTEMOpen the named switch for additional conditions.
[SWITCH -<name>- -<default>- -- head FORTH SYSTEMDefine a switch -<name>- that executes -<default>- if no matching condition has been added to the switch and open it for adding conditions.
SWITCH: FACTORIAL ( n -- factorial )
dup 1- factorial * ; \ Recursive call n <> 1 and n <> 0
0 runs 1 \ Base conditions:Note 1 is a constant
1 runs 1
switch]
An implimentation of the classic recursive FACTORIAL function using SWITCH: that is about 15% faster than the standard recursive version, while only taking 16 cells ( the same as the standard version ).
[SWITCH FOO-ERROR throw SWITCH]
: (FOO) -<Some code>- ;
: FOO ['] (foo) catch foo-error ;
An extensible error handler. Initially any errors are simply thrown to the previous CATCH but adding extra conditions means they are caught and handled by FOO, since 0 THROW drops the 0 and carries on. You can even add a success condition e.g.
[+SWITCH FOO-ERROR 0 run: ." Hip, Hip, Hoorah I've done it" ; SWITCH]
if you want.
Back to TopDocument $Id: p-switch.htm,v 1.1 2004/12/21 00:18:57 alex_mcdonald Exp $