[gforth] / gforth / kernel / toolsext.fs  

gforth: gforth/kernel/toolsext.fs


1 : anton 1.16 \ Copyright (C) 1995,1998,2000 Free Software Foundation, Inc.
2 : anton 1.1
3 :     \ This file is part of Gforth.
4 :    
5 :     \ Gforth is free software; you can redistribute it and/or
6 :     \ modify it under the terms of the GNU General Public License
7 :     \ as published by the Free Software Foundation; either version 2
8 :     \ of the License, or (at your option) any later version.
9 :    
10 :     \ This program is distributed in the hope that it will be useful,
11 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
12 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 :     \ GNU General Public License for more details.
14 :    
15 :     \ You should have received a copy of the GNU General Public License
16 :     \ along with this program; if not, write to the Free Software
17 : anton 1.12 \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
18 : anton 1.1
19 : jwilke 1.3 Warnings off
20 : anton 1.1
21 :     Variable countif
22 :    
23 :     : dummy ; immediate
24 :     : >exec >r ; restrict ( :-)
25 : anton 1.10 : scanIF f83find dup 0= IF drop ['] dummy >head-noprim THEN ;
26 : anton 1.1
27 : jwilke 1.2 Create [struct]-search ' scanIF A, ' (reveal) A, ' drop A, ' drop A,
28 : pazsan 1.5 Create [struct]-voc [struct]-search A,
29 :     NIL A, NIL A, NIL A,
30 : anton 1.1
31 :     : ?if countif @ 0<
32 :     IF [ [struct]-voc 3 cells + ] ALiteral @ lookup ! THEN ;
33 :    
34 :     UNLOCK Tlast @ TNIL Tlast ! LOCK
35 :     \ last @ 0 last !
36 :    
37 : crook 1.8 : [IF]
38 :     1 countif +! ?if ; immediate
39 :     : [THEN]
40 :     -1 countif +! ?if ; immediate
41 :     : [ELSE]
42 :     postpone [THEN] postpone [IF] ;
43 : anton 1.1 immediate
44 :     ' [IF] Alias [IFDEF] immediate
45 :     ' [IF] Alias [IFUNDEF] immediate
46 : crook 1.8 ' [THEN] Alias [ENDIF] immediate
47 : anton 1.1 ' [IF] Alias [BEGIN] immediate
48 :     ' [IF] Alias [WHILE] immediate
49 :     ' [THEN] Alias [UNTIL] immediate
50 :     ' [THEN] Alias [AGAIN] immediate
51 :     ' [IF] Alias [DO] immediate
52 :     ' [IF] Alias [?DO] immediate
53 :     ' [THEN] Alias [LOOP] immediate
54 :     ' [THEN] Alias [+LOOP] immediate
55 :     : [REPEAT] postpone [AGAIN] postpone [THEN] ;
56 :     immediate
57 : anton 1.17 \ The following was too smart for its own good; consider "postpone (".
58 :     \ Moreover, ANS Forth specifies that the next [THEN] ends an [IF]
59 :     \ (even if its in a '( ... )').
60 :    
61 :     \ ' ( Alias ( immediate ( keep fontify happy)
62 :     \ ' \ Alias \ immediate
63 : anton 1.1
64 :     UNLOCK Tlast @ swap Tlast ! LOCK
65 :     \ last @ swap last !
66 : pazsan 1.5 1 cells - [struct]-voc cell+ !
67 : anton 1.1
68 :     \ Interpretative Structuren 30apr92py
69 :    
70 : pazsan 1.13 : [defined] ( "<spaces>name" -- flag ) bl word find nip 0<> ; immediate
71 :     \G returns true if name is found in current search order
72 : anton 1.14 ' [defined] alias defined immediate
73 : pazsan 1.13 : [undefined] ( "<spaces>name" -- flag ) postpone [defined] 0= ; immediate
74 :     \G returns false if name is found in current search order
75 : crook 1.8
76 : pazsan 1.13 : [IF] ( flag -- ) \ tools-ext bracket-if
77 : crook 1.8 \G If flag is @code{TRUE} do nothing (and therefore
78 :     \G execute subsequent words as normal). If flag is @code{FALSE},
79 :     \G parse and discard words from the parse
80 :     \G area (refilling it if necessary using
81 :     \G @code{REFILL}) including nested instances of @code{[IF]}..
82 :     \G @code{[ELSE]}.. @code{[THEN]} and @code{[IF]}.. @code{[THEN]}
83 :     \G until the balancing @code{[ELSE]} or @code{[THEN]} has been
84 :     \G parsed and discarded. Immediate word.
85 :     0= IF countif off
86 : anton 1.1 lookup @ [ [struct]-voc 3 cells + ] ALiteral !
87 :     [struct]-voc lookup !
88 :     THEN ; immediate
89 : crook 1.8
90 :     : [IFDEF] ( "<spaces>name" -- ) \ gforth bracket-if-def
91 :     \G If name is found in the current search-order, behave like
92 : crook 1.9 \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
93 : crook 1.8 \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
94 : pazsan 1.13 postpone [defined] postpone [IF] ; immediate
95 : crook 1.8
96 :     : [IFUNDEF] ( "<spaces>name" -- ) \ gforth bracket-if-un-def
97 :     \G If name is not found in the current search-order, behave like
98 : crook 1.9 \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
99 : crook 1.8 \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
100 : pazsan 1.13 postpone [defined] 0= postpone [IF] ; immediate
101 : crook 1.8
102 : pazsan 1.13 : [ELSE] ( -- ) \ tools-ext bracket-else
103 : crook 1.8 \G Parse and discard words from the parse
104 :     \G area (refilling it if necessary using
105 :     \G @code{REFILL}) including nested instances of @code{[IF]}..
106 :     \G @code{[ELSE]}.. @code{[THEN]} and @code{[IF]}.. @code{[THEN]}
107 :     \G until the balancing @code{[THEN]} has been parsed and discarded.
108 :     \G @code{[ELSE]} only gets executed if the balancing @code{[IF]}
109 :     \G was @code{TRUE}; if it was @code{FALSE}, @code{[IF]} would
110 :     \G have parsed and discarded the @code{[ELSE]}, leaving the
111 :     \G subsequent words to be executed as normal.
112 :     \G Immediate word.
113 :     0 postpone [IF] ; immediate
114 :    
115 :     : [THEN] ( -- ) \ tools-ext bracket-then
116 :     \G Do nothing; used as a marker for other words to parse
117 : crook 1.9 \G and discard up to. Immediate word.
118 : crook 1.8 ; immediate
119 :    
120 :     : [ENDIF] ( -- ) \ gforth bracket-end-if
121 : crook 1.9 \G Do nothing; synonym for @code{[THEN]}
122 : crook 1.8 ; immediate
123 : anton 1.1
124 :     \ Structs for interpreter 28nov92py
125 :    
126 :     User (i)
127 :    
128 : crook 1.8 : [DO] ( n-limit n-index -- ) \ gforth bracket-do
129 :     >in @ -rot
130 : anton 1.1 DO I (i) ! dup >r >in ! interpret r> swap +LOOP drop ;
131 :     immediate
132 : crook 1.8
133 :     : [?DO] ( n-limit n-index -- ) \ gforth bracket-question-do
134 :     2dup = IF 2drop postpone [ELSE] ELSE postpone [DO] THEN ;
135 : anton 1.1 immediate
136 : crook 1.8
137 :     : [+LOOP] ( n -- ) \ gforth bracket-question-plus-loop
138 :     rdrop rdrop ; immediate
139 :    
140 :     : [LOOP] ( -- ) \ gforth bracket-loop
141 :     1 rdrop rdrop ; immediate
142 :    
143 :     : [FOR] ( n -- ) \ gforth bracket-for
144 :     0 swap postpone [DO] ; immediate
145 :    
146 :     : [NEXT] ( n -- ) \ gforth bracket-next
147 :     -1 rdrop rdrop ; immediate
148 :    
149 : pazsan 1.4 :noname (i) @ ;
150 :     :noname (i) @ postpone Literal ;
151 : crook 1.8 interpret/compile: [I] ( -- n ) \ gforth bracket-i
152 :    
153 :     : [BEGIN] ( -- ) \ gforth bracket-begin
154 :     >in @ >r BEGIN r@ >in ! interpret UNTIL rdrop ; immediate
155 :    
156 :     ' [+LOOP] Alias [UNTIL] ( flag -- ) \ gforth bracket-until
157 : anton 1.1 immediate
158 : crook 1.8
159 :     : [REPEAT] ( -- ) \ gforth bracket-repeat
160 :     false rdrop rdrop ; immediate
161 :    
162 :     ' [REPEAT] Alias [AGAIN] ( -- ) \ gforth bracket-again
163 :     immediate
164 :    
165 :     : [WHILE] ( flag -- ) \ gforth bracket-while
166 : anton 1.1 0= IF postpone [ELSE] true rdrop rdrop 1 countif +! THEN ;
167 :     immediate
168 :    
169 : jwilke 1.3 Warnings on

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help