home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / l6p150.seq < prev    next >
Encoding:
Text File  |  1990-04-21  |  3.1 KB  |  98 lines

  1. \ Lesson 6 Part 15  ( F-PC 3.5 Tutorial by Jack Brown )
  2.  
  3. \ Multi-way branching using IF .. ELSE .. THEN
  4.  
  5. : TIS  ( -- ) CR  ."  THIS  IS  DIGIT  NUMBER  "  ;
  6.  
  7. : TEST1  ( -- )
  8.     BEGIN  KEY  DUP 13 <> WHILE
  9.       ASCII 1  OVER = IF DROP  TIS  ." ONE "     ELSE
  10.       ASCII 2  OVER = IF DROP  TIS  ." TWO "     ELSE
  11.       ASCII 3  OVER = IF DROP  TIS  ." THREE "   ELSE
  12.       ASCII 4  OVER = IF DROP  TIS  ." FOUR "    ELSE
  13.       ASCII 5  OVER = IF DROP  TIS  ." FIVE "    ELSE
  14.       ASCII 6  OVER = IF DROP  TIS  ." SIX "     ELSE
  15.       ASCII 7  OVER = IF DROP  TIS  ." SEVEN "   ELSE
  16.       ASCII 8  OVER = IF DROP  TIS  ." EIGHT "   ELSE
  17.       ASCII 9  OVER = IF DROP  TIS  ." NINE "    ELSE
  18.       ASCII 0  OVER = IF DROP  TIS  ." ZERO "    ELSE
  19.      BEEP DROP  THEN  THEN  THEN  THEN  THEN
  20.                 THEN  THEN  THEN  THEN  THEN  REPEAT DROP ;
  21.  
  22. COMMENT:
  23. The Case Statement
  24.  
  25. CASE ... OF ... ENDOF ...  ENDCASE
  26.  
  27. CASE    causes an index value to be compared to a series of values.
  28.  
  29. OF      is equivalent to  OVER = IF DROP
  30. ENDOF   is equivalent to  ELSE
  31.         Any number of OF .. ENDOF  pairs may be used.
  32.  
  33. ENDCASE is equivalent of the appropriate number of THENs
  34.  
  35. When the index value equals one of the OF values, the sequence between
  36. that OF and the corresponding ENDOF is executed. Control then branches
  37. to the word following ENDCASE. If no match is found,  ENDCASE compiles
  38. the appropriate number of THENs. The "otherwise" case may be handled by
  39. a sequence placed between the last ENDOF  and ENDCASE.  If the index
  40. value is not consumed by the "otherwise" clause it must be DROPed before
  41. ENDCASE is coded.
  42.  
  43. Multi-way branching  using the CASE  Statement
  44. COMMENT;
  45.  
  46. : TEST2  ( --  -- )
  47.     BEGIN  KEY  DUP 13 <> WHILE
  48.     CASE
  49.       ASCII 1  OF         TIS  ." ONE "     ENDOF
  50.       ASCII 2  OF         TIS  ." TWO "     ENDOF
  51.       ASCII 3  OF         TIS  ." THREE "   ENDOF
  52.       ASCII 4  OF         TIS  ." FOUR "    ENDOF
  53.       ASCII 5  OF         TIS  ." FIVE "    ENDOF
  54.       ASCII 6  OF         TIS  ." SIX "     ENDOF
  55.       ASCII 7  OF         TIS  ." SEVEN "   ENDOF
  56.       ASCII 8  OF         TIS  ." EIGHT "   ENDOF
  57.       ASCII 9  OF         TIS  ." NINE "    ENDOF
  58.       ASCII 0  OF         TIS  ." ZERO "    ENDOF
  59.             BEEP   DROP
  60.     ENDCASE
  61.     REPEAT DROP ;
  62.  
  63. \ Sample code definitions for the curious.     12:51JWB02/21/86
  64.  
  65. CODE DOUBLE ( n -- n*2 )
  66.              POP   AX
  67.              ADD   AX, AX
  68.              PUSH  AX
  69.              NEXT  END-CODE
  70.  
  71. CODE SPLIT ( hilo -- lo hi )
  72.              POP   BX
  73.              SUB   AH, AH
  74.              MOV   AL, BL
  75.              PUSH  AX
  76.              MOV   AL, BH
  77.              PUSH  AX
  78.              NEXT  END-CODE
  79.  
  80. CODE JOIN ( lo hi -- hilo )
  81.             POP   AX
  82.             POP   BX
  83.             MOV   AH, AL
  84.             MOV   AL, BL
  85.             PUSH  AX
  86.             NEXT  END-CODE
  87.  
  88. CODE D2/  ( dn -- dn/2 )
  89.             POP   AX
  90.             POP   DX
  91.             SAR   AX, # 1
  92.             RCR   DX, # 1
  93.             PUSH  DX
  94.             PUSH  AX
  95.             NEXT  END-CODE
  96.  
  97. ( Please Move to Lesson 6 Part 16 )
  98.