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

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

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