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

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

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