Annotation of gforth/kernel/comp.fs, revision 1.100
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.64 anton 311: : (does>2) ( addr -- )
312: cfaligned /does-handler + !does ;
313:
1.1 pazsan 314: : dodoes, ( -- )
315: cfalign here /does-handler allot does-handler! ;
316:
1.61 pazsan 317: : (compile) ( -- ) \ gforth-obsolete: dummy
1.63 anton 318: true abort" (compile) doesn't work, use POSTPONE instead" ;
1.1 pazsan 319:
320: \ \ ticks
321:
1.90 anton 322: : name>comp ( nt -- w xt ) \ gforth name-to-comp
1.15 crook 323: \G @i{w xt} is the compilation token for the word @i{nt}.
1.1 pazsan 324: (name>comp)
325: 1 = if
326: ['] execute
327: else
328: ['] compile,
329: then ;
330:
331: : [(')] ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
332: (') postpone ALiteral ; immediate restrict
333:
334: : ['] ( compilation. "name" -- ; run-time. -- xt ) \ core bracket-tick
1.15 crook 335: \g @i{xt} represents @i{name}'s interpretation
336: \g semantics. Perform @code{-14 throw} if the word has no
1.1 pazsan 337: \g interpretation semantics.
338: ' postpone ALiteral ; immediate restrict
339:
1.5 anton 340: : COMP' ( "name" -- w xt ) \ gforth comp-tick
1.15 crook 341: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1 pazsan 342: (') name>comp ;
343:
344: : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
1.15 crook 345: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1 pazsan 346: COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
347:
1.18 jwilke 348: : postpone, ( w xt -- ) \ gforth postpone-comma
1.26 anton 349: \g Compile the compilation semantics represented by the
350: \g compilation token @i{w xt}.
1.18 jwilke 351: dup ['] execute =
352: if
353: drop compile,
354: else
1.61 pazsan 355: swap POSTPONE aliteral compile,
1.18 jwilke 356: then ;
357:
358: : POSTPONE ( "name" -- ) \ core
359: \g Compiles the compilation semantics of @i{name}.
1.48 anton 360: COMP' postpone, ; immediate
1.18 jwilke 361:
1.1 pazsan 362: \ \ recurse 17may93jaw
363:
364: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
1.10 crook 365: \g Call the current definition.
1.56 anton 366: latestxt compile, ; immediate restrict
1.1 pazsan 367:
368: \ \ compiler loop
369:
1.72 anton 370: : compiler1 ( c-addr u -- ... xt )
1.99 pazsan 371: 2dup find-name [ [IFDEF] prelude-mask ] run-prelude [ [THEN] ] dup
1.1 pazsan 372: if ( c-addr u nt )
1.72 anton 373: nip nip name>comp
1.1 pazsan 374: else
375: drop
1.72 anton 376: 2dup 2>r snumber? dup
1.1 pazsan 377: IF
378: 0>
379: IF
1.72 anton 380: ['] 2literal
381: ELSE
382: ['] literal
1.1 pazsan 383: THEN
1.72 anton 384: 2rdrop
1.1 pazsan 385: ELSE
1.72 anton 386: drop 2r> compiler-notfound1
1.1 pazsan 387: THEN
388: then ;
389:
1.22 crook 390: : [ ( -- ) \ core left-bracket
1.8 crook 391: \G Enter interpretation state. Immediate word.
1.72 anton 392: ['] interpreter1 IS parser1 state off ; immediate
1.1 pazsan 393:
394: : ] ( -- ) \ core right-bracket
1.8 crook 395: \G Enter compilation state.
1.72 anton 396: ['] compiler1 IS parser1 state on ;
1.1 pazsan 397:
398: \ \ Strings 22feb93py
399:
1.44 anton 400: : S, ( addr u -- )
1.45 anton 401: \ allot string as counted string
1.84 pazsan 402: [ has? flash [IF] ]
403: dup c, bounds ?DO I c@ c, LOOP
404: [ [ELSE] ]
405: here over char+ allot place align
406: [ [THEN] ] ;
1.45 anton 407:
408: : mem, ( addr u -- )
409: \ allot the memory block HERE (do alignment yourself)
1.84 pazsan 410: [ has? flash [IF] ]
411: bounds ?DO I c@ c, LOOP
412: [ [ELSE] ]
413: here over allot swap move
414: [ [THEN] ] ;
1.1 pazsan 415:
1.44 anton 416: : ," ( "string"<"> -- )
417: [char] " parse s, ;
1.1 pazsan 418:
419: \ \ Header states 23feb93py
420:
1.83 pazsan 421: \ problematic only for big endian machines
422:
423: has? f83headerstring [IF]
424: : cset ( bmask c-addr -- )
425: tuck c@ or swap c! ;
426:
427: : creset ( bmask c-addr -- )
428: tuck c@ swap invert and swap c! ;
429:
430: : ctoggle ( bmask c-addr -- )
431: tuck c@ xor swap c! ;
432: [ELSE]
1.1 pazsan 433: : cset ( bmask c-addr -- )
1.30 anton 434: tuck @ or swap ! ;
1.1 pazsan 435:
436: : creset ( bmask c-addr -- )
1.30 anton 437: tuck @ swap invert and swap ! ;
1.1 pazsan 438:
439: : ctoggle ( bmask c-addr -- )
1.30 anton 440: tuck @ xor swap ! ;
1.83 pazsan 441: [THEN]
1.1 pazsan 442:
443: : lastflags ( -- c-addr )
444: \ the address of the flags byte in the last header
445: \ aborts if the last defined word was headerless
1.56 anton 446: latest dup 0= abort" last word was headerless" cell+ ;
1.1 pazsan 447:
448: : immediate ( -- ) \ core
1.10 crook 449: \G Make the compilation semantics of a word be to @code{execute}
450: \G the execution semantics.
1.83 pazsan 451: immediate-mask lastflags [ has? rom [IF] ] creset [ [ELSE] ] cset [ [THEN] ] ;
1.1 pazsan 452:
453: : restrict ( -- ) \ gforth
1.10 crook 454: \G A synonym for @code{compile-only}
1.83 pazsan 455: restrict-mask lastflags [ has? rom [IF] ] creset [ [ELSE] ] cset [ [THEN] ] ;
1.18 jwilke 456:
1.1 pazsan 457: ' restrict alias compile-only ( -- ) \ gforth
1.10 crook 458: \G Remove the interpretation semantics of a word.
1.1 pazsan 459:
460: \ \ Create Variable User Constant 17mar93py
461:
1.15 crook 462: : Alias ( xt "name" -- ) \ gforth
1.1 pazsan 463: Header reveal
464: alias-mask lastflags creset
465: dup A, lastcfa ! ;
466:
467: doer? :dovar [IF]
468:
469: : Create ( "name" -- ) \ core
470: Header reveal dovar: cfa, ;
471: [ELSE]
472:
473: : Create ( "name" -- ) \ core
474: Header reveal here lastcfa ! 0 A, 0 , DOES> ;
475: [THEN]
476:
1.85 pazsan 477: has? flash [IF]
1.87 pazsan 478: : (variable) dpp @ normal-dp = IF Create dpp @
1.85 pazsan 479: ELSE normal-dp @ Constant dpp @ ram THEN ;
480: : Variable ( "name" -- ) \ core
481: (Variable) 0 , dpp ! ;
482:
483: : AVariable ( "name" -- ) \ gforth
484: (Variable) 0 A, dpp ! ;
485:
486: : 2Variable ( "name" -- ) \ double two-variable
487: (Variable) 0 , 0 , dpp ! ;
488: [ELSE]
1.1 pazsan 489: : Variable ( "name" -- ) \ core
490: Create 0 , ;
491:
492: : AVariable ( "name" -- ) \ gforth
493: Create 0 A, ;
494:
1.22 crook 495: : 2Variable ( "name" -- ) \ double two-variable
1.85 pazsan 496: Create 0 , 0 , ;
497: [THEN]
1.1 pazsan 498:
1.75 pazsan 499: has? no-userspace 0= [IF]
1.21 crook 500: : uallot ( n -- ) \ gforth
501: udp @ swap udp +! ;
1.1 pazsan 502:
503: doer? :douser [IF]
504:
505: : User ( "name" -- ) \ gforth
506: Header reveal douser: cfa, cell uallot , ;
507:
508: : AUser ( "name" -- ) \ gforth
509: User ;
510: [ELSE]
511:
512: : User Create cell uallot , DOES> @ up @ + ;
513:
514: : AUser User ;
1.75 pazsan 515: [THEN]
1.1 pazsan 516: [THEN]
517:
518: doer? :docon [IF]
519: : (Constant) Header reveal docon: cfa, ;
520: [ELSE]
521: : (Constant) Create DOES> @ ;
522: [THEN]
523:
1.76 pazsan 524: doer? :dovalue [IF]
525: : (Value) Header reveal dovalue: cfa, ;
526: [ELSE]
527: has? rom [IF]
528: : (Value) Create DOES> @ @ ;
529: [ELSE]
530: : (Value) Create DOES> @ ;
531: [THEN]
532: [THEN]
533:
1.1 pazsan 534: : Constant ( w "name" -- ) \ core
1.15 crook 535: \G Define a constant @i{name} with value @i{w}.
1.1 pazsan 536: \G
1.15 crook 537: \G @i{name} execution: @i{-- w}
1.1 pazsan 538: (Constant) , ;
539:
540: : AConstant ( addr "name" -- ) \ gforth
541: (Constant) A, ;
542:
1.84 pazsan 543: has? flash [IF]
544: : Value ( w "name" -- ) \ core-ext
545: (Value) dpp @ >r here cell allot >r
546: ram here >r , r> r> flash! r> dpp ! ;
547:
548: ' Value alias AValue
549: [ELSE]
1.1 pazsan 550: : Value ( w "name" -- ) \ core-ext
1.76 pazsan 551: (Value) , ;
1.1 pazsan 552:
1.37 jwilke 553: : AValue ( w "name" -- ) \ core-ext
1.76 pazsan 554: (Value) A, ;
1.84 pazsan 555: [THEN]
1.37 jwilke 556:
1.22 crook 557: : 2Constant ( w1 w2 "name" -- ) \ double two-constant
1.1 pazsan 558: Create ( w1 w2 "name" -- )
559: 2,
560: DOES> ( -- w1 w2 )
561: 2@ ;
562:
563: doer? :dofield [IF]
564: : (Field) Header reveal dofield: cfa, ;
565: [ELSE]
566: : (Field) Create DOES> @ + ;
567: [THEN]
1.39 pazsan 568:
569: \ \ interpret/compile:
570:
571: struct
572: >body
573: cell% field interpret/compile-int
574: cell% field interpret/compile-comp
575: end-struct interpret/compile-struct
576:
577: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
578: Create immediate swap A, A,
579: DOES>
580: abort" executed primary cfa of an interpret/compile: word" ;
581: \ state @ IF cell+ THEN perform ;
582:
1.1 pazsan 583: \ IS Defer What's Defers TO 24feb93py
584:
1.68 anton 585: defer defer-default ( -- )
1.69 anton 586: ' abort is defer-default
587: \ default action for deferred words (overridden by a warning later)
1.67 anton 588:
1.1 pazsan 589: doer? :dodefer [IF]
590:
591: : Defer ( "name" -- ) \ gforth
1.67 anton 592: \G Define a deferred word @i{name}; its execution semantics can be
593: \G set with @code{defer!} or @code{is} (and they have to, before first
594: \G executing @i{name}.
1.1 pazsan 595: Header Reveal dodefer: cfa,
1.86 pazsan 596: [ has? rom [IF] ] here >r cell allot
597: dpp @ ram here r> flash! ['] defer-default A, dpp !
598: [ [ELSE] ] ['] defer-default A, [ [THEN] ] ;
1.18 jwilke 599:
1.1 pazsan 600: [ELSE]
601:
1.77 pazsan 602: has? rom [IF]
603: : Defer ( "name" -- ) \ gforth
1.86 pazsan 604: Create here >r cell allot
605: dpp @ ram here r> flash! ['] defer-default A, dpp !
1.77 pazsan 606: DOES> @ @ execute ;
607: [ELSE]
608: : Defer ( "name" -- ) \ gforth
609: Create ['] defer-default A,
610: DOES> @ execute ;
611: [THEN]
1.1 pazsan 612: [THEN]
613:
1.67 anton 614: : defer@ ( xt-deferred -- xt ) \ gforth defer-fetch
615: \G @i{xt} represents the word currently associated with the deferred
616: \G word @i{xt-deferred}.
1.86 pazsan 617: >body @ [ has? rom [IF] ] @ [ [THEN] ] ;
1.67 anton 618:
1.25 anton 619: : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
620: \G Compiles the present contents of the deferred word @i{name}
621: \G into the current definition. I.e., this produces static
622: \G binding as if @i{name} was not deferred.
1.67 anton 623: ' defer@ compile, ; immediate
1.18 jwilke 624:
625: :noname
626: dodoes, here !does ]
627: defstart :-hook ;
628: :noname
629: ;-hook ?struc
630: [ has? xconds [IF] ] exit-like [ [THEN] ]
1.84 pazsan 631: here [ has? peephole [IF] ] 5 [ [ELSE] ] 4 [ [THEN] ] cells +
632: postpone aliteral postpone (does>2) [compile] exit
1.80 pazsan 633: [ has? peephole [IF] ] finish-code [ [THEN] ] dodoes,
1.18 jwilke 634: defstart :-hook ;
635: interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
636:
1.73 anton 637: : defer! ( xt xt-deferred -- ) \ gforth defer-store
1.67 anton 638: \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
1.86 pazsan 639: >body [ has? rom [IF] ] @ [ [THEN] ] ! ;
1.67 anton 640:
1.20 anton 641: : <IS> ( "name" xt -- ) \ gforth
642: \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
1.67 anton 643: ' defer! ;
1.20 anton 644:
645: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
646: \g At run-time, changes the @code{defer}red word @var{name} to
647: \g execute @var{xt}.
1.67 anton 648: ' postpone ALiteral postpone defer! ; immediate restrict
1.18 jwilke 649:
1.20 anton 650: ' <IS>
1.18 jwilke 651: ' [IS]
1.67 anton 652: interpret/compile: IS ( compilation/interpretation "name-deferred" -- ; run-time xt -- ) \ gforth
653: \G Changes the @code{defer}red word @var{name} to execute @var{xt}.
654: \G Its compilation semantics parses at compile time.
1.18 jwilke 655:
1.20 anton 656: ' <IS>
657: ' [IS]
658: interpret/compile: TO ( w "name" -- ) \ core-ext
1.18 jwilke 659:
660: : interpret/compile? ( xt -- flag )
661: >does-code ['] DOES> >does-code = ;
1.1 pazsan 662:
663: \ \ : ; 24feb93py
664:
665: defer :-hook ( sys1 -- sys2 )
666:
667: defer ;-hook ( sys2 -- sys1 )
668:
1.16 jwilke 669: 0 Constant defstart
670:
1.14 jwilke 671: [IFDEF] docol,
672: : (:noname) ( -- colon-sys )
673: \ common factor of : and :noname
1.34 anton 674: docol, ]comp
1.14 jwilke 675: [ELSE]
1.7 anton 676: : (:noname) ( -- colon-sys )
677: \ common factor of : and :noname
1.34 anton 678: docol: cfa,
1.14 jwilke 679: [THEN]
1.40 anton 680: defstart ] :-hook ;
1.7 anton 681:
1.1 pazsan 682: : : ( "name" -- colon-sys ) \ core colon
1.7 anton 683: Header (:noname) ;
684:
685: : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
686: 0 last !
687: cfalign here (:noname) ;
1.1 pazsan 688:
1.14 jwilke 689: [IFDEF] fini,
690: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
691: ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
692: [ELSE]
1.1 pazsan 693: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
1.57 pazsan 694: ;-hook ?struc [compile] exit
695: [ has? peephole [IF] ] finish-code [ [THEN] ]
696: reveal postpone [ ; immediate restrict
1.14 jwilke 697: [THEN]
1.1 pazsan 698:
699: \ \ Search list handling: reveal words, recursive 23feb93py
700:
701: : last? ( -- false / nfa nfa )
1.56 anton 702: latest ?dup ;
1.1 pazsan 703:
1.83 pazsan 704: Variable warnings ( -- addr ) \ gforth
705: G -1 warnings T !
706:
707: has? ec [IF]
708: : reveal ( -- ) \ gforth
709: last?
710: if \ the last word has a header
711: dup ( name>link ) @ -1 =
712: if \ it is still hidden
1.88 pazsan 713: forth-wordlist dup >r @ over
1.84 pazsan 714: [ has? flash [IF] ] flash! [ [ELSE] ] ! [ [THEN] ] r> !
1.83 pazsan 715: else
716: drop
717: then
718: then ;
719: [ELSE]
1.1 pazsan 720: : (reveal) ( nt wid -- )
1.3 pazsan 721: wordlist-id dup >r
1.1 pazsan 722: @ over ( name>link ) !
723: r> ! ;
724:
725: \ make entry in wordlist-map
726: ' (reveal) f83search reveal-method !
727:
728: : check-shadow ( addr count wid -- )
1.2 pazsan 729: \G prints a warning if the string is already present in the wordlist
730: >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
731: >stderr
732: ." redefined " name>string 2dup type
1.43 anton 733: str= 0= if
1.2 pazsan 734: ." with " type
735: else
736: 2drop
737: then
738: space space EXIT
739: then
740: 2drop 2drop ;
1.1 pazsan 741:
742: : reveal ( -- ) \ gforth
743: last?
744: if \ the last word has a header
745: dup ( name>link ) @ 1 and
746: if \ it is still hidden
747: dup ( name>link ) @ 1 xor ( nt wid )
748: 2dup >r name>string r> check-shadow ( nt wid )
1.83 pazsan 749: dup wordlist-map @ reveal-method perform
1.1 pazsan 750: else
751: drop
752: then
753: then ;
754:
755: : rehash ( wid -- )
756: dup wordlist-map @ rehash-method perform ;
1.80 pazsan 757: [THEN]
1.1 pazsan 758:
759: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
1.10 crook 760: \g Make the current definition visible, enabling it to call itself
1.1 pazsan 761: \g recursively.
762: immediate restrict
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>