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