File:  [gforth] / gforth / kernel / int.fs
Revision 1.168: download - view: text, annotated - select for diffs
Sun Mar 29 01:13:14 2009 UTC (15 years ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed gforthmi.sh to run without Cygwin
Removed unnecessary G/T switching in int.fs

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

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