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

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

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