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

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

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