From: George H. <geo...@us...> - 2008-05-16 09:16:38
|
Update of /cvsroot/win32forth/win32forth/doc In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv32533/win32forth/doc Modified Files: p-switch.htm Log Message: gah: replaced by dex-generated version. Index: p-switch.htm =================================================================== RCS file: /cvsroot/win32forth/win32forth/doc/p-switch.htm,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** p-switch.htm 21 Dec 2004 00:18:57 -0000 1.1 --- p-switch.htm 16 May 2008 09:16:34 -0000 1.2 *************** *** 1,138 **** ! <html> ! <head> ! <meta http-equiv="Content-Language" content="en-gb"> ! <meta name="GENERATOR" content="Chami HTML-Kit 1.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> ! <title>Using Switches</title> ! <style> ! <!-- h1 { font-family: Tahoma; font-size: 24pt; font-weight: bold } ! h2 { font-family: Tahoma; font-size: 18pt; font-weight: bold } ! --> </style> </head> - <body> - - <h1 align="center"><img border="0" src="FORTHPRO.gif" width="32" height="32"> - Win32Forth</h1> - <h2 align="center"><font face="Tahoma"> - <a href="mailto:win...@ya...?subject=DOC: Doc error in $Id$"> - <img border="0" src="TELLUS.gif" align="left" width="32" height="32"></a>Using - Switches</font></h2> - - 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. - - <h3> STRUCTURE OF A SWITCH </h3> - - <big><pre> - <B>HEADER OF SWITCH SWITCH CELL</B> - | 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 - </pre></big> - <h3> GLOSSARY </h3> - <big><pre> - .SWITCH xt -- FORTH SYSTEM - </pre></big> - 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. - <big><pre> - .SWITCHES FORTH SYSTEM - </pre></big> - Print out all the defined switches.The more recently a switch has been defined - the earlier it comes in the list. - <big><pre> - :SWITCH xt -<name>- -- FORTH SYSTEM - </pre></big> - 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. - <big><pre> - RUN: head n -<words ;>- -- head FORTH SYSTEM - </pre></big> - 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. - <big><pre> - RUNS head n -<word>- -- head FORTH SYSTEM - </pre></big> - 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. - <big><pre> - SWITCH: -<name>- -<words ;>- -- head FORTH SYSTEM - </pre></big> - 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. - <big><pre> - SWITCH] head -- FORTH SYSTEM - </pre></big> - Close the currently open switch structure. An error occurs if head is not the - head of the currently open switch. - <big><pre> - [+SWITCH -<switch>- -- head FORTH SYSTEM - </pre></big> - Open the named switch for additional conditions. - <big><pre> - [SWITCH -<name>- -<default>- -- head FORTH SYSTEM - </pre></big> - Define a switch -<name>- that executes -<default>- if no matching condition has - been added to the switch and open it for adding conditions. - - <h3> SOME EXAMPLES OF SWITCHES </h3> - <pre><code> - 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] - </code></pre> - <p> - 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 ). - </p> - <code><pre> [SWITCH FOO-ERROR throw SWITCH] ! : (FOO) -<Some code>- ; : FOO ['] (foo) catch foo-error ; ! </pre></code> ! <p> ! 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. ! </p> ! <code> ! [+SWITCH FOO-ERROR 0 run: ." Hip, Hip, Hoorah I've done it" ; SWITCH] ! </code> ! <p> ! if you want. ! </p> ! <a href=#top>Back to Top</a> ! <hr> ! <p>Document $Id$</p> ! <p> </p> ! <p> </p> ! </body> ! </html> --- 1,86 ---- ! <?xml version="1.0"?> ! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ! "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> ! <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> ! <meta name="GENERATOR" content="dexh v03"> <meta name="ProgId" content="FrontPage.Editor.Document"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> ! <title> ! W32F switch</title><style><!-- h1 { font-family: Tahoma; font-size: 24pt; font-weight: bold } ! h2 { font-family: Tahoma; font-size: 18pt; font-weight: bold } --> </style> </head> + <body><h1 align="center"> + <a href="mailto:win...@ya...?subject=DOC:Doc error in $Id$"> + <img border="0" src="TELLUS.gif" align="left" width="32" height="32"></a> + <img border="0" src="FORTHPRO.gif" + width="32" height="32"> Win32Forth</h1> + <hr /><h1>Using Switches + </h1><hr /><p>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. For more information on switches see + Forth Dimensions Volume 20 Issue 3 (Page 19 onwards). + </p><h2>STRUCTURE OF A SWITCH + </h2><pre>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 + </pre><h2>GLOSSARY + </h2><pre><b><a name="0">: :SWITCH ( xt -<name>- -- head ) FORTH SYSTEM + </a></b></pre><p>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. + </p><pre><b><a name="1">: [SWITCH ( -<name>- -<default>- -- head ) FORTH SYSTEM + </a></b></pre><p>Define a new switch -<name>- whose default action is -<default>- and open + it for adding conditions. + </p><pre><b><a name="2">: SWITCH: ( -<name>- -<code ;>- -- head ) FORTH SYSTEM + </a></b></pre><p>Define a new switch -<name>- whose default action is the following inline + forth code (up to the terminating ;). The forth words can call the switch + -<name>- as a recursive call with -<name>- . + </p><pre><b><a name="3">: [+SWITCH ( -<name>- -- head ) FORTH SYSTEM + </a></b></pre><p>Open existing SWITCH -<name>- for additional conditions. + </p><pre><b><a name="4">: SWITCH] ( head -- ) FORTH SYSTEM + </a></b></pre><p>Close SWITCH structure. An error occurs if head is not the head of the currently open switch. + </p><pre><b><a name="5">: RUNS ( head n -<word>- -- head ) FORTH SYSTEM + </a></b></pre><p>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. + </p><pre><b><a name="6">: RUN: ( head n -<words ;>- -- head ) FORTH SYSTEM + </a></b></pre><p>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. + </p><pre><b><a name="7">: .SWITCH ( xt -- ) FORTH SYSTEM + </a></b></pre><p>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. + </p><pre><b><a name="8">: .SWITCHES FORTH SYSTEM + </a></b></pre><p>Print out all the defined switches.The more recently a switch has been defined the earlier it comes in the + list. + </p><h2>SOME EXAMPLES OF SWITCHES + </h2><pre>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) -&ltSome 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. ! ! </pre><hr><p>Document $Id$</p> ! </body></html> |