Annotation of gforth/kernel/comp.fs, revision 1.82

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>