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

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

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