File:  [gforth] / gforth / kernel / int.fs
Revision 1.157: download - view: text, annotated - select for diffs
Sun Mar 18 22:35:52 2007 UTC (17 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed some small problems further along to compile on NXT

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

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