Annotation of gforth/kernel/toolsext.fs, revision 1.17
1.16 anton 1: \ Copyright (C) 1995,1998,2000 Free Software Foundation, Inc.
1.1 anton 2:
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
1.12 anton 17: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1 anton 18:
1.3 jwilke 19: Warnings off
1.1 anton 20:
21: Variable countif
22:
23: : dummy ; immediate
24: : >exec >r ; restrict ( :-)
1.10 anton 25: : scanIF f83find dup 0= IF drop ['] dummy >head-noprim THEN ;
1.1 anton 26:
1.2 jwilke 27: Create [struct]-search ' scanIF A, ' (reveal) A, ' drop A, ' drop A,
1.5 pazsan 28: Create [struct]-voc [struct]-search A,
29: NIL A, NIL A, NIL A,
1.1 anton 30:
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:
1.8 crook 37: : [IF]
38: 1 countif +! ?if ; immediate
39: : [THEN]
40: -1 countif +! ?if ; immediate
41: : [ELSE]
42: postpone [THEN] postpone [IF] ;
1.1 anton 43: immediate
44: ' [IF] Alias [IFDEF] immediate
45: ' [IF] Alias [IFUNDEF] immediate
1.8 crook 46: ' [THEN] Alias [ENDIF] immediate
1.1 anton 47: ' [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
1.17 ! anton 57: \ 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
1.1 anton 63:
64: UNLOCK Tlast @ swap Tlast ! LOCK
65: \ last @ swap last !
1.5 pazsan 66: 1 cells - [struct]-voc cell+ !
1.1 anton 67:
68: \ Interpretative Structuren 30apr92py
69:
1.13 pazsan 70: : [defined] ( "<spaces>name" -- flag ) bl word find nip 0<> ; immediate
71: \G returns true if name is found in current search order
1.14 anton 72: ' [defined] alias defined immediate
1.13 pazsan 73: : [undefined] ( "<spaces>name" -- flag ) postpone [defined] 0= ; immediate
74: \G returns false if name is found in current search order
1.8 crook 75:
1.13 pazsan 76: : [IF] ( flag -- ) \ tools-ext bracket-if
1.8 crook 77: \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
1.1 anton 86: lookup @ [ [struct]-voc 3 cells + ] ALiteral !
87: [struct]-voc lookup !
88: THEN ; immediate
1.8 crook 89:
90: : [IFDEF] ( "<spaces>name" -- ) \ gforth bracket-if-def
91: \G If name is found in the current search-order, behave like
1.9 crook 92: \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
1.8 crook 93: \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
1.13 pazsan 94: postpone [defined] postpone [IF] ; immediate
1.8 crook 95:
96: : [IFUNDEF] ( "<spaces>name" -- ) \ gforth bracket-if-un-def
97: \G If name is not found in the current search-order, behave like
1.9 crook 98: \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
1.8 crook 99: \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
1.13 pazsan 100: postpone [defined] 0= postpone [IF] ; immediate
1.8 crook 101:
1.13 pazsan 102: : [ELSE] ( -- ) \ tools-ext bracket-else
1.8 crook 103: \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
1.9 crook 117: \G and discard up to. Immediate word.
1.8 crook 118: ; immediate
119:
120: : [ENDIF] ( -- ) \ gforth bracket-end-if
1.9 crook 121: \G Do nothing; synonym for @code{[THEN]}
1.8 crook 122: ; immediate
1.1 anton 123:
124: \ Structs for interpreter 28nov92py
125:
126: User (i)
127:
1.8 crook 128: : [DO] ( n-limit n-index -- ) \ gforth bracket-do
129: >in @ -rot
1.1 anton 130: DO I (i) ! dup >r >in ! interpret r> swap +LOOP drop ;
131: immediate
1.8 crook 132:
133: : [?DO] ( n-limit n-index -- ) \ gforth bracket-question-do
134: 2dup = IF 2drop postpone [ELSE] ELSE postpone [DO] THEN ;
1.1 anton 135: immediate
1.8 crook 136:
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:
1.4 pazsan 149: :noname (i) @ ;
150: :noname (i) @ postpone Literal ;
1.8 crook 151: 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
1.1 anton 157: immediate
1.8 crook 158:
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
1.1 anton 166: 0= IF postpone [ELSE] true rdrop rdrop 1 countif +! THEN ;
167: immediate
168:
1.3 jwilke 169: Warnings on
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>