File:  [gforth] / gforth / kernel / int.fs
Revision 1.160: download - view: text, annotated - select for diffs
Mon Dec 31 17:34:59 2007 UTC (16 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years

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

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