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

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

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