home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / forth / 3737 < prev    next >
Encoding:
Internet Message Format  |  1993-01-01  |  5.0 KB

  1. Path: sparky!uunet!paladin.american.edu!gatech!swrinde!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu!drycas.club.cc.cmu.edu!pitt.edu!pitt!willett!FAQ
  2. Newsgroups: comp.lang.forth
  3. Subject: Forth FAQ Answers: Part 91 (CASE,OF,ENDOF,ENDCASE).  (V1.0:01.Jan.93)
  4. Message-ID: <4223.UUL1.3#5129@willett.pgh.pa.us>
  5. From: FAQ@willett.pgh.pa.us (comp.lang.forth FAQ source)
  6. Date: 2 Jan 93 00:39:47 GMT
  7. Organization: EIEI-U
  8. Lines: 122
  9.  
  10.  
  11.   [This is a message that I thought deserved to be preserved.  -dwp]
  12.  
  13.  
  14.  
  15.     From: eaker@ukulele.crd.ge.com (Chuck Eaker)
  16.     Subject: Re: Wanted .. CASE,OF,ENDOF,ENDCASE
  17.     Message-ID: <1992Nov25.164255.23225@crd.ge.com>
  18.     Date: 25 Nov 92 16:42:55 GMT
  19.  
  20.     In article <jax.722669998@well.sf.ca.us>,
  21.         jax@well.sf.ca.us (Jack J. Woehr) writes:
  22.     |> In <1992Nov24.101857.84@wronz.org.nz> mentink@wronz.org.nz writes:
  23.     |> 
  24.     |> 
  25.     |> 
  26.     |> >    Can anyone help with source ( masm/forth) to the CASE statement
  27.     |> >    word set. I.E .... CASE OF ENDOF ENDCASE ....
  28.     |> 
  29.     |>     Baden's CASE is in FORTH Dimensions VIII/5.
  30.     |> 
  31.     |>     Eaker, who wrote and enduring CASE construct, checks into
  32.     |> this newsgroup once and a while. Charles?
  33.     |> 
  34.     |>         =jax=
  35.     |> -- 
  36.     |>  # jax@well.{UUCP,sf.ca.us}  # #  Member  # # Vice President,       #
  37.     |>  # du!isis!koscej!jax        # # X3J14 TC # #  Forth Interest Group #
  38.     |>  # JAX on GEnie              # # for ANS  # #   P.O. Box 8231       #
  39.     |>  # SYSOP RCFB (303) 278-0364 # #  Forth   # #    San Jose CA 95155  #
  40.  
  41.     1. FIG-Forth
  42.     Here is the source for FIG-Forth published with the original article
  43.     (Forth Dimensions, Vol. II, No. 3, pp. 37-40.). The ?PAIRS word was
  44.     FIG-Forth's way of implementing a small amount of syntax checking.
  45.  
  46.      : CASE     ?COMP CSP @ !CSP 4 ; IMMEDIATE
  47.      : OF       4 ?PAIRS COMPILE OVER COMPILE = COMPILE OBRANCH
  48.             HERE 0 ,   COMPILE DROP  5 ; IMMEDIATE
  49.      : ENDOF    5 ?PAIRS COMPILE BRANCH HERE 0 ,
  50.             SWAP 2 [COMPILE] ENDIF 4 ; IMMEDIATE
  51.      : ENDCASE  4 ?PAIRS COMPILE DROP
  52.             BEGIN SP@ CSP @ = 0=
  53.               WHILE 2 [COMPILE ENDIF REPEAT
  54.             CSP ! ; IMMEDIATE
  55.  
  56.     1a. Here is additional source for FIG-Forth published in Forth
  57.     Dimensions, Vol. III, No. 6, pp. 187-188 in an article by Alfred J.
  58.     Monroe.  He adds a primitive compiled by OF which reduces the amount
  59.     of code compiled by OF. Use the definitions of CASE, ENDOF, and
  60.     ENDCASE given above.
  61.  
  62.      : (OF)     OVER = IF DROP 1 ELSE 0 ENDIF ;
  63.      : OF       4 ?PAIRS COMPILE (OF) COMPILE 0BRANCH
  64.             HERE 0 , 5 ; IMMEDIATE
  65.  
  66.     Mr. Monroe also gave code for some interesting variants:
  67.  
  68.      : (<OF)    OVER > IF DROP 1 ELSE 0 ENDIF ;
  69.      : <OF      4 ?PAIRS COMPILE (<OF) COMPILE 0BRANCH
  70.             HERE 0 , 5 ; IMMEDIATE
  71.      : (>OF)    OVER > IF DROP 1 ELSE 0 ENDIF ;
  72.      : >OF      4 ?PAIRS COMPILE (>OF) COMPILE 0BRANCH
  73.             HERE 0 , 5 ; IMMEDIATE
  74.      : RANGE    >R OVER DUP R> 1+ < IF SWAP 1- > IF DROP 1 ELSE 0
  75.             ENDIF ELSE DROP DROP 0 ENDIF ;
  76.      : RNG-OF   4 ?PAIRS COMPILE RANGE COMPILE 0BRANCH
  77.             HERE 0 , 5 ; IMMEDIATE
  78.  
  79.     1b. It is quite common to define (OF) as a CODE word and have
  80.     it combine the functions of the run-time (OF) and the compile-time
  81.     0BRANCH in the previous definitions. This reduces the amount of
  82.     compiled code even more.
  83.      CODE (OF) ( 1. Remove the top element of the stack and call it A.
  84.              2. If A equals the new top element of the stack,
  85.                 remove the new top element of the stack,
  86.                 skip over the branch vector, and execute
  87.                 the code which follows it.
  88.             Else
  89.                 continue execution at the location indicated
  90.                 by the branch vector.
  91.             ) END-CODE
  92.      : OF      4 ?PAIRS COMPILE (OF) HERE 0 , 5 ; IMMEDIATE
  93.  
  94.     2. dpANS-3
  95.     dpANS-3 contains the following definitions (p. 133) to illustrate
  96.     control structure extension. Note that it would be quite easy to
  97.     optimize OF along the lines suggested above. Note also that there is no
  98.     syntax checking.  These words may appear anywhere and not necessarily
  99.     combined with each other. In fact, ENDOF may be dispensed with entirely
  100.     and replaced with ELSE. Compile-time monitoring of the syntax of
  101.     control structure words is a perennial Forth problem.
  102.  
  103.      0 CONSTANT CASE IMMEDIATE  ( init count of OFs )
  104.  
  105.      : OF  ( #of -- orig #of+1 / x -- )
  106.         1+    ( count OFs )
  107.         >R    ( move off the stack in case the control-flow )
  108.           ( stack is the data stack. )
  109.         POSTPONE OVER  POSTPONE = ( copy and test case value )
  110.         POSTPONE IF    ( add orig to control flow stack )
  111.         POSTPONE DROP  ( discards case value if = )
  112.         R> ;           ( we can bring count back now )
  113.      IMMEDIATE
  114.  
  115.      : ENDOF  ( orig1 #of -- orig2 #of )
  116.         >R    ( move off the stack in case the control-flow )
  117.           ( stack is the data stack. )
  118.         POSTPONE ELSE
  119.         R> ;  ( we can bring count back now )
  120.      IMMEDIATE
  121.  
  122.      : ENDCASE ( orig 1..orign #of -- )
  123.         POSTPONE DROP  ( discard case value )
  124.         0 ?DO
  125.           POSTPONE THEN
  126.         LOOP ;
  127.      IMMEDIATE
  128.  
  129.     -- 
  130.     Chuck Eaker / P.O. Box 8, K-1 3C12 / Schenectady, NY 12301 USA
  131.     eaker@crd.ge.com        eaker@crdgw1.UUCP       (518) 387-5964
  132.