File:  [gforth] / gforth / kernel / int.fs
Revision 1.179: download - view: text, annotated - select for diffs
Thu Oct 6 20:21:28 2011 UTC (12 years, 6 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
removed comp/int-notfound1 when recognizers are available

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

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