File:  [gforth] / gforth / kernel / int.fs
Revision 1.164: download - view: text, annotated - select for diffs
Thu Oct 23 09:32:43 2008 UTC (15 years, 6 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed 4stack image generation with a 64 bit Gforth engine (tested only
on little endian machines, so far).

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

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