Imported Upstream version 21
[debian/pforth] / case.fth
1 \ @(#) case.fth 98/01/26 1.2
2 \ CASE Statement
3 \
4 \ This definition is based upon Wil Baden's assertion that
5 \ >MARK >RESOLVE ?BRANCH etc. are not needed if one has IF ELSE THEN etc.
6 \
7 \ Author: Phil Burk
8 \ Copyright 1994 3DO, Phil Burk, Larry Polansky, Devid Rosenboom
9 \
10 \ The pForth software code is dedicated to the public domain,
11 \ and any third party may reproduce, distribute and modify
12 \ the pForth software code or any derivative works thereof
13 \ without any compensation or license.  The pForth software
14 \ code is provided on an "as is" basis without any warranty
15 \ of any kind, including, without limitation, the implied
16 \ warranties of merchantability and fitness for a particular
17 \ purpose and their equivalents under the laws of any jurisdiction.
18 \
19 \ MOD: PLB 6/24/91 Check for missing ENDOF
20 \ MOD: PLB 8/7/91 Add ?OF and RANGEOF
21
22 anew TASK-CASE
23
24 variable CASE-DEPTH
25 variable OF-DEPTH
26
27 : CASE  ( n -- , start case statement ) ( -c- case-depth )
28         ?comp case-depth @ case-depth off  ( allow nesting )
29         0 of-depth !
30 ; IMMEDIATE
31
32 : ?OF  ( n flag -- | n , doit if true ) ( -c- addr )
33         [compile] IF
34         compile drop
35         1 case-depth +!
36         1 of-depth +!
37 ; IMMEDIATE
38
39 : OF  ( n t -- | n , doit if match ) ( -c- addr )
40         ?comp
41         compile over compile =
42         [compile] ?OF
43 ; IMMEDIATE
44
45 : (RANGEOF?)  ( n lo hi -- | n  flag )
46         >r over ( n lo n ) <=
47         IF
48                 dup r> ( n n hi ) <=
49         ELSE
50                 rdrop false
51         THEN
52 ;
53
54 : RANGEOF  ( n lo hi -- | n , doit if within ) ( -c- addr )
55         compile (rangeof?)
56         [compile] ?OF
57 ; IMMEDIATE
58
59 : ENDOF  ( -- ) ( addr -c- addr' )
60         [compile] ELSE
61         -1 of-depth +!
62 ; IMMEDIATE
63
64 : ENDCASE ( n -- )  ( old-case-depth addr' addr' ??? -- )
65         of-depth @
66         IF >newline ." Missing ENDOF in CASE!" cr abort
67         THEN
68 \
69         compile drop
70         case-depth @ 0
71         ?DO [compile] THEN
72         LOOP
73         case-depth !
74 ; IMMEDIATE
75