Archive-name: ForthFaq/CASE_ENDCASE Last-modified: 30.Jan.93 Version: 1.1 [This is a message that I thought deserved to be preserved. -dwp] From: eaker@ukulele.crd.ge.com (Chuck Eaker) Subject: Re: Wanted .. CASE,OF,ENDOF,ENDCASE Message-ID: <1992Nov25.164255.23225@crd.ge.com> Date: 25 Nov 92 16:42:55 GMT In article , jax@well.sf.ca.us (Jack J. Woehr) writes: |> In <1992Nov24.101857.84@wronz.org.nz> mentink@wronz.org.nz writes: |> |> |> |> > Can anyone help with source ( masm/forth) to the CASE statement |> > word set. I.E .... CASE OF ENDOF ENDCASE .... |> |> Baden's CASE is in FORTH Dimensions VIII/5. |> |> Eaker, who wrote and enduring CASE construct, checks into |> this newsgroup once and a while. Charles? |> |> =jax= |> -- |> # jax@well.{UUCP,sf.ca.us} # # Member # # Vice President, # |> # du!isis!koscej!jax # # X3J14 TC # # Forth Interest Group # |> # JAX on GEnie # # for ANS # # P.O. Box 8231 # |> # SYSOP RCFB (303) 278-0364 # # Forth # # San Jose CA 95155 # 1. FIG-Forth Here is the source for FIG-Forth published with the original article (Forth Dimensions, Vol. II, No. 3, pp. 37-40.). The ?PAIRS word was FIG-Forth's way of implementing a small amount of syntax checking. : CASE ?COMP CSP @ !CSP 4 ; IMMEDIATE : OF 4 ?PAIRS COMPILE OVER COMPILE = COMPILE OBRANCH HERE 0 , COMPILE DROP 5 ; IMMEDIATE : ENDOF 5 ?PAIRS COMPILE BRANCH HERE 0 , SWAP 2 [COMPILE] ENDIF 4 ; IMMEDIATE : ENDCASE 4 ?PAIRS COMPILE DROP BEGIN SP@ CSP @ = 0= WHILE 2 [COMPILE ENDIF REPEAT CSP ! ; IMMEDIATE 1a. Here is additional source for FIG-Forth published in Forth Dimensions, Vol. III, No. 6, pp. 187-188 in an article by Alfred J. Monroe. He adds a primitive compiled by OF which reduces the amount of code compiled by OF. Use the definitions of CASE, ENDOF, and ENDCASE given above. : (OF) OVER = IF DROP 1 ELSE 0 ENDIF ; : OF 4 ?PAIRS COMPILE (OF) COMPILE 0BRANCH HERE 0 , 5 ; IMMEDIATE Mr. Monroe also gave code for some interesting variants: : ( IF DROP 1 ELSE 0 ENDIF ; : OF) OVER > IF DROP 1 ELSE 0 ENDIF ; : >OF 4 ?PAIRS COMPILE (>OF) COMPILE 0BRANCH HERE 0 , 5 ; IMMEDIATE : RANGE >R OVER DUP R> 1+ < IF SWAP 1- > IF DROP 1 ELSE 0 ENDIF ELSE DROP DROP 0 ENDIF ; : RNG-OF 4 ?PAIRS COMPILE RANGE COMPILE 0BRANCH HERE 0 , 5 ; IMMEDIATE 1b. It is quite common to define (OF) as a CODE word and have it combine the functions of the run-time (OF) and the compile-time 0BRANCH in the previous definitions. This reduces the amount of compiled code even more. CODE (OF) ( 1. Remove the top element of the stack and call it A. 2. If A equals the new top element of the stack, remove the new top element of the stack, skip over the branch vector, and execute the code which follows it. Else continue execution at the location indicated by the branch vector. ) END-CODE : OF 4 ?PAIRS COMPILE (OF) HERE 0 , 5 ; IMMEDIATE 2. dpANS-3 dpANS-3 contains the following definitions (p. 133) to illustrate control structure extension. Note that it would be quite easy to optimize OF along the lines suggested above. Note also that there is no syntax checking. These words may appear anywhere and not necessarily combined with each other. In fact, ENDOF may be dispensed with entirely and replaced with ELSE. Compile-time monitoring of the syntax of control structure words is a perennial Forth problem. 0 CONSTANT CASE IMMEDIATE ( init count of OFs ) : OF ( #of -- orig #of+1 / x -- ) 1+ ( count OFs ) >R ( move off the stack in case the control-flow ) ( stack is the data stack. ) POSTPONE OVER POSTPONE = ( copy and test case value ) POSTPONE IF ( add orig to control flow stack ) POSTPONE DROP ( discards case value if = ) R> ; ( we can bring count back now ) IMMEDIATE : ENDOF ( orig1 #of -- orig2 #of ) >R ( move off the stack in case the control-flow ) ( stack is the data stack. ) POSTPONE ELSE R> ; ( we can bring count back now ) IMMEDIATE : ENDCASE ( orig 1..orign #of -- ) POSTPONE DROP ( discard case value ) 0 ?DO POSTPONE THEN LOOP ; IMMEDIATE -- Chuck Eaker / P.O. Box 8, K-1 3C12 / Schenectady, NY 12301 USA eaker@crd.ge.com eaker@crdgw1.UUCP (518) 387-5964 --- If you have any questions about ForthNet/comp.lang.forth or any information to add/delete or correct in this message or any suggestions on formatting or presentation, please contact Doug Philips at one of the following addresses: Internet: dwp@willett.pgh.pa.us Usenet: ...!uunet!willett.pgh.pa.us!dwp GEnie: D.PHILIPS3