File:  [gforth] / gforth / kernel / int.fs
Revision 1.169: download - view: text, annotated - select for diffs
Sat Sep 5 17:38:38 2009 UTC (14 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
started on prelude concept
  preludes can be built, but are not yet performed

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

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