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

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

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