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

1.1       pazsan      1: \ definitions needed for interpreter only
                      2: 
1.52      pazsan      3: \ Copyright (C) 1995-2000 Free Software Foundation, Inc.
1.11      anton       4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
1.63    ! anton      19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.11      anton      20: 
1.1       pazsan     21: \ \ Revision-Log
                     22: 
                     23: \       put in seperate file                           14sep97jaw 
                     24: 
                     25: \ \ input stream primitives                            23feb93py
                     26: 
1.33      jwilke     27: require ./basics.fs    \ bounds decimal hex ...
                     28: require ./io.fs                \ type ...
                     29: require ./nio.fs       \ . <# ...
                     30: require ./errore.fs    \ .error ...
1.50      anton      31: require kernel/version.fs      \ version-string
1.33      jwilke     32: require ./../chains.fs
                     33: 
1.43      crook      34: : tib ( -- c-addr ) \ core-ext t-i-b
1.40      crook      35:     \G @i{c-addr} is the address of the Terminal Input Buffer.
1.29      crook      36:     \G OBSOLESCENT: @code{source} superceeds the function of this word.
1.1       pazsan     37:     >tib @ ;
                     38: 
1.29      crook      39: Defer source ( -- c-addr u ) \ core
1.1       pazsan     40: \ used by dodefer:, must be defer
1.40      crook      41: \G @i{c-addr} is the address of the input buffer and @i{u} is the
1.29      crook      42: \G number of characters in it.
1.1       pazsan     43: 
1.29      crook      44: : (source) ( -- c-addr u )
1.1       pazsan     45:     tib #tib @ ;
                     46: ' (source) IS source
                     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.29      crook      56: : sword  ( char -- addr len ) \ gforth s-word
1.40      crook      57:     \G Parses like @code{word}, but the output is like @code{parse} output.
1.47      anton      58:     \G @xref{core-idef}.
1.3       anton      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).
1.1       pazsan     62:   source 2dup >r >r >in @ over min /string
                     63:   rot dup bl = IF  drop (parse-white)  ELSE  (word)  THEN
                     64:   2dup + r> - 1+ r> min >in ! ;
                     65: 
1.29      crook      66: : word   ( char "<chars>ccc<char>-- c-addr ) \ core
1.40      crook      67:     \G Skip leading delimiters. Parse @i{ccc}, delimited by
                     68:     \G @i{char}, in the parse area. @i{c-addr} is the address of a
1.29      crook      69:     \G transient region containing the parsed string in
1.40      crook      70:     \G counted-string format. If the parse area was empty or
1.29      crook      71:     \G contained no characters other than delimiters, the resulting
                     72:     \G string has zero length. A program may replace characters within
                     73:     \G the counted string. OBSOLESCENT: the counted string has a
                     74:     \G trailing space that is not included in its length.
                     75:     sword here place  bl here count + c!  here ;
                     76: 
                     77: : parse    ( char "ccc<char>" -- c-addr u ) \ core-ext
1.40      crook      78:     \G Parse @i{ccc}, delimited by @i{char}, in the parse
                     79:     \G area. @i{c-addr u} specifies the parsed string within the
                     80:     \G parse area. If the parse area was empty, @i{u} is 0.
1.29      crook      81:     >r  source  >in @ over min /string  over  swap r>  scan >r
1.1       pazsan     82:   over - dup r> IF 1+ THEN  >in +! ;
                     83: 
                     84: \ name                                                 13feb93py
                     85: 
                     86: [IFUNDEF] (name) \ name might be a primitive
                     87: 
1.40      crook      88: : (name) ( -- c-addr count ) \ gforth
1.1       pazsan     89:     source 2dup >r >r >in @ /string (parse-white)
                     90:     2dup + r> - 1+ r> min >in ! ;
                     91: \    name count ;
                     92: [THEN]
                     93: 
                     94: : name-too-short? ( c-addr u -- c-addr u )
                     95:     dup 0= -&16 and throw ;
                     96: 
                     97: : name-too-long? ( c-addr u -- c-addr u )
                     98:     dup $1F u> -&19 and throw ;
                     99: 
                    100: \ \ Number parsing                                     23feb93py
                    101: 
                    102: \ number? number                                       23feb93py
                    103: 
                    104: hex
                    105: const Create bases   10 ,   2 ,   A , 100 ,
                    106: \                     16     2    10   character
                    107: 
1.18      anton     108: \ !! protect BASE saving wrapper against exceptions
1.1       pazsan    109: : getbase ( addr u -- addr' u' )
                    110:     over c@ [char] $ - dup 4 u<
                    111:     IF
                    112:        cells bases + @ base ! 1 /string
                    113:     ELSE
                    114:        drop
                    115:     THEN ;
                    116: 
1.20      pazsan    117: : sign? ( addr u -- addr u flag )
1.33      jwilke    118:     over c@ [char] - =  dup >r
1.1       pazsan    119:     IF
                    120:        1 /string
                    121:     THEN
1.20      pazsan    122:     r> ;
                    123: 
                    124: : s>unumber? ( addr u -- ud flag )
1.21      pazsan    125:     base @ >r  dpl on  getbase
1.20      pazsan    126:     0. 2swap
1.18      anton     127:     BEGIN ( d addr len )
1.1       pazsan    128:        dup >r >number dup
1.18      anton     129:     WHILE \ there are characters left
1.1       pazsan    130:        dup r> -
1.18      anton     131:     WHILE \ the last >number parsed something
                    132:        dup 1- dpl ! over c@ [char] . =
                    133:     WHILE \ the current char is '.'
1.1       pazsan    134:        1 /string
1.18      anton     135:     REPEAT  THEN \ there are unparseable characters left
1.21      pazsan    136:        2drop false
1.20      pazsan    137:     ELSE
                    138:        rdrop 2drop true
1.21      pazsan    139:     THEN
                    140:     r> base ! ;
1.20      pazsan    141: 
                    142: \ ouch, this is complicated; there must be a simpler way - anton
                    143: : s>number? ( addr len -- d f )
                    144:     \ converts string addr len into d, flag indicates success
1.21      pazsan    145:     sign? >r
1.20      pazsan    146:     s>unumber?
                    147:     0= IF
1.21      pazsan    148:         rdrop false
1.18      anton     149:     ELSE \ no characters left, all ok
1.20      pazsan    150:        r>
1.1       pazsan    151:        IF
                    152:            dnegate
                    153:        THEN
1.18      anton     154:        true
1.21      pazsan    155:     THEN ;
1.1       pazsan    156: 
1.18      anton     157: : s>number ( addr len -- d )
                    158:     \ don't use this, there is no way to tell success
                    159:     s>number? drop ;
                    160: 
1.1       pazsan    161: : snumber? ( c-addr u -- 0 / n -1 / d 0> )
1.18      anton     162:     s>number? 0=
1.1       pazsan    163:     IF
                    164:        2drop false  EXIT
                    165:     THEN
1.18      anton     166:     dpl @ dup 0< IF
1.1       pazsan    167:        nip
1.18      anton     168:     ELSE
                    169:        1+
1.1       pazsan    170:     THEN ;
                    171: 
                    172: : number? ( string -- string 0 / n -1 / d 0> )
                    173:     dup >r count snumber? dup if
                    174:        rdrop
                    175:     else
                    176:        r> swap
                    177:     then ;
                    178: 
                    179: : number ( string -- d )
                    180:     number? ?dup 0= abort" ?"  0<
                    181:     IF
                    182:        s>d
                    183:     THEN ;
                    184: 
                    185: \ \ Comments ( \ \G
                    186: 
1.29      crook     187: : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ thisone- core,file   paren
1.17      crook     188:     \G ** this will not get annotated. The alias in glocals.fs will instead **
1.29      crook     189:     \G It does not work to use "wordset-" prefix since this file is glossed
                    190:     \G by cross.fs which doesn't have the same functionalty as makedoc.fs
1.1       pazsan    191:     [char] ) parse 2drop ; immediate
                    192: 
1.51      anton     193: : \ ( compilation 'ccc<newline>' -- ; run-time -- ) \ thisone- core-ext,block-ext backslash
1.29      crook     194:     \G ** this will not get annotated. The alias in glocals.fs will instead ** 
                    195:     \G It does not work to use "wordset-" prefix since this file is glossed
                    196:     \G by cross.fs which doesn't have the same functionalty as makedoc.fs
1.12      pazsan    197:     [ has? file [IF] ]
1.1       pazsan    198:     blk @
                    199:     IF
                    200:        >in @ c/l / 1+ c/l * >in !
                    201:        EXIT
                    202:     THEN
1.12      pazsan    203:     [ [THEN] ]
1.1       pazsan    204:     source >in ! drop ; immediate
                    205: 
1.51      anton     206: : \G ( compilation 'ccc<newline>' -- ; run-time -- ) \ gforth backslash-gee
1.19      crook     207:     \G Equivalent to @code{\} but used as a tag to annotate definition
                    208:     \G comments into documentation.
1.1       pazsan    209:     POSTPONE \ ; immediate
                    210: 
                    211: \ \ object oriented search list                         17mar93py
                    212: 
                    213: \ word list structure:
                    214: 
                    215: struct
                    216:   cell% field find-method   \ xt: ( c_addr u wid -- nt )
                    217:   cell% field reveal-method \ xt: ( nt wid -- ) \ used by dofield:, must be field
                    218:   cell% field rehash-method \ xt: ( wid -- )      \ re-initializes a "search-data" (hashtables)
                    219:   cell% field hash-method   \ xt: ( wid -- )    \ initializes ""
                    220: \   \ !! what else
                    221: end-struct wordlist-map-struct
                    222: 
                    223: struct
1.6       pazsan    224:   cell% field wordlist-map \ pointer to a wordlist-map-struct
1.13      anton     225:   cell% field wordlist-id \ linked list of words (for WORDS etc.)
1.1       pazsan    226:   cell% field wordlist-link \ link field to other wordlists
1.13      anton     227:   cell% field wordlist-extend \ wordlist extensions (eg bucket offset)
1.1       pazsan    228: end-struct wordlist-struct
                    229: 
                    230: : f83find      ( addr len wordlist -- nt / false )
1.6       pazsan    231:     wordlist-id @ (f83find) ;
1.1       pazsan    232: 
                    233: : initvoc              ( wid -- )
                    234:   dup wordlist-map @ hash-method perform ;
                    235: 
                    236: \ Search list table: find reveal
                    237: Create f83search ( -- wordlist-map )
                    238:     ' f83find A,  ' drop A,  ' drop A, ' drop A,
                    239: 
1.6       pazsan    240: here G f83search T A, NIL A, NIL A, NIL A,
1.1       pazsan    241: AValue forth-wordlist \ variable, will be redefined by search.fs
                    242: 
                    243: AVariable lookup               forth-wordlist lookup !
                    244: \ !! last is user and lookup?! jaw
                    245: AVariable current ( -- addr ) \ gforth
1.43      crook     246: \G @code{Variable} -- holds the @i{wid} of the compilation word list.
1.1       pazsan    247: AVariable voclink      forth-wordlist wordlist-link voclink !
1.38      anton     248: \ lookup AValue context ( -- addr ) \ gforth
                    249: Defer context ( -- addr ) \ gforth
1.43      crook     250: \G @code{context} @code{@@} is the @i{wid} of the word list at the
                    251: \G top of the search order.
1.1       pazsan    252: 
1.38      anton     253: ' lookup is context
1.1       pazsan    254: forth-wordlist current !
                    255: 
                    256: \ \ header, finding, ticks                              17dec92py
                    257: 
1.33      jwilke    258: hex
                    259: 80 constant alias-mask \ set when the word is not an alias!
                    260: 40 constant immediate-mask
                    261: 20 constant restrict-mask
1.1       pazsan    262: 
                    263: \ higher level parts of find
                    264: 
                    265: : flag-sign ( f -- 1|-1 )
                    266:     \ true becomes 1, false -1
                    267:     0= 2* 1+ ;
                    268: 
                    269: : compile-only-error ( ... -- )
                    270:     -&14 throw ;
                    271: 
                    272: : (cfa>int) ( cfa -- xt )
                    273: [ has? compiler [IF] ]
                    274:     dup interpret/compile?
                    275:     if
                    276:        interpret/compile-int @
                    277:     then 
                    278: [ [THEN] ] ;
                    279: 
                    280: : (x>int) ( cfa b -- xt )
                    281:     \ get interpretation semantics of name
                    282:     restrict-mask and
                    283:     if
                    284:        drop ['] compile-only-error
                    285:     else
                    286:        (cfa>int)
                    287:     then ;
                    288: 
                    289: : name>string ( nt -- addr count ) \ gforth     head-to-string
1.40      crook     290:     \g @i{addr count} is the name of the word represented by @i{nt}.
1.1       pazsan    291:     cell+ count $1F and ;
                    292: 
                    293: : ((name>))  ( nfa -- cfa )
                    294:     name>string + cfaligned ;
                    295: 
                    296: : (name>x) ( nfa -- cfa b )
                    297:     \ cfa is an intermediate cfa and b is the flags byte of nfa
                    298:     dup ((name>))
                    299:     swap cell+ c@ dup alias-mask and 0=
                    300:     IF
                    301:         swap @ swap
                    302:     THEN ;
                    303: 
                    304: : name>int ( nt -- xt ) \ gforth
1.31      crook     305:     \G @i{xt} represents the interpretation semantics of the word
                    306:     \G @i{nt}. If @i{nt} has no interpretation semantics (i.e. is
                    307:     \G @code{compile-only}), @i{xt} is the execution token for
                    308:     \G @code{compile-only-error}, which performs @code{-14 throw}.
1.1       pazsan    309:     (name>x) (x>int) ;
                    310: 
                    311: : name?int ( nt -- xt ) \ gforth
1.31      crook     312:     \G Like @code{name>int}, but perform @code{-14 throw} if @i{nt}
                    313:     \G has no interpretation semantics.
1.1       pazsan    314:     (name>x) restrict-mask and
                    315:     if
                    316:        compile-only-error \ does not return
                    317:     then
                    318:     (cfa>int) ;
                    319: 
                    320: : (name>comp) ( nt -- w +-1 ) \ gforth
1.31      crook     321:     \G @i{w xt} is the compilation token for the word @i{nt}.
1.1       pazsan    322:     (name>x) >r 
                    323: [ has? compiler [IF] ]
                    324:     dup interpret/compile?
                    325:     if
                    326:         interpret/compile-comp @
                    327:     then 
                    328: [ [THEN] ]
                    329:     r> immediate-mask and flag-sign
                    330:     ;
                    331: 
                    332: : (name>intn) ( nfa -- xt +-1 )
                    333:     (name>x) tuck (x>int) ( b xt )
                    334:     swap immediate-mask and flag-sign ;
                    335: 
1.30      jwilke    336: const Create ???  0 , 3 c, char ? c, char ? c, char ? c,
                    337: \ ??? is used by dovar:, must be created/:dovar
                    338: 
                    339: [IFDEF] forthstart
                    340: \ if we have a forthstart we can define head? with it
                    341: \ otherwise leave out the head? check
                    342: 
1.14      anton     343: : head? ( addr -- f )
                    344:     \G heuristic check whether addr is a name token; may deliver false
                    345:     \G positives; addr must be a valid address
                    346:     \ we follow the link fields and check for plausibility; two
                    347:     \ iterations should catch most false addresses: on the first
                    348:     \ iteration, we may get an xt, on the second a code address (or
                    349:     \ some code), which is typically not in the dictionary.
                    350:     2 0 do
1.41      anton     351:        dup dup aligned <> if \ protect @ against unaligned accesses
                    352:            drop false unloop exit
                    353:        then
1.14      anton     354:        dup @ dup
                    355:        if ( addr addr1 )
                    356:            dup rot forthstart within
                    357:            if \ addr1 is outside forthstart..addr, not a head
                    358:                drop false unloop exit
                    359:            then ( addr1 )
                    360:        else \ 0 in the link field, no further checks
                    361:            2drop true unloop exit
                    362:        then
                    363:     loop
                    364:     \ in dubio pro:
                    365:     drop true ;
                    366: 
1.48      anton     367: : >head-noprim ( cfa -- nt ) \ gforth  to-head-noprim
1.45      pazsan    368:     $25 cell do ( cfa )
1.14      anton     369:        dup i - count $9F and + cfaligned over alias-mask + =
                    370:        if ( cfa )
                    371:            dup i - cell - dup head?
                    372:            if
                    373:                nip unloop exit
                    374:            then
                    375:            drop
                    376:        then
                    377:        cell +loop
                    378:     drop ??? ( wouldn't 0 be better? ) ;
1.1       pazsan    379: 
1.30      jwilke    380: [ELSE]
                    381: 
1.48      anton     382: : >head-noprim ( cfa -- nt ) \ gforth  to-head-noprim
1.45      pazsan    383:     $25 cell do ( cfa )
1.30      jwilke    384:        dup i - count $9F and + cfaligned over alias-mask + =
                    385:        if ( cfa ) i - cell - unloop exit
                    386:        then
                    387:        cell +loop
                    388:     drop ??? ( wouldn't 0 be better? ) ;
                    389: 
                    390: [THEN]
1.1       pazsan    391: 
                    392: : body> 0 >body - ;
                    393: 
1.31      crook     394: : (search-wordlist)  ( addr count wid -- nt | false )
1.1       pazsan    395:     dup wordlist-map @ find-method perform ;
                    396: 
1.31      crook     397: : search-wordlist ( c-addr count wid -- 0 | xt +-1 ) \ search
1.53      anton     398:     \G Search the word list identified by @i{wid} for the definition
                    399:     \G named by the string at @i{c-addr count}.  If the definition is
                    400:     \G not found, return 0. If the definition is found return 1 (if
                    401:     \G the definition is immediate) or -1 (if the definition is not
                    402:     \G immediate) together with the @i{xt}.  In Gforth, the @i{xt}
                    403:     \G returned represents the interpretation semantics.  ANS Forth
                    404:     \G does not specify clearly what @i{xt} represents.
1.1       pazsan    405:     (search-wordlist) dup if
                    406:        (name>intn)
                    407:     then ;
                    408: 
1.31      crook     409: : find-name ( c-addr u -- nt | 0 ) \ gforth
                    410:     \g Find the name @i{c-addr u} in the current search
                    411:     \g order. Return its @i{nt}, if found, otherwise 0.
1.1       pazsan    412:     lookup @ (search-wordlist) ;
                    413: 
                    414: : sfind ( c-addr u -- 0 / xt +-1  ) \ gforth-obsolete
                    415:     find-name dup
                    416:     if ( nt )
                    417:        state @
                    418:        if
                    419:            (name>comp)
                    420:        else
                    421:            (name>intn)
                    422:        then
                    423:    then ;
                    424: 
1.31      crook     425: : find ( c-addr -- xt +-1 | c-addr 0 ) \ core,search
1.53      anton     426:     \G Search all word lists in the current search order for the
                    427:     \G definition named by the counted string at @i{c-addr}.  If the
                    428:     \G definition is not found, return 0. If the definition is found
                    429:     \G return 1 (if the definition has non-default compilation
                    430:     \G semantics) or -1 (if the definition has default compilation
                    431:     \G semantics).  The @i{xt} returned in interpret state represents
                    432:     \G the interpretation semantics.  The @i{xt} returned in compile
                    433:     \G state represented either the compilation semantics (for
                    434:     \G non-default compilation semantics) or the run-time semantics
                    435:     \G that the compilation semantics would @code{compile,} (for
                    436:     \G default compilation semantics).  The ANS Forth standard does
                    437:     \G not specify clearly what the returned @i{xt} represents (and
                    438:     \G also talks about immediacy instead of non-default compilation
                    439:     \G semantics), so this word is questionable in portable programs.
                    440:     \G If non-portability is ok, @code{find-name} and friends are
                    441:     \G better (@pxref{Name token}).
1.1       pazsan    442:     dup count sfind dup
                    443:     if
                    444:        rot drop
                    445:     then ;
                    446: 
1.34      jwilke    447: \ ticks in interpreter
1.1       pazsan    448: 
                    449: : (') ( "name" -- nt ) \ gforth
1.32      anton     450:     name name-too-short?
1.28      anton     451:     find-name dup 0=
1.1       pazsan    452:     IF
1.42      anton     453:        drop -&13 throw
1.1       pazsan    454:     THEN  ;
                    455: 
                    456: : '    ( "name" -- xt ) \ core tick
1.31      crook     457:     \g @i{xt} represents @i{name}'s interpretation
                    458:     \g semantics. Perform @code{-14 throw} if the word has no
1.1       pazsan    459:     \g interpretation semantics.
                    460:     (') name?int ;
1.34      jwilke    461: 
                    462: has? compiler 0= [IF]  \ interpreter only version of IS and TO
                    463: 
                    464: : IS ' >body ! ;
                    465: ' IS Alias TO
                    466: 
                    467: [THEN]
1.1       pazsan    468: 
                    469: \ \ the interpreter loop                                 mar92py
                    470: 
                    471: \ interpret                                            10mar92py
                    472: 
1.37      anton     473: Defer parser ( c-addr u -- )
1.1       pazsan    474: Defer name ( -- c-addr count ) \ gforth
1.55      anton     475: \G Get the next word from the input buffer
1.1       pazsan    476: ' (name) IS name
                    477: Defer compiler-notfound ( c-addr count -- )
                    478: Defer interpreter-notfound ( c-addr count -- )
                    479: 
                    480: : no.extensions  ( addr u -- )
1.42      anton     481:     2drop -&13 throw ;
1.1       pazsan    482: ' no.extensions IS compiler-notfound
                    483: ' no.extensions IS interpreter-notfound
                    484: 
                    485: : interpret ( ?? -- ?? ) \ gforth
                    486:     \ interpret/compile the (rest of the) input buffer
1.33      jwilke    487: [ has? backtrace [IF] ]
1.24      anton     488:     rp@ backtrace-rp0 !
1.33      jwilke    489: [ [THEN] ]
1.1       pazsan    490:     BEGIN
                    491:        ?stack name dup
                    492:     WHILE
                    493:        parser
                    494:     REPEAT
                    495:     2drop ;
                    496: 
                    497: \ interpreter                                  30apr92py
                    498: 
                    499: \ not the most efficient implementations of interpreter and compiler
1.33      jwilke    500: : interpreter ( c-addr u -- ) 
1.1       pazsan    501:     2dup find-name dup
                    502:     if
                    503:        nip nip name>int execute
                    504:     else
                    505:        drop
                    506:        2dup 2>r snumber?
                    507:        IF
                    508:            2rdrop
                    509:        ELSE
                    510:            2r> interpreter-notfound
                    511:        THEN
                    512:     then ;
                    513: 
                    514: ' interpreter  IS  parser
                    515: 
                    516: \ \ Query Evaluate                                     07apr93py
                    517: 
                    518: has? file 0= [IF]
1.12      pazsan    519: : sourceline# ( -- n )  1 ;
1.61      pazsan    520: [ELSE]
1.58      pazsan    521: Variable #fill-bytes
                    522: \G number of bytes read via (read-line) by the last refill
1.61      pazsan    523: [THEN]
1.58      pazsan    524: 
1.1       pazsan    525: : refill ( -- flag ) \ core-ext,block-ext,file-ext
1.29      crook     526:     \G Attempt to fill the input buffer from the input source.  When
                    527:     \G the input source is the user input device, attempt to receive
                    528:     \G input into the terminal input device. If successful, make the
                    529:     \G result the input buffer, set @code{>IN} to 0 and return true;
                    530:     \G otherwise return false. When the input source is a block, add 1
                    531:     \G to the value of @code{BLK} to make the next block the input
                    532:     \G source and current input buffer, and set @code{>IN} to 0;
                    533:     \G return true if the new value of @code{BLK} is a valid block
                    534:     \G number, false otherwise. When the input source is a text file,
                    535:     \G attempt to read the next line from the file. If successful,
                    536:     \G make the result the current input buffer, set @code{>IN} to 0
                    537:     \G and return true; otherwise, return false.  A successful result
                    538:     \G includes receipt of a line containing 0 characters.
1.12      pazsan    539:     [ has? file [IF] ]
                    540:        blk @  IF  1 blk +!  true  0 >in !  EXIT  THEN
                    541:        [ [THEN] ]
                    542:     tib /line
                    543:     [ has? file [IF] ]
                    544:        loadfile @ ?dup
1.59      pazsan    545:        IF    (read-line) throw #fill-bytes !
1.12      pazsan    546:        ELSE
                    547:            [ [THEN] ]
                    548:        sourceline# 0< IF 2drop false EXIT THEN
                    549:        accept true
                    550:        [ has? file [IF] ]
                    551:        THEN
                    552:        1 loadline +!
                    553:        [ [THEN] ]
                    554:     swap #tib ! 0 >in ! ;
1.1       pazsan    555: 
                    556: : query   ( -- ) \ core-ext
1.29      crook     557:     \G Make the user input device the input source. Receive input into
                    558:     \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
                    559:     \G superceeded by @code{accept}.
1.12      pazsan    560:     [ has? file [IF] ]
                    561:        blk off loadfile off
                    562:        [ [THEN] ]
1.1       pazsan    563:     tib /line accept #tib ! 0 >in ! ;
                    564: 
                    565: \ save-mem extend-mem
                    566: 
                    567: has? os [IF]
                    568: : save-mem     ( addr1 u -- addr2 u ) \ gforth
                    569:     \g copy a memory block into a newly allocated region in the heap
                    570:     swap >r
                    571:     dup allocate throw
                    572:     swap 2dup r> -rot move ;
                    573: 
                    574: : extend-mem   ( addr1 u1 u -- addr addr2 u2 )
                    575:     \ extend memory block allocated from the heap by u aus
                    576:     \ the (possibly reallocated piece is addr2 u2, the extension is at addr
                    577:     over >r + dup >r resize throw
                    578:     r> over r> + -rot ;
                    579: [THEN]
                    580: 
                    581: \ EVALUATE                                              17may93jaw
                    582: 
                    583: has? file 0= [IF]
                    584: : push-file  ( -- )  r>
1.12      pazsan    585:   tibstack @ >r  >tib @ >r  #tib @ >r
1.1       pazsan    586:   >tib @ tibstack @ = IF  r@ tibstack +!  THEN
                    587:   tibstack @ >tib ! >in @ >r  >r ;
                    588: 
                    589: : pop-file   ( throw-code -- throw-code )
                    590:   r>
1.12      pazsan    591:   r> >in !  r> #tib !  r> >tib !  r> tibstack !  >r ;
1.1       pazsan    592: [THEN]
                    593: 
1.29      crook     594: : evaluate ( c-addr u -- ) \ core,block
1.40      crook     595:     \G Save the current input source specification. Store @code{-1} in
                    596:     \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
                    597:     \G @code{0} and make the string @i{c-addr u} the input source
                    598:     \G and input buffer. Interpret. When the parse area is empty,
                    599:     \G restore the input source specification.
1.56      anton     600:     loadfilename# @ >r
1.60      anton     601:     1 loadfilename# ! \ "*evaluated string*"
1.40      crook     602:     push-file #tib ! >tib !
1.29      crook     603:     >in off
                    604:     [ has? file [IF] ]
                    605:        blk off loadfile off -1 loadline !
                    606:        [ [THEN] ]
                    607:     ['] interpret catch
1.56      anton     608:     pop-file
                    609:     r> loadfilename# !
                    610:     throw ;
1.1       pazsan    611: 
                    612: \ \ Quit                                               13feb93py
                    613: 
                    614: Defer 'quit
                    615: 
                    616: Defer .status
                    617: 
                    618: : prompt        state @ IF ."  compiled" EXIT THEN ."  ok" ;
                    619: 
                    620: : (Query)  ( -- )
1.12      pazsan    621:     [ has? file [IF] ]
                    622:        loadfile off  blk off loadline off
                    623:        [ [THEN] ]
                    624:     refill drop ;
1.1       pazsan    625: 
1.39      anton     626: : (quit) ( -- )
                    627:     \ exits only through THROW etc.
1.42      anton     628: \    sp0 @ cell - handler @ &12 + ! \ !! kludge: fix the stack pointer
1.39      anton     629:     \ stored in the system's CATCH frame, so the stack depth will be 0
                    630:     \ after the next THROW it catches (it may be off due to BOUNCEs or
                    631:     \ because process-args left something on the stack)
                    632:     BEGIN
                    633:        .status cr (query) interpret prompt
                    634:     AGAIN ;
1.1       pazsan    635: 
                    636: ' (quit) IS 'quit
                    637: 
                    638: \ \ DOERROR (DOERROR)                                  13jun93jaw
                    639: 
                    640: 8 Constant max-errors
                    641: Variable error-stack  0 error-stack !
                    642: max-errors 6 * cells allot
                    643: \ format of one cell:
                    644: \ source ( addr u )
                    645: \ >in
                    646: \ line-number
                    647: \ Loadfilename ( addr u )
                    648: 
                    649: : dec. ( n -- ) \ gforth
1.40      crook     650:     \G Display @i{n} as a signed decimal number, followed by a space.
                    651:     \ !! not used...
1.1       pazsan    652:     base @ decimal swap . base ! ;
                    653: 
1.23      jwilke    654: : dec.r ( u -- ) \ gforth
1.40      crook     655:     \G Display @i{u} as a unsigned decimal number
1.23      jwilke    656:     base @ decimal swap 0 .r base ! ;
                    657: 
1.1       pazsan    658: : hex. ( u -- ) \ gforth
1.40      crook     659:     \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
1.17      crook     660:     \G followed by a space.
1.40      crook     661:     \ !! not used...
1.33      jwilke    662:     [char] $ emit base @ swap hex u. base ! ;
1.1       pazsan    663: 
                    664: : typewhite ( addr u -- ) \ gforth
1.40      crook     665:     \G Like type, but white space is printed instead of the characters.
1.1       pazsan    666:     bounds ?do
                    667:        i c@ #tab = if \ check for tab
                    668:            #tab
                    669:        else
                    670:            bl
                    671:        then
                    672:        emit
                    673:     loop ;
                    674: 
                    675: DEFER DOERROR
1.33      jwilke    676: 
                    677: has? backtrace [IF]
1.15      anton     678: Defer dobacktrace ( -- )
                    679: ' noop IS dobacktrace
1.33      jwilke    680: [THEN]
1.1       pazsan    681: 
1.23      jwilke    682: : .error-string ( throw-code -- )
                    683:   dup -2 = 
                    684:   IF   "error @ ?dup IF count type  THEN drop
                    685:   ELSE .error
                    686:   THEN ;
                    687: 
                    688: : .error-frame ( throwcode addr1 u1 n1 n2 addr2 u2 -- throwcode )
                    689: \ addr2 u2:    filename of included file
                    690: \ n2:          line number
                    691: \ n1:          error position in input line
                    692: \ addr1 u1:    input line
1.1       pazsan    693:   cr error-stack @
                    694:   IF
                    695:      ." in file included from "
1.23      jwilke    696:      type ." :" dec.r  drop 2drop
1.1       pazsan    697:   ELSE
1.57      anton     698:       type ." :" dup >r dec.r ." : " 3 pick .error-string
                    699:       r> IF \ if line# non-zero, there is a line
                    700:          cr dup 2over type cr drop
                    701:          nip -trailing 1- ( line-start index2 )
                    702:          0 >r  BEGIN
                    703:              2dup + c@ bl >  WHILE
                    704:              r> 1+ >r  1- dup 0<  UNTIL  THEN  1+
                    705:          ( line-start index1 )
                    706:          typewhite
                    707:          r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
                    708:              [char] ^ emit
                    709:          loop
                    710:       ELSE
                    711:          2drop drop
                    712:       THEN
1.23      jwilke    713:   THEN ;
1.1       pazsan    714: 
                    715: : (DoError) ( throw-code -- )
                    716:   [ has? os [IF] ]
1.8       pazsan    717:       >stderr
1.1       pazsan    718:   [ [THEN] ] 
1.57      anton     719:   source >in @ sourceline# sourcefilename .error-frame
1.1       pazsan    720:   error-stack @ 0 ?DO
                    721:     -1 error-stack +!
                    722:     error-stack dup @ 6 * cells + cell+
                    723:     6 cells bounds DO
                    724:       I @
                    725:     cell +LOOP
                    726:     .error-frame
                    727:   LOOP
1.33      jwilke    728:   drop 
                    729: [ has? backtrace [IF] ]
                    730:   dobacktrace
                    731: [ [THEN] ]
1.8       pazsan    732:   normal-dp dpp ! ;
1.1       pazsan    733: 
                    734: ' (DoError) IS DoError
                    735: 
                    736: : quit ( ?? -- ?? ) \ core
1.27      crook     737:     \G Empty the return stack, make the user input device
                    738:     \G the input source, enter interpret state and start
                    739:     \G the text interpreter.
1.5       anton     740:     rp0 @ rp! handler off clear-tibstack >tib @ >r
1.1       pazsan    741:     BEGIN
                    742:        [ has? compiler [IF] ]
                    743:        postpone [
                    744:        [ [THEN] ]
                    745:        ['] 'quit CATCH dup
                    746:     WHILE
1.22      anton     747:        <# \ reset hold area, or we may get another error
1.1       pazsan    748:        DoError r@ >tib ! r@ tibstack !
                    749:     REPEAT
                    750:     drop r> >tib ! ;
                    751: 
                    752: \ \ Cold Boot                                          13feb93py
                    753: 
                    754: : (bootmessage)
                    755:     ." GForth " version-string type 
1.52      pazsan    756:     ." , Copyright (C) 1995-2000 Free Software Foundation, Inc." cr
1.1       pazsan    757:     ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
                    758: [ has? os [IF] ]
                    759:      cr ." Type `bye' to exit"
                    760: [ [THEN] ] ;
                    761: 
                    762: defer bootmessage
                    763: defer process-args
                    764: 
                    765: ' (bootmessage) IS bootmessage
                    766: 
1.10      anton     767: Defer 'cold ( -- ) \ gforth  tick-cold
1.1       pazsan    768: \ hook (deferred word) for things to do right before interpreting the
                    769: \ command-line arguments
                    770: ' noop IS 'cold
                    771: 
                    772: 
                    773: Variable init8
                    774: 
                    775: : cold ( -- ) \ gforth
1.44      anton     776: [ has? backtrace [IF] ]
                    777:     rp@ backtrace-rp0 !
                    778: [ [THEN] ]
1.1       pazsan    779: [ has? file [IF] ]
                    780:     pathstring 2@ fpath only-path 
                    781:     init-included-files
                    782: [ [THEN] ]
                    783:     'cold
                    784:     init8 chainperform
                    785: [ has? file [IF] ]
1.62      anton     786:     loadfilename# off
1.8       pazsan    787:     process-args
1.12      pazsan    788:     loadline off
1.1       pazsan    789: [ [THEN] ]
                    790:     bootmessage
1.12      pazsan    791:     quit ;
1.1       pazsan    792: 
1.5       anton     793: : clear-tibstack ( -- )
                    794: [ has? glocals [IF] ]
                    795:     lp@ forthstart 7 cells + @ - 
                    796: [ [ELSE] ]
                    797:     [ has? os [IF] ]
1.8       pazsan    798:     r0 @ forthstart 6 cells + @ -
1.5       anton     799:     [ [ELSE] ]
1.16      pazsan    800:     sp@ $10 cells +
1.5       anton     801:     [ [THEN] ]
                    802: [ [THEN] ]
                    803:     dup >tib ! tibstack ! #tib off >in off ;
                    804: 
1.1       pazsan    805: : boot ( path **argv argc -- )
                    806:     main-task up!
                    807: [ has? os [IF] ]
                    808:     stdout TO outfile-id
1.7       pazsan    809:     stdin  TO infile-id
1.1       pazsan    810: \ !! [ [THEN] ]
                    811: \ !! [ has? file [IF] ]
                    812:     argc ! argv ! pathstring 2!
                    813: [ [THEN] ]
                    814:     sp@ sp0 !
1.5       anton     815:     clear-tibstack
1.1       pazsan    816:     rp@ rp0 !
                    817: [ has? floating [IF] ]
                    818:     fp@ fp0 !
                    819: [ [THEN] ]
1.46      anton     820:     handler off
1.8       pazsan    821:     ['] cold catch DoError cr
1.1       pazsan    822: [ has? os [IF] ]
1.35      anton     823:     1 (bye) \ !! determin exit code from throw code?
1.1       pazsan    824: [ [THEN] ]
                    825: ;
                    826: 
                    827: has? os [IF]
                    828: : bye ( -- ) \ tools-ext
                    829: [ has? file [IF] ]
                    830:     script? 0= IF  cr  THEN
                    831: [ [ELSE] ]
                    832:     cr
                    833: [ [THEN] ]
                    834:     0 (bye) ;
                    835: [THEN]
                    836: 
                    837: \ **argv may be scanned by the C starter to get some important
                    838: \ information, as -display and -geometry for an X client FORTH
                    839: \ or space and stackspace overrides
                    840: 
                    841: \ 0 arg contains, however, the name of the program.
                    842: 

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