[gforth] / gforth / kernel / toolsext.fs  

gforth: gforth/kernel/toolsext.fs


1 : anton 1.11 \ 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 : pazsan 1.13 ' ( Alias ( immediate ( keep fontify happy)
58 : anton 1.1 ' \ Alias \ immediate
59 :    
60 :     UNLOCK Tlast @ swap Tlast ! LOCK
61 :     \ last @ swap last !
62 : pazsan 1.5 1 cells - [struct]-voc cell+ !
63 : anton 1.1
64 :     \ Interpretative Structuren 30apr92py
65 :    
66 : pazsan 1.13 : [defined] ( "<spaces>name" -- flag ) bl word find nip 0<> ; immediate
67 :     \G returns true if name is found in current search order
68 : anton 1.14 ' [defined] alias defined immediate
69 : pazsan 1.13 : [undefined] ( "<spaces>name" -- flag ) postpone [defined] 0= ; immediate
70 :     \G returns false if name is found in current search order
71 : crook 1.8
72 : pazsan 1.13 : [IF] ( flag -- ) \ tools-ext bracket-if
73 : crook 1.8 \G If flag is @code{TRUE} do nothing (and therefore
74 :     \G execute subsequent words as normal). If flag is @code{FALSE},
75 :     \G parse and discard words from the parse
76 :     \G area (refilling it if necessary using
77 :     \G @code{REFILL}) including nested instances of @code{[IF]}..
78 :     \G @code{[ELSE]}.. @code{[THEN]} and @code{[IF]}.. @code{[THEN]}
79 :     \G until the balancing @code{[ELSE]} or @code{[THEN]} has been
80 :     \G parsed and discarded. Immediate word.
81 :     0= IF countif off
82 : anton 1.1 lookup @ [ [struct]-voc 3 cells + ] ALiteral !
83 :     [struct]-voc lookup !
84 :     THEN ; immediate
85 : crook 1.8
86 :     : [IFDEF] ( "<spaces>name" -- ) \ gforth bracket-if-def
87 :     \G If name is found in the current search-order, behave like
88 : crook 1.9 \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
89 : crook 1.8 \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
90 : pazsan 1.13 postpone [defined] postpone [IF] ; immediate
91 : crook 1.8
92 :     : [IFUNDEF] ( "<spaces>name" -- ) \ gforth bracket-if-un-def
93 :     \G If name is not found in the current search-order, behave like
94 : crook 1.9 \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
95 : crook 1.8 \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
96 : pazsan 1.13 postpone [defined] 0= postpone [IF] ; immediate
97 : crook 1.8
98 : pazsan 1.13 : [ELSE] ( -- ) \ tools-ext bracket-else
99 : crook 1.8 \G Parse and discard words from the parse
100 :     \G area (refilling it if necessary using
101 :     \G @code{REFILL}) including nested instances of @code{[IF]}..
102 :     \G @code{[ELSE]}.. @code{[THEN]} and @code{[IF]}.. @code{[THEN]}
103 :     \G until the balancing @code{[THEN]} has been parsed and discarded.
104 :     \G @code{[ELSE]} only gets executed if the balancing @code{[IF]}
105 :     \G was @code{TRUE}; if it was @code{FALSE}, @code{[IF]} would
106 :     \G have parsed and discarded the @code{[ELSE]}, leaving the
107 :     \G subsequent words to be executed as normal.
108 :     \G Immediate word.
109 :     0 postpone [IF] ; immediate
110 :    
111 :     : [THEN] ( -- ) \ tools-ext bracket-then
112 :     \G Do nothing; used as a marker for other words to parse
113 : crook 1.9 \G and discard up to. Immediate word.
114 : crook 1.8 ; immediate
115 :    
116 :     : [ENDIF] ( -- ) \ gforth bracket-end-if
117 : crook 1.9 \G Do nothing; synonym for @code{[THEN]}
118 : crook 1.8 ; immediate
119 : anton 1.1
120 :     \ Structs for interpreter 28nov92py
121 :    
122 :     User (i)
123 :    
124 : crook 1.8 : [DO] ( n-limit n-index -- ) \ gforth bracket-do
125 :     >in @ -rot
126 : anton 1.1 DO I (i) ! dup >r >in ! interpret r> swap +LOOP drop ;
127 :     immediate
128 : crook 1.8
129 :     : [?DO] ( n-limit n-index -- ) \ gforth bracket-question-do
130 :     2dup = IF 2drop postpone [ELSE] ELSE postpone [DO] THEN ;
131 : anton 1.1 immediate
132 : crook 1.8
133 :     : [+LOOP] ( n -- ) \ gforth bracket-question-plus-loop
134 :     rdrop rdrop ; immediate
135 :    
136 :     : [LOOP] ( -- ) \ gforth bracket-loop
137 :     1 rdrop rdrop ; immediate
138 :    
139 :     : [FOR] ( n -- ) \ gforth bracket-for
140 :     0 swap postpone [DO] ; immediate
141 :    
142 :     : [NEXT] ( n -- ) \ gforth bracket-next
143 :     -1 rdrop rdrop ; immediate
144 :    
145 : pazsan 1.4 :noname (i) @ ;
146 :     :noname (i) @ postpone Literal ;
147 : crook 1.8 interpret/compile: [I] ( -- n ) \ gforth bracket-i
148 :    
149 :     : [BEGIN] ( -- ) \ gforth bracket-begin
150 :     >in @ >r BEGIN r@ >in ! interpret UNTIL rdrop ; immediate
151 :    
152 :     ' [+LOOP] Alias [UNTIL] ( flag -- ) \ gforth bracket-until
153 : anton 1.1 immediate
154 : crook 1.8
155 :     : [REPEAT] ( -- ) \ gforth bracket-repeat
156 :     false rdrop rdrop ; immediate
157 :    
158 :     ' [REPEAT] Alias [AGAIN] ( -- ) \ gforth bracket-again
159 :     immediate
160 :    
161 :     : [WHILE] ( flag -- ) \ gforth bracket-while
162 : anton 1.1 0= IF postpone [ELSE] true rdrop rdrop 1 countif +! THEN ;
163 :     immediate
164 :    
165 : jwilke 1.3 Warnings on

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help