Win32Forth

Using Switches

Switches are a cross between chains and case structures. Like chains they can be defined where they need to be compiled and extended by later code. Like case structures they perform different operations depending on the value on the top of the stack. Unlike CASE the comparators are stored as a single cell so cannot be a non-constant value. Also they pass the input value to a default case if no match is found in the linked cases, whereas with CASE one has to add code to handle a default case.

STRUCTURE OF A SWITCH

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

GLOSSARY

.SWITCH  xt --                            FORTH            SYSTEM
Print 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            SYSTEM
Print out all the defined switches.The more recently a switch has been defined the earlier it comes in the list.
:SWITCH  xt -<name>- --                   FORTH            SYSTEM
Define 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            SYSTEM
Add 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            SYSTEM
Add 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            SYSTEM
Define 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            SYSTEM
Close the currently open switch structure. An error occurs if head is not the head of the currently open switch.
[+SWITCH    -<switch>- -- head            FORTH            SYSTEM
Open the named switch for additional conditions.
[SWITCH     -<name>- -<default>- -- head  FORTH            SYSTEM
Define a switch -<name>- that executes -<default>- if no matching condition has been added to the switch and open it for adding conditions.

SOME EXAMPLES OF SWITCHES


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 Top

Document $Id: p-switch.htm,v 1.1 2004/12/21 00:18:57 alex_mcdonald Exp $