Annotation of gforth/kernel/int.fs, revision 1.148

1.1       pazsan      1: \ definitions needed for interpreter only
                      2: 
1.123     anton       3: \ Copyright (C) 1995-2000,2004,2005 Free Software Foundation, Inc.
1.11      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.63      anton      19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.11      anton      20: 
1.1       pazsan     21: \ \ Revision-Log
                     22: 
                     23: \       put in seperate file                           14sep97jaw 
                     24: 
                     25: \ \ input stream primitives                            23feb93py
                     26: 
1.33      jwilke     27: require ./basics.fs    \ bounds decimal hex ...
                     28: require ./io.fs                \ type ...
                     29: require ./nio.fs       \ . <# ...
                     30: require ./errore.fs    \ .error ...
1.50      anton      31: require kernel/version.fs      \ version-string
1.148   ! pazsan     32: has? ec 0= [IF]
1.33      jwilke     33: require ./../chains.fs
1.148   ! pazsan     34: [THEN]
1.33      jwilke     35: 
1.64      pazsan     36: has? new-input 0= [IF]
1.43      crook      37: : tib ( -- c-addr ) \ core-ext t-i-b
1.40      crook      38:     \G @i{c-addr} is the address of the Terminal Input Buffer.
1.29      crook      39:     \G OBSOLESCENT: @code{source} superceeds the function of this word.
1.1       pazsan     40:     >tib @ ;
                     41: 
1.29      crook      42: Defer source ( -- c-addr u ) \ core
1.1       pazsan     43: \ used by dodefer:, must be defer
1.40      crook      44: \G @i{c-addr} is the address of the input buffer and @i{u} is the
1.29      crook      45: \G number of characters in it.
1.1       pazsan     46: 
1.29      crook      47: : (source) ( -- c-addr u )
1.1       pazsan     48:     tib #tib @ ;
                     49: ' (source) IS source
1.64      pazsan     50: [THEN]
1.1       pazsan     51: 
                     52: : (word) ( addr1 n1 char -- addr2 n2 )
                     53:   dup >r skip 2dup r> scan  nip - ;
                     54: 
                     55: \ (word) should fold white spaces
                     56: \ this is what (parse-white) does
                     57: 
                     58: \ word parse                                           23feb93py
                     59: 
1.130     anton      60: : sword  ( char -- addr len ) \ gforth-obsolete s-word
                     61: \G Parses like @code{word}, but the output is like @code{parse} output.
                     62: \G @xref{core-idef}.
                     63:     \ this word was called PARSE-WORD until 0.3.0, but Open Firmware and
                     64:     \ dpANS6 A.6.2.2008 have a word with that name that behaves
                     65:     \ differently (like NAME).
                     66:     source 2dup >r >r >in @ over min /string
                     67:     rot dup bl = IF
                     68:         drop (parse-white)
                     69:     ELSE
                     70:         (word)
                     71:     THEN
1.138     pazsan     72: [ has? new-input [IF] ]
1.132     anton      73:     2dup input-lexeme!
1.138     pazsan     74: [ [THEN] ]
1.130     anton      75:     2dup + r> - 1+ r> min >in ! ;
1.1       pazsan     76: 
1.29      crook      77: : word   ( char "<chars>ccc<char>-- c-addr ) \ core
1.40      crook      78:     \G Skip leading delimiters. Parse @i{ccc}, delimited by
                     79:     \G @i{char}, in the parse area. @i{c-addr} is the address of a
1.29      crook      80:     \G transient region containing the parsed string in
1.40      crook      81:     \G counted-string format. If the parse area was empty or
1.29      crook      82:     \G contained no characters other than delimiters, the resulting
                     83:     \G string has zero length. A program may replace characters within
                     84:     \G the counted string. OBSOLESCENT: the counted string has a
                     85:     \G trailing space that is not included in its length.
                     86:     sword here place  bl here count + c!  here ;
                     87: 
                     88: : parse    ( char "ccc<char>" -- c-addr u ) \ core-ext
1.80      anton      89: \G Parse @i{ccc}, delimited by @i{char}, in the parse
                     90: \G area. @i{c-addr u} specifies the parsed string within the
                     91: \G parse area. If the parse area was empty, @i{u} is 0.
1.132     anton      92:     >r  source  >in @ over min /string ( c-addr1 u1 )
1.130     anton      93:     over  swap r>  scan >r
1.132     anton      94:     over - dup r> IF 1+ THEN  >in +!
1.138     pazsan     95: [ has? new-input [IF] ]
                     96:     2dup input-lexeme!
                     97: [ [THEN] ] ;
1.1       pazsan     98: 
                     99: \ name                                                 13feb93py
                    100: 
                    101: [IFUNDEF] (name) \ name might be a primitive
                    102: 
1.40      crook     103: : (name) ( -- c-addr count ) \ gforth
1.1       pazsan    104:     source 2dup >r >r >in @ /string (parse-white)
1.138     pazsan    105: [ has? new-input [IF] ]
1.132     anton     106:     2dup input-lexeme!
1.138     pazsan    107: [ [THEN] ]
1.1       pazsan    108:     2dup + r> - 1+ r> min >in ! ;
                    109: \    name count ;
                    110: [THEN]
                    111: 
                    112: : name-too-short? ( c-addr u -- c-addr u )
                    113:     dup 0= -&16 and throw ;
                    114: 
                    115: : name-too-long? ( c-addr u -- c-addr u )
1.67      anton     116:     dup lcount-mask u> -&19 and throw ;
1.1       pazsan    117: 
                    118: \ \ Number parsing                                     23feb93py
                    119: 
                    120: \ number? number                                       23feb93py
                    121: 
                    122: hex
1.110     anton     123: const Create bases   0A , 10 ,   2 ,   0A ,
1.109     anton     124: \                    10   16     2     10
1.1       pazsan    125: 
1.18      anton     126: \ !! protect BASE saving wrapper against exceptions
1.1       pazsan    127: : getbase ( addr u -- addr' u' )
1.108     anton     128:     2dup s" 0x" string-prefix? >r
                    129:     2dup s" 0X" string-prefix? r> or
1.117     anton     130:     base @ &34 < and if
1.108     anton     131:        hex 2 /string
                    132:     endif
1.109     anton     133:     over c@ [char] # - dup 4 u<
1.1       pazsan    134:     IF
                    135:        cells bases + @ base ! 1 /string
                    136:     ELSE
                    137:        drop
                    138:     THEN ;
                    139: 
1.124     anton     140: : sign? ( addr u -- addr1 u1 flag )
1.33      jwilke    141:     over c@ [char] - =  dup >r
1.1       pazsan    142:     IF
                    143:        1 /string
                    144:     THEN
1.20      pazsan    145:     r> ;
                    146: 
1.109     anton     147: : s'>unumber? ( addr u -- ud flag )
                    148:     \ convert string "C" or "C'" to character code
                    149:     dup 0= if
                    150:        false exit
                    151:     endif
1.116     anton     152:     x@+/string 0 s" '" 2rot string-prefix? ;
1.109     anton     153: 
1.124     anton     154: : s>unumber? ( addr u -- ud flag ) \ gforth
1.125     anton     155:     \G converts string addr u into ud, flag indicates success
1.121     anton     156:     dpl on
1.109     anton     157:     over c@ '' = if
                    158:        1 /string s'>unumber? exit
                    159:     endif
1.121     anton     160:     base @ >r  getbase
1.20      pazsan    161:     0. 2swap
1.18      anton     162:     BEGIN ( d addr len )
1.1       pazsan    163:        dup >r >number dup
1.18      anton     164:     WHILE \ there are characters left
1.1       pazsan    165:        dup r> -
1.18      anton     166:     WHILE \ the last >number parsed something
                    167:        dup 1- dpl ! over c@ [char] . =
                    168:     WHILE \ the current char is '.'
1.1       pazsan    169:        1 /string
1.18      anton     170:     REPEAT  THEN \ there are unparseable characters left
1.21      pazsan    171:        2drop false
1.20      pazsan    172:     ELSE
                    173:        rdrop 2drop true
1.21      pazsan    174:     THEN
                    175:     r> base ! ;
1.20      pazsan    176: 
                    177: \ ouch, this is complicated; there must be a simpler way - anton
1.125     anton     178: : s>number? ( addr u -- d f ) \ gforth
                    179:     \G converts string addr u into d, flag indicates success
1.21      pazsan    180:     sign? >r
1.20      pazsan    181:     s>unumber?
                    182:     0= IF
1.21      pazsan    183:         rdrop false
1.18      anton     184:     ELSE \ no characters left, all ok
1.20      pazsan    185:        r>
1.1       pazsan    186:        IF
                    187:            dnegate
                    188:        THEN
1.18      anton     189:        true
1.21      pazsan    190:     THEN ;
1.1       pazsan    191: 
1.18      anton     192: : s>number ( addr len -- d )
                    193:     \ don't use this, there is no way to tell success
                    194:     s>number? drop ;
                    195: 
1.1       pazsan    196: : snumber? ( c-addr u -- 0 / n -1 / d 0> )
1.18      anton     197:     s>number? 0=
1.1       pazsan    198:     IF
                    199:        2drop false  EXIT
                    200:     THEN
1.18      anton     201:     dpl @ dup 0< IF
1.1       pazsan    202:        nip
1.18      anton     203:     ELSE
                    204:        1+
1.1       pazsan    205:     THEN ;
                    206: 
                    207: : number? ( string -- string 0 / n -1 / d 0> )
                    208:     dup >r count snumber? dup if
                    209:        rdrop
                    210:     else
                    211:        r> swap
                    212:     then ;
                    213: 
                    214: : number ( string -- d )
                    215:     number? ?dup 0= abort" ?"  0<
                    216:     IF
                    217:        s>d
                    218:     THEN ;
                    219: 
                    220: \ \ Comments ( \ \G
                    221: 
1.29      crook     222: : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ thisone- core,file   paren
1.17      crook     223:     \G ** this will not get annotated. The alias in glocals.fs will instead **
1.29      crook     224:     \G It does not work to use "wordset-" prefix since this file is glossed
                    225:     \G by cross.fs which doesn't have the same functionalty as makedoc.fs
1.1       pazsan    226:     [char] ) parse 2drop ; immediate
                    227: 
1.51      anton     228: : \ ( compilation 'ccc<newline>' -- ; run-time -- ) \ thisone- core-ext,block-ext backslash
1.29      crook     229:     \G ** this will not get annotated. The alias in glocals.fs will instead ** 
                    230:     \G It does not work to use "wordset-" prefix since this file is glossed
                    231:     \G by cross.fs which doesn't have the same functionalty as makedoc.fs
1.12      pazsan    232:     [ has? file [IF] ]
1.1       pazsan    233:     blk @
                    234:     IF
                    235:        >in @ c/l / 1+ c/l * >in !
                    236:        EXIT
                    237:     THEN
1.12      pazsan    238:     [ [THEN] ]
1.1       pazsan    239:     source >in ! drop ; immediate
                    240: 
1.51      anton     241: : \G ( compilation 'ccc<newline>' -- ; run-time -- ) \ gforth backslash-gee
1.19      crook     242:     \G Equivalent to @code{\} but used as a tag to annotate definition
                    243:     \G comments into documentation.
1.1       pazsan    244:     POSTPONE \ ; immediate
                    245: 
1.139     pazsan    246: has? ec [IF]
                    247:     AVariable forth-wordlist
1.140     pazsan    248:     AVariable current  forth-wordlist current !
                    249:     ' current alias context
1.139     pazsan    250:     | ' (f83find) alias (search-wordlist) ( addr len wid -- nt / false )
                    251:     : find-name ( c-addr u -- nt | 0 ) \ gforth
                    252:        \g Find the name @i{c-addr u} in the current search
                    253:        \g order. Return its @i{nt}, if found, otherwise 0.
                    254:        context @ (search-wordlist) ;
                    255: [ELSE]
1.1       pazsan    256: \ \ object oriented search list                         17mar93py
                    257: 
                    258: \ word list structure:
                    259: 
                    260: struct
                    261:   cell% field find-method   \ xt: ( c_addr u wid -- nt )
                    262:   cell% field reveal-method \ xt: ( nt wid -- ) \ used by dofield:, must be field
                    263:   cell% field rehash-method \ xt: ( wid -- )      \ re-initializes a "search-data" (hashtables)
                    264:   cell% field hash-method   \ xt: ( wid -- )    \ initializes ""
                    265: \   \ !! what else
                    266: end-struct wordlist-map-struct
                    267: 
                    268: struct
1.6       pazsan    269:   cell% field wordlist-map \ pointer to a wordlist-map-struct
1.13      anton     270:   cell% field wordlist-id \ linked list of words (for WORDS etc.)
1.1       pazsan    271:   cell% field wordlist-link \ link field to other wordlists
1.13      anton     272:   cell% field wordlist-extend \ wordlist extensions (eg bucket offset)
1.1       pazsan    273: end-struct wordlist-struct
                    274: 
1.103     pazsan    275: has? f83headerstring [IF]
                    276: : f83find      ( addr len wordlist -- nt / false )
                    277:     wordlist-id @ (f83find) ;
                    278: [ELSE]
1.1       pazsan    279: : f83find      ( addr len wordlist -- nt / false )
1.67      anton     280:     wordlist-id @ (listlfind) ;
1.103     pazsan    281: [THEN]
1.1       pazsan    282: 
                    283: : initvoc              ( wid -- )
                    284:   dup wordlist-map @ hash-method perform ;
                    285: 
                    286: \ Search list table: find reveal
                    287: Create f83search ( -- wordlist-map )
                    288:     ' f83find A,  ' drop A,  ' drop A, ' drop A,
                    289: 
1.6       pazsan    290: here G f83search T A, NIL A, NIL A, NIL A,
1.1       pazsan    291: AValue forth-wordlist \ variable, will be redefined by search.fs
                    292: 
                    293: AVariable lookup               forth-wordlist lookup !
                    294: \ !! last is user and lookup?! jaw
                    295: AVariable current ( -- addr ) \ gforth
1.43      crook     296: \G @code{Variable} -- holds the @i{wid} of the compilation word list.
1.1       pazsan    297: AVariable voclink      forth-wordlist wordlist-link voclink !
1.38      anton     298: \ lookup AValue context ( -- addr ) \ gforth
                    299: Defer context ( -- addr ) \ gforth
1.43      crook     300: \G @code{context} @code{@@} is the @i{wid} of the word list at the
                    301: \G top of the search order.
1.1       pazsan    302: 
1.38      anton     303: ' lookup is context
1.1       pazsan    304: forth-wordlist current !
                    305: 
1.139     pazsan    306: : (search-wordlist)  ( addr count wid -- nt | false )
                    307:     dup wordlist-map @ find-method perform ;
                    308: 
                    309: : search-wordlist ( c-addr count wid -- 0 | xt +-1 ) \ search
                    310:     \G Search the word list identified by @i{wid} for the definition
                    311:     \G named by the string at @i{c-addr count}.  If the definition is
                    312:     \G not found, return 0. If the definition is found return 1 (if
                    313:     \G the definition is immediate) or -1 (if the definition is not
                    314:     \G immediate) together with the @i{xt}.  In Gforth, the @i{xt}
                    315:     \G returned represents the interpretation semantics.  ANS Forth
                    316:     \G does not specify clearly what @i{xt} represents.
                    317:     (search-wordlist) dup if
                    318:        (name>intn)
                    319:     then ;
                    320: 
                    321: : find-name ( c-addr u -- nt | 0 ) \ gforth
                    322:     \g Find the name @i{c-addr u} in the current search
                    323:     \g order. Return its @i{nt}, if found, otherwise 0.
                    324:     lookup @ (search-wordlist) ;
                    325: [THEN]
                    326: 
1.1       pazsan    327: \ \ header, finding, ticks                              17dec92py
                    328: 
1.69      pazsan    329: \ The constants are defined as 32 bits, but then erased
                    330: \ and overwritten by the right ones
1.67      anton     331: 
1.103     pazsan    332: has? f83headerstring [IF]
                    333:     \ to save space, Gforth EC limits words to 31 characters
                    334:     $80 constant alias-mask
                    335:     $40 constant immediate-mask
                    336:     $20 constant restrict-mask
                    337:     $1f constant lcount-mask
                    338: [ELSE]    
1.67      anton     339: $80000000 constant alias-mask
1.69      pazsan    340: 1 bits/char 1 - lshift
                    341: -1 cells allot  bigendian [IF]   c, 0 1 cells 1- times
                    342:                           [ELSE] 0 1 cells 1- times c, [THEN]
1.67      anton     343: $40000000 constant immediate-mask
1.69      pazsan    344: 1 bits/char 2 - lshift
                    345: -1 cells allot  bigendian [IF]   c, 0 1 cells 1- times
                    346:                           [ELSE] 0 1 cells 1- times c, [THEN]
1.67      anton     347: $20000000 constant restrict-mask
1.69      pazsan    348: 1 bits/char 3 - lshift
                    349: -1 cells allot  bigendian [IF]   c, 0 1 cells 1- times
                    350:                           [ELSE] 0 1 cells 1- times c, [THEN]
1.67      anton     351: $1fffffff constant lcount-mask
1.69      pazsan    352: 1 bits/char 3 - lshift 1 -
1.71      pazsan    353: -1 cells allot  bigendian [IF]   c, -1 1 cells 1- times
                    354:                           [ELSE] -1 1 cells 1- times c, [THEN]
1.103     pazsan    355: [THEN]
1.1       pazsan    356: 
                    357: \ higher level parts of find
                    358: 
                    359: : flag-sign ( f -- 1|-1 )
                    360:     \ true becomes 1, false -1
                    361:     0= 2* 1+ ;
                    362: 
1.79      anton     363: : ticking-compile-only-error ( ... -- )
                    364:     -&2048 throw ;
1.1       pazsan    365: 
1.93      anton     366: : compile-only-error ( ... -- )
                    367:     -&14 throw ;
                    368: 
1.1       pazsan    369: : (cfa>int) ( cfa -- xt )
                    370: [ has? compiler [IF] ]
                    371:     dup interpret/compile?
                    372:     if
                    373:        interpret/compile-int @
                    374:     then 
                    375: [ [THEN] ] ;
                    376: 
1.67      anton     377: : (x>int) ( cfa w -- xt )
1.1       pazsan    378:     \ get interpretation semantics of name
1.141     pazsan    379:     restrict-mask and [ has? rom [IF] ] 0= [ [THEN] ]
1.1       pazsan    380:     if
1.93      anton     381:        drop ['] compile-only-error
1.1       pazsan    382:     else
                    383:        (cfa>int)
                    384:     then ;
                    385: 
1.103     pazsan    386: has? f83headerstring [IF]
                    387: : name>string ( nt -- addr count ) \ gforth     head-to-string
                    388:     \g @i{addr count} is the name of the word represented by @i{nt}.
                    389:     cell+ count lcount-mask and ;
                    390: 
                    391: : ((name>))  ( nfa -- cfa )
                    392:     name>string + cfaligned ;
                    393: 
                    394: : (name>x) ( nfa -- cfa w )
                    395:     \ cfa is an intermediate cfa and w is the flags cell of nfa
                    396:     dup ((name>))
                    397:     swap cell+ c@ dup alias-mask and 0=
                    398:     IF
                    399:         swap @ swap
                    400:     THEN ;
                    401: [ELSE]
1.1       pazsan    402: : name>string ( nt -- addr count ) \ gforth     head-to-string
1.40      crook     403:     \g @i{addr count} is the name of the word represented by @i{nt}.
1.67      anton     404:     cell+ dup cell+ swap @ lcount-mask and ;
1.1       pazsan    405: 
                    406: : ((name>))  ( nfa -- cfa )
                    407:     name>string + cfaligned ;
                    408: 
1.67      anton     409: : (name>x) ( nfa -- cfa w )
                    410:     \ cfa is an intermediate cfa and w is the flags cell of nfa
1.1       pazsan    411:     dup ((name>))
1.67      anton     412:     swap cell+ @ dup alias-mask and 0=
1.1       pazsan    413:     IF
                    414:         swap @ swap
                    415:     THEN ;
1.103     pazsan    416: [THEN]
1.1       pazsan    417: 
                    418: : name>int ( nt -- xt ) \ gforth
1.31      crook     419:     \G @i{xt} represents the interpretation semantics of the word
                    420:     \G @i{nt}. If @i{nt} has no interpretation semantics (i.e. is
                    421:     \G @code{compile-only}), @i{xt} is the execution token for
1.79      anton     422:     \G @code{ticking-compile-only-error}, which performs @code{-2048 throw}.
1.1       pazsan    423:     (name>x) (x>int) ;
                    424: 
                    425: : name?int ( nt -- xt ) \ gforth
1.79      anton     426:     \G Like @code{name>int}, but perform @code{-2048 throw} if @i{nt}
1.31      crook     427:     \G has no interpretation semantics.
1.141     pazsan    428:     (name>x) restrict-mask and [ has? rom [IF] ] 0= [ [THEN] ]
1.1       pazsan    429:     if
1.79      anton     430:        ticking-compile-only-error \ does not return
1.1       pazsan    431:     then
                    432:     (cfa>int) ;
                    433: 
                    434: : (name>comp) ( nt -- w +-1 ) \ gforth
1.31      crook     435:     \G @i{w xt} is the compilation token for the word @i{nt}.
1.1       pazsan    436:     (name>x) >r 
                    437: [ has? compiler [IF] ]
                    438:     dup interpret/compile?
                    439:     if
                    440:         interpret/compile-comp @
                    441:     then 
                    442: [ [THEN] ]
1.141     pazsan    443:     r> immediate-mask and [ has? rom [IF] ] 0= [ [THEN] ] flag-sign
1.1       pazsan    444:     ;
                    445: 
                    446: : (name>intn) ( nfa -- xt +-1 )
1.67      anton     447:     (name>x) tuck (x>int) ( w xt )
1.141     pazsan    448:     swap immediate-mask and [ has? rom [IF] ] 0= [ [THEN] ] flag-sign ;
1.1       pazsan    449: 
1.72      pazsan    450: const Create ???  0 , 3 , char ? c, char ? c, char ? c,
1.30      jwilke    451: \ ??? is used by dovar:, must be created/:dovar
                    452: 
                    453: [IFDEF] forthstart
                    454: \ if we have a forthstart we can define head? with it
                    455: \ otherwise leave out the head? check
                    456: 
1.14      anton     457: : head? ( addr -- f )
1.82      anton     458: \G heuristic check whether addr is a name token; may deliver false
                    459: \G positives; addr must be a valid address; returns 1 for
                    460: \G particularly unsafe positives
1.14      anton     461:     \ we follow the link fields and check for plausibility; two
                    462:     \ iterations should catch most false addresses: on the first
                    463:     \ iteration, we may get an xt, on the second a code address (or
                    464:     \ some code), which is typically not in the dictionary.
1.82      anton     465:     \ we added a third iteration for working with code and ;code words.
                    466:     3 0 do
1.41      anton     467:        dup dup aligned <> if \ protect @ against unaligned accesses
                    468:            drop false unloop exit
                    469:        then
1.14      anton     470:        dup @ dup
                    471:        if ( addr addr1 )
                    472:            dup rot forthstart within
                    473:            if \ addr1 is outside forthstart..addr, not a head
                    474:                drop false unloop exit
                    475:            then ( addr1 )
                    476:        else \ 0 in the link field, no further checks
1.81      anton     477:            2drop 1 unloop exit \ this is very unsure, so return 1
1.14      anton     478:        then
                    479:     loop
                    480:     \ in dubio pro:
                    481:     drop true ;
                    482: 
1.48      anton     483: : >head-noprim ( cfa -- nt ) \ gforth  to-head-noprim
1.97      anton     484:     \ also heuristic
                    485:     dup forthstart - max-name-length @ float+ cell+ min cell max cell ?do ( cfa )
1.70      pazsan    486:        dup i - dup @ [ alias-mask lcount-mask or ] literal
                    487:        [ 1 bits/char 3 - lshift 1 - 1 bits/char 1 - lshift or
1.71      pazsan    488:        -1 cells allot bigendian [IF]   c, -1 1 cells 1- times
                    489:        [ELSE] -1 1 cells 1- times c, [THEN] ]
1.70      pazsan    490:        and ( cfa len|alias )
1.97      anton     491:        swap + cell+ cfaligned over alias-mask + =
1.14      anton     492:        if ( cfa )
                    493:            dup i - cell - dup head?
                    494:            if
                    495:                nip unloop exit
                    496:            then
                    497:            drop
                    498:        then
                    499:        cell +loop
                    500:     drop ??? ( wouldn't 0 be better? ) ;
1.1       pazsan    501: 
1.30      jwilke    502: [ELSE]
                    503: 
1.48      anton     504: : >head-noprim ( cfa -- nt ) \ gforth  to-head-noprim
1.45      pazsan    505:     $25 cell do ( cfa )
1.70      pazsan    506:        dup i - dup @ [ alias-mask lcount-mask or ] literal
                    507:        [ 1 bits/char 3 - lshift 1 - 1 bits/char 1 - lshift or
1.71      pazsan    508:        -1 cells allot bigendian [IF]   c, -1 1 cells 1- times
                    509:        [ELSE] -1 1 cells 1- times c, [THEN] ]
1.70      pazsan    510:        and ( cfa len|alias )
1.67      anton     511:        swap + cell + cfaligned over alias-mask + =
1.30      jwilke    512:        if ( cfa ) i - cell - unloop exit
                    513:        then
                    514:        cell +loop
                    515:     drop ??? ( wouldn't 0 be better? ) ;
                    516: 
                    517: [THEN]
1.1       pazsan    518: 
1.83      anton     519: cell% 2* 0 0 field >body ( xt -- a_addr ) \ core
                    520: \G Get the address of the body of the word represented by @i{xt} (the
                    521: \G address of the word's data field).
                    522: drop drop
                    523: 
                    524: cell% -2 * 0 0 field body> ( xt -- a_addr )
1.84      anton     525:     drop drop
                    526: 
                    527: has? standardthreading has? compiler and [IF]
                    528: 
                    529: ' @ alias >code-address ( xt -- c_addr ) \ gforth
                    530: \G @i{c-addr} is the code address of the word @i{xt}.
                    531: 
                    532: : >does-code ( xt -- a_addr ) \ gforth
                    533: \G If @i{xt} is the execution token of a child of a @code{DOES>} word,
                    534: \G @i{a-addr} is the start of the Forth code after the @code{DOES>};
                    535: \G Otherwise @i{a-addr} is 0.
                    536:     dup @ dodoes: = if
                    537:        cell+ @
                    538:     else
                    539:        drop 0
                    540:     endif ;
                    541: 
1.142     pazsan    542: has? flash [IF] ' flash! [ELSE] ' ! [THEN]
                    543: alias code-address! ( c_addr xt -- ) \ gforth
1.85      anton     544: \G Create a code field with code address @i{c-addr} at @i{xt}.
                    545: 
                    546: : does-code! ( a_addr xt -- ) \ gforth
                    547: \G Create a code field at @i{xt} for a child of a @code{DOES>}-word;
                    548: \G @i{a-addr} is the start of the Forth code after @code{DOES>}.
1.142     pazsan    549:     [ has? flash [IF] ]
                    550:     dodoes: over flash! cell+ flash!
                    551:     [ [ELSE] ]
                    552:     dodoes: over ! cell+ !
                    553:     [ [THEN] ] ;
1.85      anton     554: 
1.86      anton     555: ' drop alias does-handler! ( a_addr -- ) \ gforth
                    556: \G Create a @code{DOES>}-handler at address @i{a-addr}. Normally,
                    557: \G @i{a-addr} points just behind a @code{DOES>}.
                    558: 
1.85      anton     559: 2 cells constant /does-handler ( -- n ) \ gforth
                    560: \G The size of a @code{DOES>}-handler (includes possible padding).
                    561: 
1.84      anton     562: [THEN] 
1.1       pazsan    563: 
                    564: : sfind ( c-addr u -- 0 / xt +-1  ) \ gforth-obsolete
                    565:     find-name dup
                    566:     if ( nt )
                    567:        state @
                    568:        if
                    569:            (name>comp)
                    570:        else
                    571:            (name>intn)
                    572:        then
                    573:    then ;
                    574: 
1.31      crook     575: : find ( c-addr -- xt +-1 | c-addr 0 ) \ core,search
1.53      anton     576:     \G Search all word lists in the current search order for the
                    577:     \G definition named by the counted string at @i{c-addr}.  If the
                    578:     \G definition is not found, return 0. If the definition is found
                    579:     \G return 1 (if the definition has non-default compilation
                    580:     \G semantics) or -1 (if the definition has default compilation
                    581:     \G semantics).  The @i{xt} returned in interpret state represents
                    582:     \G the interpretation semantics.  The @i{xt} returned in compile
                    583:     \G state represented either the compilation semantics (for
                    584:     \G non-default compilation semantics) or the run-time semantics
                    585:     \G that the compilation semantics would @code{compile,} (for
                    586:     \G default compilation semantics).  The ANS Forth standard does
                    587:     \G not specify clearly what the returned @i{xt} represents (and
                    588:     \G also talks about immediacy instead of non-default compilation
                    589:     \G semantics), so this word is questionable in portable programs.
                    590:     \G If non-portability is ok, @code{find-name} and friends are
                    591:     \G better (@pxref{Name token}).
1.1       pazsan    592:     dup count sfind dup
                    593:     if
                    594:        rot drop
                    595:     then ;
                    596: 
1.34      jwilke    597: \ ticks in interpreter
1.1       pazsan    598: 
                    599: : (') ( "name" -- nt ) \ gforth
1.139     pazsan    600:     parse-name name-too-short?
1.28      anton     601:     find-name dup 0=
1.1       pazsan    602:     IF
1.42      anton     603:        drop -&13 throw
1.1       pazsan    604:     THEN  ;
                    605: 
                    606: : '    ( "name" -- xt ) \ core tick
1.31      crook     607:     \g @i{xt} represents @i{name}'s interpretation
                    608:     \g semantics. Perform @code{-14 throw} if the word has no
1.1       pazsan    609:     \g interpretation semantics.
                    610:     (') name?int ;
1.34      jwilke    611: 
                    612: has? compiler 0= [IF]  \ interpreter only version of IS and TO
                    613: 
                    614: : IS ' >body ! ;
                    615: ' IS Alias TO
                    616: 
                    617: [THEN]
1.1       pazsan    618: 
                    619: \ \ the interpreter loop                                 mar92py
                    620: 
                    621: \ interpret                                            10mar92py
                    622: 
1.120     anton     623: Defer parser1 ( c-addr u -- ... xt)
                    624: \ "... xt" is the action to be performed by the text-interpretation of c-addr u
                    625: 
                    626: : parser ( c-addr u -- ... )
                    627: \ text-interpret the word/number c-addr u, possibly producing a number
                    628:     parser1 execute ;
                    629: 
1.139     pazsan    630: has? ec [IF]
                    631:     ' (name) Alias parse-name
1.140     pazsan    632:     : no.extensions  2drop -&13 throw ;
1.139     pazsan    633:     ' no.extensions Alias compiler-notfound1
                    634:     ' no.extensions Alias interpreter-notfound1
                    635: [ELSE]    
1.119     anton     636: Defer parse-name ( "name" -- c-addr u ) \ gforth
1.55      anton     637: \G Get the next word from the input buffer
1.119     anton     638: ' (name) IS parse-name
1.77      anton     639: 
1.119     anton     640: ' parse-name alias parse-word ( -- c-addr u ) \ gforth-obsolete
                    641: \G old name for @code{parse-name}
                    642:     
                    643: ' parse-name alias name ( -- c-addr u ) \ gforth-obsolete
                    644: \G old name for @code{parse-name}
                    645:     
1.120     anton     646: Defer compiler-notfound1 ( c-addr count -- ... xt )
                    647: Defer interpreter-notfound1 ( c-addr count -- ... xt )
1.1       pazsan    648: 
                    649: : no.extensions  ( addr u -- )
1.42      anton     650:     2drop -&13 throw ;
1.120     anton     651: ' no.extensions IS compiler-notfound1
                    652: ' no.extensions IS interpreter-notfound1
1.1       pazsan    653: 
1.106     anton     654: Defer before-word ( -- ) \ gforth
                    655: \ called before the text interpreter parses the next word
                    656: ' noop IS before-word
1.139     pazsan    657: [THEN]
1.106     anton     658: 
1.66      anton     659: : interpret1 ( ... -- ... )
1.33      jwilke    660: [ has? backtrace [IF] ]
1.24      anton     661:     rp@ backtrace-rp0 !
1.33      jwilke    662: [ [THEN] ]
1.1       pazsan    663:     BEGIN
1.139     pazsan    664:        ?stack [ has? EC 0= [IF] ] before-word [ [THEN] ] parse-name dup
1.1       pazsan    665:     WHILE
1.120     anton     666:        parser1 execute
1.1       pazsan    667:     REPEAT
1.66      anton     668:     2drop ;
                    669:     
                    670: : interpret ( ?? -- ?? ) \ gforth
                    671:     \ interpret/compile the (rest of the) input buffer
                    672: [ has? backtrace [IF] ]
                    673:     backtrace-rp0 @ >r 
                    674: [ [THEN] ]
                    675:     ['] interpret1 catch
1.65      anton     676: [ has? backtrace [IF] ]
                    677:     r> backtrace-rp0 !
1.66      anton     678:     [ [THEN] ]
                    679:     throw ;
1.1       pazsan    680: 
                    681: \ interpreter                                  30apr92py
                    682: 
                    683: \ not the most efficient implementations of interpreter and compiler
1.120     anton     684: : interpreter1 ( c-addr u -- ... xt ) 
1.1       pazsan    685:     2dup find-name dup
                    686:     if
1.120     anton     687:        nip nip name>int
1.1       pazsan    688:     else
                    689:        drop
                    690:        2dup 2>r snumber?
                    691:        IF
1.120     anton     692:            2rdrop ['] noop
1.1       pazsan    693:        ELSE
1.120     anton     694:            2r> interpreter-notfound1
1.1       pazsan    695:        THEN
                    696:     then ;
                    697: 
1.120     anton     698: ' interpreter1  IS  parser1
1.1       pazsan    699: 
                    700: \ \ Query Evaluate                                     07apr93py
                    701: 
                    702: has? file 0= [IF]
1.12      pazsan    703: : sourceline# ( -- n )  1 ;
1.61      pazsan    704: [ELSE]
1.64      pazsan    705: has? new-input 0= [IF]
1.58      pazsan    706: Variable #fill-bytes
                    707: \G number of bytes read via (read-line) by the last refill
1.61      pazsan    708: [THEN]
1.64      pazsan    709: [THEN]
1.58      pazsan    710: 
1.64      pazsan    711: has? new-input 0= [IF]
1.138     pazsan    712: : input-start-line ( -- )  >in off ;
1.1       pazsan    713: : refill ( -- flag ) \ core-ext,block-ext,file-ext
1.29      crook     714:     \G Attempt to fill the input buffer from the input source.  When
                    715:     \G the input source is the user input device, attempt to receive
                    716:     \G input into the terminal input device. If successful, make the
                    717:     \G result the input buffer, set @code{>IN} to 0 and return true;
                    718:     \G otherwise return false. When the input source is a block, add 1
                    719:     \G to the value of @code{BLK} to make the next block the input
                    720:     \G source and current input buffer, and set @code{>IN} to 0;
                    721:     \G return true if the new value of @code{BLK} is a valid block
                    722:     \G number, false otherwise. When the input source is a text file,
                    723:     \G attempt to read the next line from the file. If successful,
                    724:     \G make the result the current input buffer, set @code{>IN} to 0
                    725:     \G and return true; otherwise, return false.  A successful result
                    726:     \G includes receipt of a line containing 0 characters.
1.12      pazsan    727:     [ has? file [IF] ]
1.138     pazsan    728:        blk @  IF  1 blk +!  true  EXIT  THEN
1.12      pazsan    729:        [ [THEN] ]
                    730:     tib /line
                    731:     [ has? file [IF] ]
                    732:        loadfile @ ?dup
1.59      pazsan    733:        IF    (read-line) throw #fill-bytes !
1.12      pazsan    734:        ELSE
                    735:            [ [THEN] ]
                    736:        sourceline# 0< IF 2drop false EXIT THEN
1.145     pazsan    737:        accept eof @ 0=
1.12      pazsan    738:        [ has? file [IF] ]
                    739:        THEN
                    740:        1 loadline +!
                    741:        [ [THEN] ]
1.138     pazsan    742:     swap #tib !
                    743:     input-start-line ;
1.1       pazsan    744: 
                    745: : query   ( -- ) \ core-ext
1.29      crook     746:     \G Make the user input device the input source. Receive input into
                    747:     \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
                    748:     \G superceeded by @code{accept}.
1.12      pazsan    749:     [ has? file [IF] ]
                    750:        blk off loadfile off
                    751:        [ [THEN] ]
1.64      pazsan    752:     refill drop ;
                    753: [THEN]
1.1       pazsan    754: 
                    755: \ save-mem extend-mem
                    756: 
                    757: has? os [IF]
                    758: : save-mem     ( addr1 u -- addr2 u ) \ gforth
                    759:     \g copy a memory block into a newly allocated region in the heap
                    760:     swap >r
                    761:     dup allocate throw
                    762:     swap 2dup r> -rot move ;
                    763: 
1.68      anton     764: : free-mem-var ( addr -- )
                    765:     \ addr is the address of a 2variable containing address and size
                    766:     \ of a memory range; frees memory and clears the 2variable.
                    767:     dup 2@ drop dup
                    768:     if ( addr mem-start )
                    769:        free throw
                    770:        0 0 rot 2!
                    771:     else
                    772:        2drop
                    773:     then ;
                    774: 
1.1       pazsan    775: : extend-mem   ( addr1 u1 u -- addr addr2 u2 )
                    776:     \ extend memory block allocated from the heap by u aus
1.105     anton     777:     \ the (possibly reallocated) piece is addr2 u2, the extension is at addr
1.1       pazsan    778:     over >r + dup >r resize throw
                    779:     r> over r> + -rot ;
                    780: [THEN]
                    781: 
                    782: \ EVALUATE                                              17may93jaw
                    783: 
1.64      pazsan    784: has? file 0= has? new-input 0= and [IF]
1.1       pazsan    785: : push-file  ( -- )  r>
1.12      pazsan    786:   tibstack @ >r  >tib @ >r  #tib @ >r
1.1       pazsan    787:   >tib @ tibstack @ = IF  r@ tibstack +!  THEN
                    788:   tibstack @ >tib ! >in @ >r  >r ;
                    789: 
                    790: : pop-file   ( throw-code -- throw-code )
                    791:   r>
1.12      pazsan    792:   r> >in !  r> #tib !  r> >tib !  r> tibstack !  >r ;
1.1       pazsan    793: [THEN]
                    794: 
1.64      pazsan    795: has? new-input 0= [IF]
1.29      crook     796: : evaluate ( c-addr u -- ) \ core,block
1.40      crook     797:     \G Save the current input source specification. Store @code{-1} in
                    798:     \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
                    799:     \G @code{0} and make the string @i{c-addr u} the input source
                    800:     \G and input buffer. Interpret. When the parse area is empty,
                    801:     \G restore the input source specification.
1.64      pazsan    802: [ has? file [IF] ]
1.92      anton     803:     s" *evaluated string*" loadfilename>r
1.64      pazsan    804: [ [THEN] ]
1.40      crook     805:     push-file #tib ! >tib !
1.130     anton     806:     input-start-line
1.29      crook     807:     [ has? file [IF] ]
                    808:        blk off loadfile off -1 loadline !
                    809:        [ [THEN] ]
                    810:     ['] interpret catch
1.56      anton     811:     pop-file
1.64      pazsan    812: [ has? file [IF] ]
1.92      anton     813:     r>loadfilename
1.64      pazsan    814: [ [THEN] ]
1.56      anton     815:     throw ;
1.64      pazsan    816: [THEN]
1.1       pazsan    817: 
                    818: \ \ Quit                                               13feb93py
                    819: 
                    820: Defer 'quit
                    821: 
                    822: Defer .status
                    823: 
                    824: : prompt        state @ IF ."  compiled" EXIT THEN ."  ok" ;
                    825: 
1.39      anton     826: : (quit) ( -- )
                    827:     \ exits only through THROW etc.
                    828:     BEGIN
1.98      anton     829:        .status
                    830:        ['] cr catch if
1.144     pazsan    831:            [ has? OS [IF] ] >stderr [ [THEN] ]
                    832:            cr ." Can't print to stdout, leaving" cr
1.98      anton     833:            \ if stderr does not work either, already DoError causes a hang
                    834:            2 (bye)
                    835:        endif
1.138     pazsan    836:        refill  WHILE
1.122     anton     837:            interpret prompt
                    838:     REPEAT
                    839:     bye ;
1.1       pazsan    840: 
                    841: ' (quit) IS 'quit
                    842: 
                    843: \ \ DOERROR (DOERROR)                                  13jun93jaw
                    844: 
1.131     pazsan    845: has? ec 0= [IF]
1.1       pazsan    846: 8 Constant max-errors
1.130     anton     847: 5 has? file 2 and + Constant /error
1.1       pazsan    848: Variable error-stack  0 error-stack !
1.112     pazsan    849: max-errors /error * cells allot
1.1       pazsan    850: \ format of one cell:
1.133     anton     851: \ source ( c-addr u )
                    852: \ last parsed lexeme ( c-addr u )
1.1       pazsan    853: \ line-number
                    854: \ Loadfilename ( addr u )
                    855: 
1.133     anton     856: : error> ( --  c-addr1 u1 c-addr2 u2 line# [addr u] )
1.64      pazsan    857:     -1 error-stack +!
                    858:     error-stack dup @
1.112     pazsan    859:     /error * cells + cell+
                    860:     /error cells bounds DO
1.130     anton     861:         I @
                    862:     cell +LOOP ;
                    863: 
1.133     anton     864: : >error ( c-addr1 u1 c-addr2 u2 line# [addr u] -- )
1.64      pazsan    865:     error-stack dup @ dup 1+
                    866:     max-errors 1- min error-stack !
1.112     pazsan    867:     /error * cells + cell+
                    868:     /error 1- cells bounds swap DO
1.130     anton     869:         I !
                    870:     -1 cells +LOOP ;
                    871: 
1.133     anton     872: : input-error-data ( -- c-addr1 u1 c-addr2 u2 line# [addr u] )
1.130     anton     873:     \ error data for the current input, to be used by >error or .error-frame
1.133     anton     874:     source input-lexeme 2@ sourceline#
1.130     anton     875:     [ has? file [IF] ] sourcefilename [ [THEN] ] ;
1.64      pazsan    876: 
1.1       pazsan    877: : dec. ( n -- ) \ gforth
1.40      crook     878:     \G Display @i{n} as a signed decimal number, followed by a space.
                    879:     \ !! not used...
1.1       pazsan    880:     base @ decimal swap . base ! ;
                    881: 
1.111     anton     882: : dec.r ( u n -- ) \ gforth
                    883:     \G Display @i{u} as a unsigned decimal number in a field @i{n}
                    884:     \G characters wide.
                    885:     base @ >r decimal .r r> base ! ;
1.23      jwilke    886: 
1.1       pazsan    887: : hex. ( u -- ) \ gforth
1.40      crook     888:     \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
1.17      crook     889:     \G followed by a space.
1.40      crook     890:     \ !! not used...
1.33      jwilke    891:     [char] $ emit base @ swap hex u. base ! ;
1.1       pazsan    892: 
1.94      anton     893: : -trailing  ( c_addr u1 -- c_addr u2 ) \ string dash-trailing
                    894: \G Adjust the string specified by @i{c-addr, u1} to remove all
                    895: \G trailing spaces. @i{u2} is the length of the modified string.
                    896:     BEGIN
1.102     pazsan    897:        dup
1.94      anton     898:     WHILE
1.102     pazsan    899:        1- 2dup + c@ bl <>
                    900:     UNTIL  1+  THEN ;
1.94      anton     901: 
1.1       pazsan    902: DEFER DOERROR
1.33      jwilke    903: 
                    904: has? backtrace [IF]
1.15      anton     905: Defer dobacktrace ( -- )
                    906: ' noop IS dobacktrace
1.33      jwilke    907: [THEN]
1.1       pazsan    908: 
1.23      jwilke    909: : .error-string ( throw-code -- )
                    910:   dup -2 = 
                    911:   IF   "error @ ?dup IF count type  THEN drop
                    912:   ELSE .error
                    913:   THEN ;
                    914: 
1.111     anton     915: : umin ( u1 u2 -- u )
                    916:     2dup u>
                    917:     if
                    918:        swap
                    919:     then
                    920:     drop ;
                    921: 
1.112     pazsan    922: Defer mark-start
                    923: Defer mark-end
                    924: 
                    925: :noname ." >>>" ; IS mark-start
                    926: :noname ." <<<" ; IS mark-end
                    927: 
1.130     anton     928: : part-type ( addr1 u1 u -- addr2 u2 )
                    929:     \ print first u characters of addr1 u1, addr2 u2 is the rest
1.133     anton     930:     over umin 2 pick over type /string ;
1.130     anton     931: 
1.133     anton     932: : .error-line ( c-addr1 u1 c-addr2 u2 -- )
                    933:     \ print error in line c-addr1 u1, where the error-causing lexeme
                    934:     \ is c-addr2 u2
                    935:     >r 2 pick - part-type ( c-addr3 u3 R: u2 )
                    936:     mark-start r> part-type mark-end ( c-addr4 u4 )
                    937:     type ;
1.130     anton     938: 
1.133     anton     939: : .error-frame ( throwcode addr1 u1 addr2 u2 n2 [addr3 u3] -- throwcode )
                    940:     \ addr3 u3: filename of included file - optional
1.130     anton     941:     \ n2:       line number
1.133     anton     942:     \ addr2 u2: parsed lexeme (should be marked as causing the error)
1.130     anton     943:     \ addr1 u1: input line
                    944:     error-stack @
                    945:     IF ( throwcode addr1 u1 n0 n1 n2 [addr2 u2] )
                    946:         [ has? file [IF] ] \ !! unbalanced stack effect
1.129     pazsan    947:          over IF
                    948:              cr ." in file included from "
                    949:              type ." :"
1.130     anton     950:              0 dec.r  2drop 2drop
                    951:           ELSE
                    952:               2drop 2drop 2drop drop
                    953:           THEN
                    954:           [ [THEN] ] ( throwcode addr1 u1 n0 n1 n2 )
                    955:     ELSE ( throwcode addr1 u1 n0 n1 n2 [addr2 u2] )
                    956:         [ has? file [IF] ]
                    957:             cr type ." :"
                    958:             [ [THEN] ] ( throwcode addr1 u1 n0 n1 n2 )
                    959:         dup 0 dec.r ." : " 5 pick .error-string
                    960:         IF \ if line# non-zero, there is a line
                    961:             cr .error-line
                    962:         ELSE
                    963:             2drop 2drop
                    964:         THEN
                    965:     THEN ;
1.1       pazsan    966: 
                    967: : (DoError) ( throw-code -- )
                    968:   [ has? os [IF] ]
1.8       pazsan    969:       >stderr
1.1       pazsan    970:   [ [THEN] ] 
1.130     anton     971:   input-error-data .error-frame
1.1       pazsan    972:   error-stack @ 0 ?DO
1.64      pazsan    973:     error>
1.1       pazsan    974:     .error-frame
                    975:   LOOP
1.33      jwilke    976:   drop 
                    977: [ has? backtrace [IF] ]
                    978:   dobacktrace
                    979: [ [THEN] ]
1.8       pazsan    980:   normal-dp dpp ! ;
1.1       pazsan    981: 
                    982: ' (DoError) IS DoError
1.131     pazsan    983: 
                    984: [ELSE]
                    985:     : dec.  base @ >r decimal . r> base ! ;
1.144     pazsan    986:     : DoError ( throw-code -- )
1.145     pazsan    987:        cr source drop >in @ type ." <<< "
1.144     pazsan    988:        dup -2 =  IF  "error @ type  drop  EXIT  THEN
                    989:        .error ;
1.131     pazsan    990: [THEN]
1.1       pazsan    991: 
                    992: : quit ( ?? -- ?? ) \ core
1.27      crook     993:     \G Empty the return stack, make the user input device
                    994:     \G the input source, enter interpret state and start
                    995:     \G the text interpreter.
1.64      pazsan    996:     rp0 @ rp! handler off clear-tibstack
                    997:     [ has? new-input 0= [IF] ] >tib @ >r [ [THEN] ]
1.1       pazsan    998:     BEGIN
                    999:        [ has? compiler [IF] ]
1.104     anton    1000:            [compile] [
1.1       pazsan   1001:        [ [THEN] ]
1.104     anton    1002:        \ stack depths may be arbitrary here
1.1       pazsan   1003:        ['] 'quit CATCH dup
                   1004:     WHILE
1.104     anton    1005:            <# \ reset hold area, or we may get another error
                   1006:            DoError
                   1007:            \ stack depths may be arbitrary still (or again), so clear them
                   1008:            clearstacks
                   1009:            [ has? new-input [IF] ] clear-tibstack
                   1010:            [ [ELSE] ] r@ >tib ! r@ tibstack !
                   1011:            [ [THEN] ]
1.1       pazsan   1012:     REPEAT
1.64      pazsan   1013:     drop [ has? new-input [IF] ] clear-tibstack
                   1014:     [ [ELSE] ] r> >tib !
                   1015:     [ [THEN] ] ;
1.1       pazsan   1016: 
                   1017: \ \ Cold Boot                                          13feb93py
                   1018: 
                   1019: : (bootmessage)
1.101     anton    1020:     ." Gforth " version-string type 
1.130     anton    1021:     ." , Copyright (C) 1995-2006 Free Software Foundation, Inc." cr
1.101     anton    1022:     ." Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
1.1       pazsan   1023: [ has? os [IF] ]
                   1024:      cr ." Type `bye' to exit"
                   1025: [ [THEN] ] ;
                   1026: 
                   1027: defer bootmessage
1.135     pazsan   1028: has? file [IF]
1.1       pazsan   1029: defer process-args
1.135     pazsan   1030: [THEN]
1.1       pazsan   1031: 
                   1032: ' (bootmessage) IS bootmessage
                   1033: 
1.136     pazsan   1034: has? ec 0= [IF]
1.10      anton    1035: Defer 'cold ( -- ) \ gforth  tick-cold
1.1       pazsan   1036: \ hook (deferred word) for things to do right before interpreting the
                   1037: \ command-line arguments
                   1038: ' noop IS 'cold
                   1039: 
1.76      jwilke   1040: AVariable init8 NIL init8 !
1.148   ! pazsan   1041: [THEN]
1.1       pazsan   1042: 
                   1043: : cold ( -- ) \ gforth
1.135     pazsan   1044:     [ has? backtrace [IF] ]
1.44      anton    1045:     rp@ backtrace-rp0 !
                   1046: [ [THEN] ]
1.1       pazsan   1047: [ has? file [IF] ]
1.78      pazsan   1048:     os-cold
1.1       pazsan   1049: [ [THEN] ]
1.126     pazsan   1050: [ has? ec 0= [IF] ]
1.116     anton    1051:     set-encoding-fixed-width
1.136     pazsan   1052:     'cold
1.148   ! pazsan   1053:     init8 chainperform
1.126     pazsan   1054: [ [THEN] ]
1.1       pazsan   1055: [ has? file [IF] ]
1.8       pazsan   1056:     process-args
1.12      pazsan   1057:     loadline off
1.1       pazsan   1058: [ [THEN] ]
                   1059:     bootmessage
1.12      pazsan   1060:     quit ;
1.1       pazsan   1061: 
1.64      pazsan   1062: has? new-input 0= [IF]
1.5       anton    1063: : clear-tibstack ( -- )
                   1064: [ has? glocals [IF] ]
                   1065:     lp@ forthstart 7 cells + @ - 
                   1066: [ [ELSE] ]
                   1067:     [ has? os [IF] ]
1.8       pazsan   1068:     r0 @ forthstart 6 cells + @ -
1.5       anton    1069:     [ [ELSE] ]
1.139     pazsan   1070:     sp@ cell+
1.5       anton    1071:     [ [THEN] ]
                   1072: [ [THEN] ]
1.138     pazsan   1073:     dup >tib ! tibstack ! #tib off
                   1074:     input-start-line ;
1.64      pazsan   1075: [THEN]
1.5       anton    1076: 
1.64      pazsan   1077: : boot ( path n **argv argc -- )
1.134     pazsan   1078: [ has? no-userspace 0= [IF] ]
1.1       pazsan   1079:     main-task up!
1.134     pazsan   1080: [ [THEN] ]
1.1       pazsan   1081: [ has? os [IF] ]
1.78      pazsan   1082:     os-boot
1.134     pazsan   1083: [ [THEN] ]
                   1084: [ has? rom [IF] ]
1.147     pazsan   1085:     ram-shadow dup @ dup -1 <> >r u> r> and IF
                   1086:        ram-shadow 2@  ELSE
                   1087:        ram-mirror ram-size  THEN  ram-start swap move
1.1       pazsan   1088: [ [THEN] ]
                   1089:     sp@ sp0 !
1.74      pazsan   1090: [ has? peephole [IF] ]
1.87      anton    1091:     \ only needed for greedy static superinstruction selection
                   1092:     \ primtable prepare-peephole-table TO peeptable
1.74      pazsan   1093: [ [THEN] ]
1.64      pazsan   1094: [ has? new-input [IF] ]
                   1095:     current-input off
                   1096: [ [THEN] ]
1.5       anton    1097:     clear-tibstack
1.127     anton    1098:     0 0 includefilename 2!
1.1       pazsan   1099:     rp@ rp0 !
                   1100: [ has? floating [IF] ]
                   1101:     fp@ fp0 !
                   1102: [ [THEN] ]
1.46      anton    1103:     handler off
1.98      anton    1104:     ['] cold catch dup -&2049 <> if \ broken pipe?
                   1105:        DoError cr
                   1106:     endif
1.1       pazsan   1107: [ has? os [IF] ]
1.35      anton    1108:     1 (bye) \ !! determin exit code from throw code?
1.1       pazsan   1109: [ [THEN] ]
                   1110: ;
                   1111: 
                   1112: has? os [IF]
                   1113: : bye ( -- ) \ tools-ext
                   1114: [ has? file [IF] ]
                   1115:     script? 0= IF  cr  THEN
                   1116: [ [ELSE] ]
                   1117:     cr
                   1118: [ [THEN] ]
                   1119:     0 (bye) ;
                   1120: [THEN]
                   1121: 
                   1122: \ **argv may be scanned by the C starter to get some important
                   1123: \ information, as -display and -geometry for an X client FORTH
                   1124: \ or space and stackspace overrides
                   1125: 
                   1126: \ 0 arg contains, however, the name of the program.
                   1127: 

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