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