home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / internet / cld9z89.zip / Z0000105.TXT < prev    next >
Text File  |  1993-06-09  |  5KB  |  135 lines

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