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

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

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