File:  [gforth] / gforth / kernel / int.fs
Revision 1.138: download - view: text, annotated - select for diffs
Sat Feb 25 18:28:12 2006 UTC (18 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
R8C Forth works!

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

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