File:  [gforth] / gforth / extend.fs
Revision 1.40: download - view: text, annotated - select for diffs
Wed Aug 9 20:04:05 2000 UTC (19 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
documentation changes
fixed bug in f~

    1: \ EXTEND.FS    CORE-EXT Word not fully tested!         12may93jaw
    2: 
    3: \ Copyright (C) 1995,1998 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., 675 Mass Ave, Cambridge, MA 02139, USA.
   20: 
   21: 
   22: \ May be cross-compiled
   23: 
   24: decimal
   25: 
   26: \ .(                                                    12may93jaw
   27: 
   28: : .(   ( "ccc<paren>" -- ) \ core-ext dot-paren
   29:   \G Parse a string @i{ccc} delimited by a @code{)} (right
   30:   \G parenthesis). Display the string. This is often used to display
   31:   \G progress information during compilation; see examples below.
   32:   [char] ) parse type ; immediate
   33: 
   34: \ VALUE 2>R 2R> 2R@                                     17may93jaw
   35: 
   36: \ !! 2value
   37: 
   38: : 2Literal ( compilation w1 w2 -- ; run-time  -- w1 w2 ) \ double two-literal
   39:     \G Compile appropriate code such that, at run-time, cell pair @i{w1, w2} are
   40:     \G placed on the stack. Interpretation semantics are undefined.
   41:     swap postpone Literal  postpone Literal ; immediate restrict
   42: 
   43: ' drop alias d>s ( d -- n ) \ double		d_to_s
   44: 
   45: : m*/ ( d1 n2 u3 -- dquot ) \ double m-star-slash
   46:     \G dquot=(d1*n2)/u3, with the intermediate result being triple-precision.
   47:     \G In ANS Forth u3 can only be a positive signed number.
   48:     >r s>d >r abs -rot
   49:     s>d r> xor r> swap >r >r dabs rot tuck um* 2swap um*
   50:     swap >r 0 d+ r> -rot r@ um/mod -rot r> um/mod nip swap
   51:     r> IF dnegate THEN ;
   52: 
   53: \ CASE OF ENDOF ENDCASE                                 17may93jaw
   54: 
   55: \ just as described in dpANS5
   56: 
   57: 0 CONSTANT case ( compilation  -- case-sys ; run-time  -- ) \ core-ext
   58:     immediate
   59: 
   60: : of ( compilation  -- of-sys ; run-time x1 x2 -- |x1 ) \ core-ext
   61:     \ !! the implementation does not match the stack effect
   62:     1+ >r
   63:     postpone over postpone = postpone if postpone drop
   64:     r> ; immediate
   65: 
   66: : endof ( compilation case-sys1 of-sys -- case-sys2 ; run-time  -- ) \ core-ext end-of
   67:     >r postpone else r> ; immediate
   68: 
   69: : endcase ( compilation case-sys -- ; run-time x -- ) \ core-ext end-case
   70:     postpone drop
   71:     0 ?do postpone then loop ; immediate
   72: 
   73: \ C"                                                    17may93jaw
   74: 
   75: : (c")     "lit ;
   76: 
   77: : CLiteral
   78:     postpone (c") here over char+ allot  place align ; immediate restrict
   79: 
   80: : C" ( compilation "ccc<quote>" -- ; run-time  -- c-addr ) \ core-ext c-quote
   81:     \G Compilation: parse a string @i{ccc} delimited by a @code{"}
   82:     \G (double quote). At run-time, return @i{c-addr} which
   83:     \G specifies the counted string @i{ccc}.  Interpretation
   84:     \G semantics are undefined.
   85:     [char] " parse postpone CLiteral ; immediate restrict
   86: 
   87: \ [COMPILE]                                             17may93jaw
   88: 
   89: : [compile] ( compilation "name" -- ; run-time ? -- ? ) \ core-ext bracket-compile
   90:     comp' drop
   91:     dup [ comp' exit drop ] literal = if
   92: 	execute \ EXIT has default compilation semantics, perform them
   93:     else
   94: 	compile,
   95:     then ; immediate
   96: 
   97: \ CONVERT                                               17may93jaw
   98: 
   99: : convert ( ud1 c-addr1 -- ud2 c-addr2 ) \ core-ext
  100:     \G OBSOLESCENT: superseded by @code{>number}.
  101:     char+ true >number drop ;
  102: 
  103: \ ERASE                                                 17may93jaw
  104: 
  105: : erase ( addr u -- ) \ core-ext
  106:     \G Clear all bits in @i{u} aus starting at @i{addr}.
  107:     \ !! dependence on "1 chars 1 ="
  108:     ( 0 1 chars um/mod nip )  0 fill ;
  109: : blank ( c-addr u -- ) \ string
  110:     \G Store the space character into @i{u} chars starting at @i{c-addr}.
  111:     bl fill ;
  112: 
  113: \ SEARCH                                                02sep94py
  114: 
  115: : search ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 flag ) \ string
  116:     \G Search the string specified by @i{c-addr1, u1} for the string
  117:     \G specified by @i{c-addr2, u2}. If @i{flag} is true: match was found
  118:     \G at @i{c-addr3} with @i{u3} characters remaining. If @i{flag} is false:
  119:     \G no match was found; @i{c-addr3, u3} are equal to @i{c-addr1, u1}.
  120:     \ not very efficient; but if we want efficiency, we'll do it as primitive
  121:     2>r 2dup
  122:     begin
  123: 	dup r@ >=
  124:     while
  125: 	over 2r@ swap -text 0= if
  126: 	    2swap 2drop 2r> 2drop true exit
  127: 	endif
  128: 	1 /string
  129:     repeat
  130:     2drop 2r> 2drop false ;
  131: 
  132: \ SOURCE-ID SAVE-INPUT RESTORE-INPUT                    11jun93jaw
  133: 
  134: : source-id ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d
  135:     \G Return 0 (the input source is the user input device), -1 (the
  136:     \G input source is a string being processed by @code{evaluate}) or
  137:     \G a @i{fileid} (the input source is the file specified by
  138:     \G @i{fileid}).
  139:     loadfile @ dup 0= IF  drop sourceline# 0 min  THEN ;
  140: 
  141: : save-input ( -- xn .. x1 n ) \ core-ext
  142:     \G The @i{n} entries @i{xn - x1} describe the current state of the
  143:     \G input source specification, in some platform-dependent way that can
  144:     \G be used by @code{restore-input}.
  145:     >in @
  146:     loadfile @
  147:     if
  148: 	loadfile @ file-position throw #TIB @ 1+ 0 d- \ !! bug for CRLF and EOF
  149:     else
  150: 	blk @
  151: 	linestart @
  152:     then
  153:     sourceline#
  154:     >tib @
  155:     source-id
  156:     6 ;
  157: 
  158: : restore-input ( xn .. x1 n -- flag ) \ core-ext
  159:     \G Attempt to restore the input source specification to the state
  160:     \G described by the @i{n} entries @i{xn - x1}. @i{flag} is
  161:     \G true if the restore fails.  In Gforth it fails pretty often
  162:     \G (and sometimes with a @code{throw}).
  163:     6 <> -12 and throw
  164:     source-id <> -12 and throw
  165:     >tib !
  166:     >r ( line# )
  167:     loadfile @ 0<>
  168:     if
  169: 	loadfile @ reposition-file throw
  170: 	refill 0= -36 and throw \ should never throw
  171:     else
  172: 	linestart !
  173: 	blk !
  174: 	sourceline# r@ <> blk @ 0= and loadfile @ 0= and
  175: 	if
  176: 	    drop rdrop true EXIT
  177: 	then
  178:     then
  179:     r> loadline !
  180:     >in !
  181:     false ;
  182: 
  183: \ This things we don't need, but for being complete... jaw
  184: 
  185: \ EXPECT SPAN                                           17may93jaw
  186: 
  187: variable span ( -- c-addr ) \ core-ext
  188: \G @code{Variable} -- @i{c-addr} is the address of a cell that stores the
  189: \G length of the last string received by @code{expect}. OBSOLESCENT.
  190: 
  191: : expect ( c-addr +n -- ) \ core-ext
  192:     \G Receive a string of at most @i{+n} characters, and store it
  193:     \G in memory starting at @i{c-addr}. The string is
  194:     \G displayed. Input terminates when the <return> key is pressed or
  195:     \G @i{+n} characters have been received. The normal Gforth line
  196:     \G editing capabilites are available. The length of the string is
  197:     \G stored in @code{span}; it does not include the <return>
  198:     \G character. OBSOLESCENT: superceeded by @code{accept}.
  199:     0 rot over
  200:     BEGIN ( maxlen span c-addr pos1 )
  201: 	key decode ( maxlen span c-addr pos2 flag )
  202: 	>r 2over = r> or
  203:     UNTIL
  204:     2 pick swap /string type
  205:     nip span ! ;
  206: 
  207: \ marker                                               18dec94py
  208: 
  209: \ Marker creates a mark that is removed (including everything 
  210: \ defined afterwards) when executing the mark.
  211: 
  212: : included-files-mark ( -- u )
  213:     included-files 2@ nip
  214:     blk @ 0=
  215:     if \ not input from blocks
  216: 	source-id 1 -1 within
  217: 	if \ input from file
  218: 	    1- \ do not include the last file (hopefully this is the
  219: 	       \ currently included file)
  220: 	then
  221:     then ;  
  222: 
  223: \ hmm, most of the saving appears to be pretty unnecessary: we could
  224: \ derive the wordlists and the words that have to be kept from the
  225: \ saved value of dp value. - anton
  226: 
  227: : marker, ( -- mark )
  228:     here
  229:     included-files-mark ,
  230:     dup A, \ here
  231:     voclink @ A, \ vocabulary list start
  232:     \ for all wordlists, remember wordlist-id (the linked list)
  233:     voclink
  234:     BEGIN
  235: 	@ dup
  236:     WHILE
  237: 	dup 0 wordlist-link - wordlist-id @ A,
  238:     REPEAT
  239:     drop
  240:     \ remember udp
  241:     udp @ , ;
  242: 
  243: : marker! ( mark -- )
  244:     \ reset included files count; resize will happen on next add-included-file
  245:     included-files 2@ drop over @ included-files 2! cell+
  246:     \ rest of marker!
  247:     dup @ swap cell+ ( here rest-of-marker )
  248:     dup @ voclink ! cell+
  249:     \ restore wordlists to former words
  250:     voclink
  251:     BEGIN
  252: 	@ dup 
  253:     WHILE
  254: 	over @ over 0 wordlist-link - wordlist-id !
  255: 	swap cell+ swap
  256:     REPEAT
  257:     drop
  258:     \ rehash wordlists to remove forgotten words
  259:     \ why don't we do this in a single step? - anton
  260:     voclink
  261:     BEGIN
  262: 	@ dup
  263:     WHILE
  264: 	dup 0 wordlist-link - rehash
  265:     REPEAT
  266:     drop
  267:     \ restore udp and dp
  268:     @ udp !  dp !
  269:     \ clean up vocabulary stack
  270:     0 vp @ 0
  271:     ?DO
  272: 	vp cell+ I cells + @ dup here >
  273: 	IF  drop  ELSE  swap 1+  THEN
  274:     LOOP
  275:     dup 0= or set-order \ -1 set-order if order is empty
  276:     get-current here > IF
  277: 	forth-wordlist set-current
  278:     THEN ;
  279: 
  280: : marker ( "<spaces> name" -- ) \ core-ext
  281:     \G Create a definition, @i{name} (called a @i{mark}) whose
  282:     \G execution semantics are to remove itself and everything 
  283:     \G defined after it.
  284:     marker, Create A,
  285: DOES> ( -- )
  286:     @ marker! ;
  287: 

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