File:  [gforth] / gforth / kernel / int.fs
Revision 1.108: download - view: text, annotated - select for diffs
Wed Dec 8 11:56:30 2004 UTC (19 years, 4 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added 0x and 0X as hex prefixes

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

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