[gforth] / gforth / kernel / comp.fs  

gforth: gforth/kernel/comp.fs


1 : pazsan 1.1 \ compiler definitions 14sep97jaw
2 :    
3 : anton 1.74 \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005 Free Software Foundation, Inc.
4 : anton 1.6
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 :     \ as published by the Free Software Foundation; either version 2
10 :     \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 :     \ along with this program; if not, write to the Free Software
19 : anton 1.29 \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20 : anton 1.6
21 : pazsan 1.1 \ \ Revisions-Log
22 :    
23 :     \ put in seperate file 14sep97jaw
24 :    
25 :     \ \ here allot , c, A, 17dec92py
26 :    
27 : jwilke 1.14 [IFUNDEF] allot
28 :     [IFUNDEF] forthstart
29 :     : allot ( n -- ) \ core
30 :     dup unused u> -8 and throw
31 :     dp +! ;
32 :     [THEN]
33 :     [THEN]
34 :    
35 :     \ we default to this version if we have nothing else 05May99jaw
36 :     [IFUNDEF] allot
37 : pazsan 1.1 : allot ( n -- ) \ core
38 : anton 1.23 \G Reserve @i{n} address units of data space without
39 :     \G initialization. @i{n} is a signed number, passing a negative
40 :     \G @i{n} releases memory. In ANS Forth you can only deallocate
41 :     \G memory from the current contiguous region in this way. In
42 :     \G Gforth you can deallocate anything in this way but named words.
43 :     \G The system does not check this restriction.
44 : anton 1.12 here +
45 :     dup 1- usable-dictionary-end forthstart within -8 and throw
46 :     dp ! ;
47 : jwilke 1.14 [THEN]
48 : pazsan 1.1
49 : crook 1.22 : c, ( c -- ) \ core c-comma
50 : crook 1.15 \G Reserve data space for one char and store @i{c} in the space.
51 : pazsan 1.1 here 1 chars allot c! ;
52 :    
53 : crook 1.22 : , ( w -- ) \ core comma
54 : crook 1.15 \G Reserve data space for one cell and store @i{w} in the space.
55 : pazsan 1.1 here cell allot ! ;
56 :    
57 :     : 2, ( w1 w2 -- ) \ gforth
58 : crook 1.15 \G Reserve data space for two cells and store the double @i{w1
59 : anton 1.24 \G w2} there, @i{w2} first (lower address).
60 : pazsan 1.1 here 2 cells allot 2! ;
61 :    
62 :     \ : aligned ( addr -- addr' ) \ core
63 :     \ [ cell 1- ] Literal + [ -1 cells ] Literal and ;
64 :    
65 :     : align ( -- ) \ core
66 : crook 1.15 \G If the data-space pointer is not aligned, reserve enough space to align it.
67 : pazsan 1.1 here dup aligned swap ?DO bl c, LOOP ;
68 :    
69 : crook 1.22 \ : faligned ( addr -- f-addr ) \ float f-aligned
70 : pazsan 1.1 \ [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
71 :    
72 : crook 1.22 : falign ( -- ) \ float f-align
73 : crook 1.15 \G If the data-space pointer is not float-aligned, reserve
74 :     \G enough space to align it.
75 : pazsan 1.1 here dup faligned swap
76 :     ?DO
77 :     bl c,
78 :     LOOP ;
79 :    
80 : anton 1.9 : maxalign ( -- ) \ gforth
81 : anton 1.23 \G Align data-space pointer for all alignment requirements.
82 : pazsan 1.1 here dup maxaligned swap
83 :     ?DO
84 :     bl c,
85 :     LOOP ;
86 :    
87 :     \ the code field is aligned if its body is maxaligned
88 :     ' maxalign Alias cfalign ( -- ) \ gforth
89 : anton 1.24 \G Align data-space pointer for code field requirements (i.e., such
90 :     \G that the corresponding body is maxaligned).
91 : pazsan 1.1
92 :     ' , alias A, ( addr -- ) \ gforth
93 :    
94 :     ' NOOP ALIAS const
95 :    
96 :     \ \ Header 23feb93py
97 :    
98 :     \ input-stream, nextname and noname are quite ugly (passing
99 :     \ information through global variables), but they are useful for dealing
100 :     \ with existing/independent defining words
101 :    
102 :     defer (header)
103 :     defer header ( -- ) \ gforth
104 :     ' (header) IS header
105 :    
106 :     : string, ( c-addr u -- ) \ gforth
107 :     \G puts down string as cstring
108 :     dup c, here swap chars dup allot move ;
109 :    
110 : anton 1.30 : longstring, ( c-addr u -- ) \ gforth
111 : anton 1.55 \G puts down string as longcstring
112 : anton 1.30 dup , here swap chars dup allot move ;
113 :    
114 : pazsan 1.1 : header, ( c-addr u -- ) \ gforth
115 :     name-too-long?
116 : anton 1.53 dup max-name-length @ max max-name-length !
117 : pazsan 1.1 align here last !
118 :     current @ 1 or A, \ link field; before revealing, it contains the
119 :     \ tagged reveal-into wordlist
120 : pazsan 1.78 [ has? f83headerstring [IF] ]
121 :     string,
122 :     [ [ELSE] ]
123 : pazsan 1.79 longstring,
124 : pazsan 1.78 [ [THEN] ]
125 : pazsan 1.79 cfalign alias-mask lastflags cset ;
126 : pazsan 1.1
127 :     : input-stream-header ( "name" -- )
128 :     name name-too-short? header, ;
129 :    
130 :     : input-stream ( -- ) \ general
131 :     \G switches back to getting the name from the input stream ;
132 :     ['] input-stream-header IS (header) ;
133 :    
134 :     ' input-stream-header IS (header)
135 :    
136 : anton 1.32 2variable nextname-string
137 : pazsan 1.1
138 : pazsan 1.33 has? OS [IF]
139 : pazsan 1.1 : nextname-header ( -- )
140 : anton 1.32 nextname-string 2@ header,
141 :     nextname-string free-mem-var
142 : pazsan 1.1 input-stream ;
143 : pazsan 1.33 [THEN]
144 : pazsan 1.1
145 :     \ the next name is given in the string
146 :    
147 : pazsan 1.33 has? OS [IF]
148 : pazsan 1.1 : nextname ( c-addr u -- ) \ gforth
149 : anton 1.19 \g The next defined word will have the name @var{c-addr u}; the
150 :     \g defining word will leave the input stream alone.
151 : pazsan 1.1 name-too-long?
152 : anton 1.32 nextname-string free-mem-var
153 :     save-mem nextname-string 2!
154 : pazsan 1.1 ['] nextname-header IS (header) ;
155 : pazsan 1.33 [THEN]
156 : pazsan 1.1
157 :     : noname-header ( -- )
158 :     0 last ! cfalign
159 :     input-stream ;
160 :    
161 :     : noname ( -- ) \ gforth
162 : anton 1.19 \g The next defined word will be anonymous. The defining word will
163 :     \g leave the input stream alone. The xt of the defined word will
164 : anton 1.56 \g be given by @code{latestxt}.
165 : pazsan 1.1 ['] noname-header IS (header) ;
166 :    
167 : anton 1.56 : latestxt ( -- xt ) \ gforth
168 : crook 1.15 \G @i{xt} is the execution token of the last word defined.
169 : crook 1.13 \ The main purpose of this word is to get the xt of words defined using noname
170 : pazsan 1.1 lastcfa @ ;
171 :    
172 : anton 1.56 ' latestxt alias lastxt \ gforth-obsolete
173 :     \G old name for @code{latestxt}.
174 :    
175 :     : latest ( -- nt ) \ gforth
176 :     \G @var{nt} is the name token of the last word defined; it is 0 if the
177 :     \G last word has no name.
178 :     last @ ;
179 :    
180 : pazsan 1.1 \ \ literals 17dec92py
181 :    
182 :     : Literal ( compilation n -- ; run-time -- n ) \ core
183 : anton 1.27 \G Compilation semantics: compile the run-time semantics.@*
184 :     \G Run-time Semantics: push @i{n}.@*
185 :     \G Interpretation semantics: undefined.
186 : jwilke 1.14 [ [IFDEF] lit, ]
187 :     lit,
188 :     [ [ELSE] ]
189 : anton 1.27 postpone lit ,
190 : jwilke 1.14 [ [THEN] ] ; immediate restrict
191 : pazsan 1.1
192 : anton 1.72 : 2Literal ( compilation w1 w2 -- ; run-time -- w1 w2 ) \ double two-literal
193 :     \G Compile appropriate code such that, at run-time, @i{w1 w2} are
194 :     \G placed on the stack. Interpretation semantics are undefined.
195 :     swap postpone Literal postpone Literal ; immediate restrict
196 :    
197 : pazsan 1.1 : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
198 : jwilke 1.14 [ [IFDEF] alit, ]
199 :     alit,
200 :     [ [ELSE] ]
201 :     postpone lit A,
202 :     [ [THEN] ] ; immediate restrict
203 : pazsan 1.1
204 : pazsan 1.70 Defer char@ ( addr u -- char addr' u' )
205 :     :noname over c@ -rot 1 /string ; IS char@
206 :    
207 : crook 1.8 : char ( '<spaces>ccc' -- c ) \ core
208 : crook 1.15 \G Skip leading spaces. Parse the string @i{ccc} and return @i{c}, the
209 :     \G display code representing the first character of @i{ccc}.
210 : pazsan 1.70 bl word count char@ 2drop ;
211 : pazsan 1.1
212 : crook 1.8 : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
213 : crook 1.10 \G Compilation: skip leading spaces. Parse the string
214 : crook 1.15 \G @i{ccc}. Run-time: return @i{c}, the display code
215 :     \G representing the first character of @i{ccc}. Interpretation
216 : crook 1.10 \G semantics for this word are undefined.
217 : pazsan 1.1 char postpone Literal ; immediate restrict
218 :    
219 :     \ \ threading 17mar93py
220 :    
221 :     : cfa, ( code-address -- ) \ gforth cfa-comma
222 :     here
223 :     dup lastcfa !
224 :     0 A, 0 , code-address! ;
225 :    
226 : jwilke 1.14 [IFUNDEF] compile,
227 : anton 1.34 defer compile, ( xt -- ) \ core-ext compile-comma
228 :     \G Compile the word represented by the execution token @i{xt}
229 :     \G into the current definition.
230 :    
231 :     ' , is compile,
232 : jwilke 1.14 [THEN]
233 : pazsan 1.1
234 : anton 1.40 defer basic-block-end ( -- )
235 :    
236 : anton 1.60 :noname ( -- )
237 :     0 compile-prim1 ;
238 :     is basic-block-end
239 : anton 1.40
240 : pazsan 1.36 has? peephole [IF]
241 : anton 1.40
242 :     \ dynamic only
243 : anton 1.49 : peephole-compile, ( xt -- )
244 :     \ compile xt, appending its code to the current dynamic superinstruction
245 :     here swap , compile-prim1 ;
246 : anton 1.38
247 : anton 1.35 : compile-to-prims, ( xt -- )
248 :     \G compile xt to use primitives (and their peephole optimization)
249 :     \G instead of ","-ing the xt.
250 :     \ !! all POSTPONEs here postpone primitives; this can be optimized
251 : pazsan 1.39 dup >does-code if
252 : anton 1.66 ['] does-exec peephole-compile, , EXIT
253 :     \ dup >body POSTPONE literal ['] call peephole-compile, >does-code , EXIT
254 : anton 1.35 then
255 :     dup >code-address CASE
256 : anton 1.66 docon: OF >body ['] lit@ peephole-compile, , EXIT ENDOF
257 :     \ docon: OF >body POSTPONE literal ['] @ peephole-compile, EXIT ENDOF
258 : anton 1.42 \ docon is also used by VALUEs, so don't @ at compile time
259 : anton 1.66 docol: OF >body ['] call peephole-compile, , EXIT ENDOF
260 :     dovar: OF >body ['] lit peephole-compile, , EXIT ENDOF
261 :     douser: OF >body @ ['] useraddr peephole-compile, , EXIT ENDOF
262 :     dodefer: OF >body ['] lit-perform peephole-compile, , EXIT ENDOF
263 :     dofield: OF >body @ ['] lit+ peephole-compile, , EXIT ENDOF
264 :     \ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF
265 : anton 1.48 \ code words and ;code-defined words (code words could be optimized):
266 : anton 1.66 dup in-dictionary? IF drop POSTPONE literal ['] execute peephole-compile, EXIT THEN
267 : anton 1.35 ENDCASE
268 : anton 1.49 peephole-compile, ;
269 : anton 1.35
270 :     ' compile-to-prims, IS compile,
271 : pazsan 1.36 [ELSE]
272 :     ' , is compile,
273 :     [THEN]
274 : anton 1.34
275 : pazsan 1.1 : !does ( addr -- ) \ gforth store-does
276 : anton 1.56 latestxt does-code! ;
277 : pazsan 1.1
278 : anton 1.65 \ !! unused, but ifdefed/gosted in some places
279 : pazsan 1.1 : (does>) ( R: addr -- )
280 : anton 1.62 r> cfaligned /does-handler + !does ; \ !! no gforth-native
281 : pazsan 1.1
282 : anton 1.64 : (does>2) ( addr -- )
283 :     cfaligned /does-handler + !does ;
284 :    
285 : pazsan 1.1 : dodoes, ( -- )
286 :     cfalign here /does-handler allot does-handler! ;
287 :    
288 : pazsan 1.61 : (compile) ( -- ) \ gforth-obsolete: dummy
289 : anton 1.63 true abort" (compile) doesn't work, use POSTPONE instead" ;
290 : pazsan 1.1
291 :     \ \ ticks
292 :    
293 :     : name>comp ( nt -- w xt ) \ gforth
294 : crook 1.15 \G @i{w xt} is the compilation token for the word @i{nt}.
295 : pazsan 1.1 (name>comp)
296 :     1 = if
297 :     ['] execute
298 :     else
299 :     ['] compile,
300 :     then ;
301 :    
302 :     : [(')] ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
303 :     (') postpone ALiteral ; immediate restrict
304 :    
305 :     : ['] ( compilation. "name" -- ; run-time. -- xt ) \ core bracket-tick
306 : crook 1.15 \g @i{xt} represents @i{name}'s interpretation
307 :     \g semantics. Perform @code{-14 throw} if the word has no
308 : pazsan 1.1 \g interpretation semantics.
309 :     ' postpone ALiteral ; immediate restrict
310 :    
311 : anton 1.5 : COMP' ( "name" -- w xt ) \ gforth comp-tick
312 : crook 1.15 \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
313 : pazsan 1.1 (') name>comp ;
314 :    
315 :     : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
316 : crook 1.15 \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
317 : pazsan 1.1 COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
318 :    
319 : jwilke 1.18 : postpone, ( w xt -- ) \ gforth postpone-comma
320 : anton 1.26 \g Compile the compilation semantics represented by the
321 :     \g compilation token @i{w xt}.
322 : jwilke 1.18 dup ['] execute =
323 :     if
324 :     drop compile,
325 :     else
326 : pazsan 1.61 swap POSTPONE aliteral compile,
327 : jwilke 1.18 then ;
328 :    
329 :     : POSTPONE ( "name" -- ) \ core
330 :     \g Compiles the compilation semantics of @i{name}.
331 : anton 1.48 COMP' postpone, ; immediate
332 : jwilke 1.18
333 : pazsan 1.1 \ \ recurse 17may93jaw
334 :    
335 :     : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
336 : crook 1.10 \g Call the current definition.
337 : anton 1.56 latestxt compile, ; immediate restrict
338 : pazsan 1.1
339 :     \ \ compiler loop
340 :    
341 : anton 1.72 : compiler1 ( c-addr u -- ... xt )
342 : pazsan 1.1 2dup find-name dup
343 :     if ( c-addr u nt )
344 : anton 1.72 nip nip name>comp
345 : pazsan 1.1 else
346 :     drop
347 : anton 1.72 2dup 2>r snumber? dup
348 : pazsan 1.1 IF
349 :     0>
350 :     IF
351 : anton 1.72 ['] 2literal
352 :     ELSE
353 :     ['] literal
354 : pazsan 1.1 THEN
355 : anton 1.72 2rdrop
356 : pazsan 1.1 ELSE
357 : anton 1.72 drop 2r> compiler-notfound1
358 : pazsan 1.1 THEN
359 :     then ;
360 :    
361 : crook 1.22 : [ ( -- ) \ core left-bracket
362 : crook 1.8 \G Enter interpretation state. Immediate word.
363 : anton 1.72 ['] interpreter1 IS parser1 state off ; immediate
364 : pazsan 1.1
365 :     : ] ( -- ) \ core right-bracket
366 : crook 1.8 \G Enter compilation state.
367 : anton 1.72 ['] compiler1 IS parser1 state on ;
368 : pazsan 1.1
369 :     \ \ Strings 22feb93py
370 :    
371 : anton 1.44 : S, ( addr u -- )
372 : anton 1.45 \ allot string as counted string
373 : anton 1.44 here over char+ allot place align ;
374 : anton 1.45
375 :     : mem, ( addr u -- )
376 :     \ allot the memory block HERE (do alignment yourself)
377 :     here over allot swap move ;
378 : pazsan 1.1
379 : anton 1.44 : ," ( "string"<"> -- )
380 :     [char] " parse s, ;
381 : pazsan 1.1
382 :     \ \ Header states 23feb93py
383 :    
384 :     : cset ( bmask c-addr -- )
385 : anton 1.30 tuck @ or swap ! ;
386 : pazsan 1.1
387 :     : creset ( bmask c-addr -- )
388 : anton 1.30 tuck @ swap invert and swap ! ;
389 : pazsan 1.1
390 :     : ctoggle ( bmask c-addr -- )
391 : anton 1.30 tuck @ xor swap ! ;
392 : pazsan 1.1
393 :     : lastflags ( -- c-addr )
394 :     \ the address of the flags byte in the last header
395 :     \ aborts if the last defined word was headerless
396 : anton 1.56 latest dup 0= abort" last word was headerless" cell+ ;
397 : pazsan 1.1
398 :     : immediate ( -- ) \ core
399 : crook 1.10 \G Make the compilation semantics of a word be to @code{execute}
400 :     \G the execution semantics.
401 : pazsan 1.1 immediate-mask lastflags cset ;
402 :    
403 :     : restrict ( -- ) \ gforth
404 : crook 1.10 \G A synonym for @code{compile-only}
405 : pazsan 1.1 restrict-mask lastflags cset ;
406 : jwilke 1.18
407 : pazsan 1.1 ' restrict alias compile-only ( -- ) \ gforth
408 : crook 1.10 \G Remove the interpretation semantics of a word.
409 : pazsan 1.1
410 :     \ \ Create Variable User Constant 17mar93py
411 :    
412 : crook 1.15 : Alias ( xt "name" -- ) \ gforth
413 : pazsan 1.1 Header reveal
414 :     alias-mask lastflags creset
415 :     dup A, lastcfa ! ;
416 :    
417 :     doer? :dovar [IF]
418 :    
419 :     : Create ( "name" -- ) \ core
420 :     Header reveal dovar: cfa, ;
421 :     [ELSE]
422 :    
423 :     : Create ( "name" -- ) \ core
424 :     Header reveal here lastcfa ! 0 A, 0 , DOES> ;
425 :     [THEN]
426 :    
427 :     : Variable ( "name" -- ) \ core
428 :     Create 0 , ;
429 :    
430 :     : AVariable ( "name" -- ) \ gforth
431 :     Create 0 A, ;
432 :    
433 : crook 1.22 : 2Variable ( "name" -- ) \ double two-variable
434 : pazsan 1.1 create 0 , 0 , ;
435 :    
436 : pazsan 1.75 has? no-userspace 0= [IF]
437 : crook 1.21 : uallot ( n -- ) \ gforth
438 :     udp @ swap udp +! ;
439 : pazsan 1.1
440 :     doer? :douser [IF]
441 :    
442 :     : User ( "name" -- ) \ gforth
443 :     Header reveal douser: cfa, cell uallot , ;
444 :    
445 :     : AUser ( "name" -- ) \ gforth
446 :     User ;
447 :     [ELSE]
448 :    
449 :     : User Create cell uallot , DOES> @ up @ + ;
450 :    
451 :     : AUser User ;
452 : pazsan 1.75 [THEN]
453 : pazsan 1.1 [THEN]
454 :    
455 :     doer? :docon [IF]
456 :     : (Constant) Header reveal docon: cfa, ;
457 :     [ELSE]
458 :     : (Constant) Create DOES> @ ;
459 :     [THEN]
460 :    
461 : pazsan 1.76 doer? :dovalue [IF]
462 :     : (Value) Header reveal dovalue: cfa, ;
463 :     [ELSE]
464 :     has? rom [IF]
465 :     : (Value) Create DOES> @ @ ;
466 :     [ELSE]
467 :     : (Value) Create DOES> @ ;
468 :     [THEN]
469 :     [THEN]
470 :    
471 : pazsan 1.1 : Constant ( w "name" -- ) \ core
472 : crook 1.15 \G Define a constant @i{name} with value @i{w}.
473 : pazsan 1.1 \G
474 : crook 1.15 \G @i{name} execution: @i{-- w}
475 : pazsan 1.1 (Constant) , ;
476 :    
477 :     : AConstant ( addr "name" -- ) \ gforth
478 :     (Constant) A, ;
479 :    
480 :     : Value ( w "name" -- ) \ core-ext
481 : pazsan 1.76 (Value) , ;
482 : pazsan 1.1
483 : jwilke 1.37 : AValue ( w "name" -- ) \ core-ext
484 : pazsan 1.76 (Value) A, ;
485 : jwilke 1.37
486 : crook 1.22 : 2Constant ( w1 w2 "name" -- ) \ double two-constant
487 : pazsan 1.1 Create ( w1 w2 "name" -- )
488 :     2,
489 :     DOES> ( -- w1 w2 )
490 :     2@ ;
491 :    
492 :     doer? :dofield [IF]
493 :     : (Field) Header reveal dofield: cfa, ;
494 :     [ELSE]
495 :     : (Field) Create DOES> @ + ;
496 :     [THEN]
497 : pazsan 1.39
498 :     \ \ interpret/compile:
499 :    
500 :     struct
501 :     >body
502 :     cell% field interpret/compile-int
503 :     cell% field interpret/compile-comp
504 :     end-struct interpret/compile-struct
505 :    
506 :     : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
507 :     Create immediate swap A, A,
508 :     DOES>
509 :     abort" executed primary cfa of an interpret/compile: word" ;
510 :     \ state @ IF cell+ THEN perform ;
511 :    
512 : pazsan 1.1 \ IS Defer What's Defers TO 24feb93py
513 :    
514 : anton 1.68 defer defer-default ( -- )
515 : anton 1.69 ' abort is defer-default
516 :     \ default action for deferred words (overridden by a warning later)
517 : anton 1.67
518 : pazsan 1.1 doer? :dodefer [IF]
519 :    
520 :     : Defer ( "name" -- ) \ gforth
521 : anton 1.67 \G Define a deferred word @i{name}; its execution semantics can be
522 :     \G set with @code{defer!} or @code{is} (and they have to, before first
523 :     \G executing @i{name}.
524 : pazsan 1.1 Header Reveal dodefer: cfa,
525 : anton 1.67 ['] defer-default A, ;
526 : jwilke 1.18
527 : pazsan 1.1 [ELSE]
528 :    
529 : pazsan 1.77 has? rom [IF]
530 :     : Defer ( "name" -- ) \ gforth
531 :     Create ['] defer-default A,
532 :     DOES> @ @ execute ;
533 :     [ELSE]
534 :     : Defer ( "name" -- ) \ gforth
535 :     Create ['] defer-default A,
536 :     DOES> @ execute ;
537 :     [THEN]
538 : pazsan 1.1 [THEN]
539 :    
540 : anton 1.67 : defer@ ( xt-deferred -- xt ) \ gforth defer-fetch
541 :     \G @i{xt} represents the word currently associated with the deferred
542 :     \G word @i{xt-deferred}.
543 :     >body @ ;
544 :    
545 : anton 1.25 : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
546 :     \G Compiles the present contents of the deferred word @i{name}
547 :     \G into the current definition. I.e., this produces static
548 :     \G binding as if @i{name} was not deferred.
549 : anton 1.67 ' defer@ compile, ; immediate
550 : jwilke 1.18
551 :     :noname
552 :     dodoes, here !does ]
553 :     defstart :-hook ;
554 :     :noname
555 :     ;-hook ?struc
556 :     [ has? xconds [IF] ] exit-like [ [THEN] ]
557 : anton 1.64 here 5 cells + postpone aliteral postpone (does>2) [compile] exit
558 :     finish-code dodoes,
559 : jwilke 1.18 defstart :-hook ;
560 :     interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
561 :    
562 : anton 1.73 : defer! ( xt xt-deferred -- ) \ gforth defer-store
563 : anton 1.67 \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
564 :     >body ! ;
565 :    
566 : anton 1.20 : <IS> ( "name" xt -- ) \ gforth
567 :     \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
568 : anton 1.67 ' defer! ;
569 : anton 1.20
570 :     : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
571 :     \g At run-time, changes the @code{defer}red word @var{name} to
572 :     \g execute @var{xt}.
573 : anton 1.67 ' postpone ALiteral postpone defer! ; immediate restrict
574 : jwilke 1.18
575 : anton 1.20 ' <IS>
576 : jwilke 1.18 ' [IS]
577 : anton 1.67 interpret/compile: IS ( compilation/interpretation "name-deferred" -- ; run-time xt -- ) \ gforth
578 :     \G Changes the @code{defer}red word @var{name} to execute @var{xt}.
579 :     \G Its compilation semantics parses at compile time.
580 : jwilke 1.18
581 : anton 1.20 ' <IS>
582 :     ' [IS]
583 :     interpret/compile: TO ( w "name" -- ) \ core-ext
584 : jwilke 1.18
585 :     : interpret/compile? ( xt -- flag )
586 :     >does-code ['] DOES> >does-code = ;
587 : pazsan 1.1
588 :     \ \ : ; 24feb93py
589 :    
590 :     defer :-hook ( sys1 -- sys2 )
591 :    
592 :     defer ;-hook ( sys2 -- sys1 )
593 :    
594 : jwilke 1.16 0 Constant defstart
595 :    
596 : jwilke 1.14 [IFDEF] docol,
597 :     : (:noname) ( -- colon-sys )
598 :     \ common factor of : and :noname
599 : anton 1.34 docol, ]comp
600 : jwilke 1.14 [ELSE]
601 : anton 1.7 : (:noname) ( -- colon-sys )
602 :     \ common factor of : and :noname
603 : anton 1.34 docol: cfa,
604 : jwilke 1.14 [THEN]
605 : anton 1.40 defstart ] :-hook ;
606 : anton 1.7
607 : pazsan 1.1 : : ( "name" -- colon-sys ) \ core colon
608 : anton 1.7 Header (:noname) ;
609 :    
610 :     : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
611 :     0 last !
612 :     cfalign here (:noname) ;
613 : pazsan 1.1
614 : jwilke 1.14 [IFDEF] fini,
615 :     : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
616 :     ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
617 :     [ELSE]
618 : pazsan 1.1 : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
619 : pazsan 1.57 ;-hook ?struc [compile] exit
620 :     [ has? peephole [IF] ] finish-code [ [THEN] ]
621 :     reveal postpone [ ; immediate restrict
622 : jwilke 1.14 [THEN]
623 : pazsan 1.1
624 :     \ \ Search list handling: reveal words, recursive 23feb93py
625 :    
626 :     : last? ( -- false / nfa nfa )
627 : anton 1.56 latest ?dup ;
628 : pazsan 1.1
629 :     : (reveal) ( nt wid -- )
630 : pazsan 1.3 wordlist-id dup >r
631 : pazsan 1.1 @ over ( name>link ) !
632 :     r> ! ;
633 :    
634 :     \ make entry in wordlist-map
635 :     ' (reveal) f83search reveal-method !
636 :    
637 :     Variable warnings ( -- addr ) \ gforth
638 :     G -1 warnings T !
639 :    
640 :     : check-shadow ( addr count wid -- )
641 : pazsan 1.2 \G prints a warning if the string is already present in the wordlist
642 :     >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
643 :     >stderr
644 :     ." redefined " name>string 2dup type
645 : anton 1.43 str= 0= if
646 : pazsan 1.2 ." with " type
647 :     else
648 :     2drop
649 :     then
650 :     space space EXIT
651 :     then
652 :     2drop 2drop ;
653 : pazsan 1.1
654 :     : reveal ( -- ) \ gforth
655 :     last?
656 :     if \ the last word has a header
657 :     dup ( name>link ) @ 1 and
658 :     if \ it is still hidden
659 :     dup ( name>link ) @ 1 xor ( nt wid )
660 :     2dup >r name>string r> check-shadow ( nt wid )
661 :     dup wordlist-map @ reveal-method perform
662 :     else
663 :     drop
664 :     then
665 :     then ;
666 :    
667 :     : rehash ( wid -- )
668 :     dup wordlist-map @ rehash-method perform ;
669 :    
670 :     ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
671 : crook 1.10 \g Make the current definition visible, enabling it to call itself
672 : pazsan 1.1 \g recursively.
673 :     immediate restrict

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help