File:  [gforth] / gforth / extend.fs
Revision 1.59: download - view: text, annotated - select for diffs
Thu Mar 8 13:55:49 2007 UTC (12 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
M*/ now also works on symmetric-division systems

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

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