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

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

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