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

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

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