File:  [gforth] / gforth / Attic / primitives
Revision 1.9: download - view: text, annotated - select for diffs
Fri Jun 17 12:35:14 1994 UTC (29 years, 10 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Integrated locals (in particular automatic scoping) into the system.

    1: \ Copyright 1992 by the ANSI figForth Development Group
    2: \ 
    3: \ WARNING: This file is processed by m4. Make sure your identifiers
    4: \ don't collide with m4's (e.g. by undefining them).
    5: \ 
    6: \ This file contains instructions in the following format:
    7: \ 
    8: \ forth name	stack effect	category	[pronounciation]
    9: \ [""glossary entry""]
   10: \ C code
   11: \ [:
   12: \ Forth code]
   13: \ 
   14: \ The pronounciataion is also used for forming C names.
   15: \ 
   16: \ These informations are automagically translated into C-code for the
   17: \ interpreter and into some other files. The forth name of a word is
   18: \ automatically turned into upper case. I hope that your C compiler has
   19: \ decent optimization, otherwise the automatically generated code will
   20: \ be somewhat slow. The Forth version of the code is included for manual
   21: \ compilers, so they will need to compile only the important words.
   22: \ 
   23: \ Note that stack pointer adjustment is performed according to stack
   24: \ effect by automatically generated code and NEXT is automatically
   25: \ appended to the C code. Also, you can use the names in the stack
   26: \ effect in the C code. Stack access is automatic. One exception: if
   27: \ your code does not fall through, the results are not stored into the
   28: \ stack. Use different names on both sides of the '--', if you change a
   29: \ value (some stores to the stack are optimized away).
   30: \ 
   31: \ The stack variables have the following types:
   32: \ name matches	type
   33: \ f.*		Bool
   34: \ c.*		Char
   35: \ [nw].*		Cell
   36: \ u.*		UCell
   37: \ d.*		DCell
   38: \ ud.*		UDCell
   39: \ r.*		Float
   40: \ a_.*		Cell *
   41: \ c_.*		Char *
   42: \ f_.*		Float *
   43: \ df_.*		DFloat *
   44: \ sf_.*		SFloat *
   45: \ xt.*		XT
   46: \ wid.*		WID
   47: \ f83name.*	F83Name *
   48: \ 
   49: \ In addition the following names can be used:
   50: \ ip	the instruction pointer
   51: \ sp	the data stack pointer
   52: \ rp	the parameter stack pointer
   53: \ NEXT	executes NEXT
   54: \ cfa	
   55: \ NEXT1	executes NEXT1
   56: \ FLAG(x)	makes a Forth flag from a C flag
   57: \ 
   58: \ Percentages in comments are from Koopmans book: average/maximum use
   59: \ (taken from four, not very representattive benchmarks)
   60: \ 
   61: \ To do:
   62: \ make sensible error returns for file words
   63: \ 
   64: \ throw execute, cfa and NEXT1 out?
   65: \ macroize *ip, ip++, *ip++ (pipelining)?
   66: 
   67: \ these m4 macros would collide with identifiers
   68: undefine(`index')
   69: undefine(`shift')
   70: 
   71: noop	--		fig
   72: ;
   73: 
   74: lit	-- w		fig
   75: w = (Cell)*ip++;
   76: 
   77: execute		xt --		core,fig
   78: cfa = xt;
   79: IF_TOS(TOS = sp[0]);
   80: NEXT1;
   81: 
   82: branch-lp+!#	--	new	branch_lp_plus_store_number
   83: /* this will probably not be used */
   84: branch_adjust_lp:
   85: lp += (int)(ip[1]);
   86: goto branch;
   87: 
   88: branch	--		fig
   89: branch:
   90: ip = (Xt *)(((int)ip)+(int)*ip);
   91: 
   92: \ condbranch(forthname,restline,code)
   93: \ this is non-syntactical: code must open a brace that is close by the macro
   94: define(condbranch,
   95: $1	$2
   96: $3    goto branch;
   97: }
   98: else
   99:     ip++;
  100: 
  101: $1-lp+!#	$2_lp_plus_store_number
  102: $3    goto branch_adjust_lp;
  103: }
  104: else
  105:     ip+=2;
  106: 
  107: )
  108: 
  109: condbranch(?branch,f --		f83	question_branch,
  110: if (f==0) {
  111:     IF_TOS(TOS = sp[0]);
  112: )
  113: 
  114: condbranch((next),--		cmFORTH	paren_next,
  115: if ((*rp)--) {
  116: )
  117: 
  118: condbranch((loop),--		fig	paren_loop,
  119: int index = *rp+1;
  120: int limit = rp[1];
  121: if (index != limit) {
  122:     *rp = index;
  123: )
  124: 
  125: condbranch((+loop),n --		fig	paren_plus_loop,
  126: /* !! check this thoroughly */
  127: int index = *rp;
  128: int olddiff = index-rp[1];
  129: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
  130: /* dependent upon two's complement arithmetic */
  131: if ((olddiff^(olddiff+n))>=0   /* the limit is not crossed */
  132:     || (olddiff^n)>=0          /* it is a wrap-around effect */) {
  133:     *rp = index+n;
  134:     IF_TOS(TOS = sp[0]);
  135: )
  136: 
  137: condbranch((s+loop),n --		new	paren_symmetric_plus_loop,
  138: ""The run-time procedure compiled by S+LOOP. It loops until the index
  139: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
  140: version of (+LOOP).""
  141: /* !! check this thoroughly */
  142: int oldindex = *rp;
  143: int diff = oldindex-rp[1];
  144: int newdiff = diff+n;
  145: if (n<0) {
  146:     diff = -diff;
  147:     newdiff = - newdiff;
  148: }
  149: if (diff>=0 || newdiff<0) {
  150:     *rp = oldindex+n;
  151:     IF_TOS(TOS = sp[0]);
  152: )
  153: 
  154: unloop		--	core
  155: rp += 2;
  156: 
  157: (for)	ncount --		cmFORTH		paren_for
  158: /* or (for) = >r -- collides with unloop! */
  159: *--rp = 0;
  160: *--rp = ncount;
  161: 
  162: (do)	nlimit nstart --		fig		paren_do
  163: /* or do it in high-level? 0.09/0.23% */
  164: *--rp = nlimit;
  165: *--rp = nstart;
  166: :
  167:  swap >r >r ;
  168: 
  169: (?do)	nlimit nstart --	core-ext	paren_question_do
  170: *--rp = nlimit;
  171: *--rp = nstart;
  172: if (nstart == nlimit) {
  173:     IF_TOS(TOS = sp[0]);
  174:     goto branch;
  175:     }
  176: else {
  177:     ip++;
  178: }
  179: 
  180: i	-- n		core,fig
  181: n = *rp;
  182: 
  183: j	-- n		core
  184: n = rp[2];
  185: 
  186: \ digit is high-level: 0/0%
  187: 
  188: emit	c --		fig
  189: putchar(c);
  190: emitcounter++;
  191: 
  192: key	-- n		fig
  193: fflush(stdout);
  194: /* !! noecho */
  195: n = key();
  196: 
  197: key?	-- n		fig	key_q
  198: fflush(stdout);
  199: n = key_query;
  200: 
  201: cr	--		fig
  202: puts("");
  203: 
  204: move	c_from c_to ucount --		core
  205: memmove(c_to,c_from,ucount);
  206: /* make an Ifdef for bsd and others? */
  207: 
  208: cmove	c_from c_to u --	string
  209: while (u-- > 0)
  210:   *c_to++ = *c_from++;
  211: 
  212: cmove>	c_from c_to u --	string	c_move_up
  213: while (u-- > 0)
  214:   c_to[u] = c_from[u];
  215: 
  216: fill	c_addr u c --	core
  217: memset(c_addr,c,u);
  218: 
  219: compare		c_addr1 u1 c_addr2 u2 -- n	string
  220: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
  221: if (n==0)
  222:   n = u1-u2;
  223: if (n<0)
  224:   n = -1;
  225: else if (n>0)
  226:   n = 1;
  227: 
  228: -text		c_addr1 u c_addr2 -- n	new	dash_text
  229: n = memcmp(c_addr1, c_addr2, u);
  230: if (n<0)
  231:   n = -1;
  232: else if (n>0)
  233:   n = 1;
  234: 
  235: capscomp	c_addr1 u c_addr2 -- n	new
  236: Char c1, c2;
  237: for (;; u--, c_addr1++, c_addr2++) {
  238:   if (u == 0) {
  239:     n = 0;
  240:     break;
  241:   }
  242:   c1 = toupper(*c_addr1);
  243:   c2 = toupper(*c_addr2);
  244:   if (c1 != c2) {
  245:     if (c1 < c2)
  246:       n = -1;
  247:     else
  248:       n = 1;
  249:     break;
  250:   }
  251: }
  252: 
  253: -trailing	c_addr u1 -- c_addr u2		string	dash_trailing
  254: u2 = u1;
  255: while (c_addr[u2-1] == ' ')
  256:   u2--;
  257: 
  258: /string		c_addr1 u1 n -- c_addr2 u2	string	slash_string
  259: c_addr2 = c_addr1+n;
  260: u2 = u1-n;
  261: 
  262: +	n1 n2 -- n		core,fig	plus
  263: n = n1+n2;
  264: 
  265: -	n1 n2 -- n		core,fig	minus
  266: n = n1-n2;
  267: 
  268: negate	n1 -- n2		core,fig
  269: /* use minus as alias */
  270: n2 = -n1;
  271: 
  272: 1+	n1 -- n2		core		one_plus
  273: n2 = n1+1;
  274: 
  275: 1-	n1 -- n2		core		one_minus
  276: n2 = n1-1;
  277: 
  278: max	n1 n2 -- n	core
  279: if (n1<n2)
  280:   n = n2;
  281: else
  282:   n = n1;
  283: :
  284:  2dup < if
  285:   swap drop
  286:  else
  287:   drop
  288:  endif ;
  289: 
  290: min	n1 n2 -- n	core
  291: if (n1<n2)
  292:   n = n1;
  293: else
  294:   n = n2;
  295: 
  296: abs	n1 -- n2	core
  297: if (n1<0)
  298:   n2 = -n1;
  299: else
  300:   n2 = n1;
  301: 
  302: *	n1 n2 -- n		core,fig	star
  303: n = n1*n2;
  304: 
  305: /	n1 n2 -- n		core,fig	slash
  306: n = n1/n2;
  307: 
  308: mod	n1 n2 -- n		core
  309: n = n1%n2;
  310: 
  311: /mod	n1 n2 -- n3 n4		core		slash_mod
  312: n4 = n1/n2;
  313: n3 = n1%n2; /* !! is this correct? look into C standard! */
  314: 
  315: 2*	n1 -- n2		core		two_star
  316: n2 = 2*n1;
  317: 
  318: 2/	n1 -- n2		core		two_slash
  319: /* !! is this still correct? */
  320: n2 = n1>>1;
  321: 
  322: fm/mod	d1 n1 -- n2 n3		core		f_m_slash_mod
  323: ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
  324: /* assumes that the processor uses either floored or symmetric division */
  325: n3 = d1/n1;
  326: n2 = d1%n1;
  327: /* note that this 1%-3>0 is optimized by the compiler */
  328: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
  329:   n3--;
  330:   n2+=n1;
  331: }
  332: 
  333: sm/rem	d1 n1 -- n2 n3		core		s_m_slash_rem
  334: ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
  335: /* assumes that the processor uses either floored or symmetric division */
  336: n3 = d1/n1;
  337: n2 = d1%n1;
  338: /* note that this 1%-3<0 is optimized by the compiler */
  339: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
  340:   n3++;
  341:   n2-=n1;
  342: }
  343: 
  344: m*	n1 n2 -- d		core	m_star
  345: d = (DCell)n1 * (DCell)n2;
  346: 
  347: um*	u1 u2 -- ud		core	u_m_star
  348: /* use u* as alias */
  349: ud = (UDCell)u1 * (UDCell)u2;
  350: 
  351: um/mod	ud u1 -- u2 u3		core	u_m_slash_mod
  352: u3 = ud/u1;
  353: u2 = ud%u1;
  354: 
  355: m+	d1 n -- d2		double		m_plus
  356: d2 = d1+n;
  357: 
  358: d+	d1 d2 -- d		double,fig	d_plus
  359: d = d1+d2;
  360: 
  361: d-	d1 d2 -- d		double		d_minus
  362: d = d1-d2;
  363: 
  364: dnegate	d1 -- d2		double
  365: /* use dminus as alias */
  366: d2 = -d1;
  367: 
  368: dmax	d1 d2 -- d	double
  369: if (d1<d2)
  370:   d = d2;
  371: else
  372:   d = d1;
  373: 
  374: dmin	d1 d2 -- d	double
  375: if (d1<d2)
  376:   d = d1;
  377: else
  378:   d = d2;
  379: 
  380: dabs	d1 -- d2	double
  381: if (d1<0)
  382:   d2 = -d1;
  383: else
  384:   d2 = d1;
  385: 
  386: d2*	d1 -- d2		double		d_two_star
  387: d2 = 2*d1;
  388: 
  389: d2/	d1 -- d2		double		d_two_slash
  390: /* !! is this still correct? */
  391: d2 = d1/2;
  392: 
  393: d>s	d -- n			double		d_to_s
  394: /* make this an alias for drop? */
  395: n = d;
  396: 
  397: and	w1 w2 -- w		core,fig
  398: w = w1&w2;
  399: 
  400: or	w1 w2 -- w		core,fig
  401: w = w1|w2;
  402: 
  403: xor	w1 w2 -- w		core,fig
  404: w = w1^w2;
  405: 
  406: invert	w1 -- w2		core
  407: w2 = ~w1;
  408: 
  409: rshift	u1 n -- u2		core
  410:   u2 = u1>>n;
  411: 
  412: lshift	u1 n -- u2		core
  413:   u2 = u1<<n;
  414: 
  415: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
  416: define(comparisons,
  417: $1=	$2 -- f		$6	$3equals
  418: f = FLAG($4==$5);
  419: 
  420: $1<>	$2 -- f		$7	$3different
  421: /* use != as alias ? */
  422: f = FLAG($4!=$5);
  423: 
  424: $1<	$2 -- f		$8	$3less
  425: f = FLAG($4<$5);
  426: 
  427: $1>	$2 -- f		$9	$3greater
  428: f = FLAG($4>$5);
  429: 
  430: $1<=	$2 -- f		new	$3less_or_equal
  431: f = FLAG($4<=$5);
  432: 
  433: $1>=	$2 -- f		new	$3greater_or_equal
  434: f = FLAG($4>=$5);
  435: 
  436: )
  437: 
  438: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
  439: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
  440: comparisons(u, u1 u2, u_, u1, u2, new, new, core, core-ext)
  441: comparisons(d, d1 d2, d_, d1, d2, double, new, double, new)
  442: comparisons(d0, d, d_zero_, d, 0, double, new, double, new)
  443: comparisons(du, ud1 ud2, d_u_, ud1, ud2, new, new, double-ext, new)
  444: 
  445: within	u1 u2 u3 -- f		core-ext
  446: f = FLAG(u1-u2 < u3-u2);
  447: 
  448: sp@	-- a_addr		fig		spat
  449: a_addr = sp;
  450: 
  451: sp!	a_addr --		fig		spstore
  452: sp = a_addr+1;
  453: /* works with and without TOS caching */
  454: 
  455: rp@	-- a_addr		fig		rpat
  456: a_addr = rp;
  457: 
  458: rp!	a_addr --		fig		rpstore
  459: rp = a_addr;
  460: 
  461: fp@	-- f_addr	new	fp_fetch
  462: f_addr = fp;
  463: 
  464: fp!	f_addr --	new	fp_store
  465: fp = f_addr;
  466: 
  467: ;s	--		core	exit
  468: ip = (Xt *)(*rp++);
  469: 
  470: >r	w --		core,fig	to_r
  471: *--rp = w;
  472: 
  473: r>	-- w		core,fig	r_from
  474: w = *rp++;
  475: 
  476: r@	-- w		core,fig	r_fetch
  477: /* use r as alias */
  478: /* make r@ an alias for i */
  479: w = *rp;
  480: 
  481: rdrop	--		fig
  482: rp++;
  483: 
  484: i'	-- w		fig		i_tick
  485: w=rp[1];
  486: 
  487: over	w1 w2 -- w1 w2 w1		core,fig
  488: 
  489: drop	w --		core,fig
  490: 
  491: swap	w1 w2 -- w2 w1		core,fig
  492: 
  493: dup	w -- w w		core,fig
  494: 
  495: rot	w1 w2 w3 -- w2 w3 w1	core	rote
  496: 
  497: -rot	w1 w2 w3 -- w3 w1 w2	fig	not_rote
  498: 
  499: nip	w1 w2 -- w2		core-ext
  500: 
  501: tuck	w1 w2 -- w2 w1 w2	core-ext
  502: 
  503: ?dup	w -- w			core	question_dupe
  504: if (w!=0) {
  505:   IF_TOS(*sp-- = w;)
  506: #ifndef USE_TOS
  507:   *--sp = w;
  508: #endif
  509: }
  510: 
  511: pick	u -- w			core-ext
  512: w = sp[u+1];
  513: 
  514: 2drop	w1 w2 --		core	two_drop
  515: 
  516: 2dup	w1 w2 -- w1 w2 w1 w2	core	two_dupe
  517: 
  518: 2over	w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2	core	two_over
  519: 
  520: 2swap	w1 w2 w3 w4 -- w3 w4 w1 w2	core	two_swap
  521: 
  522: 2rot	w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2	double	two_rote
  523: 
  524: \ toggle is high-level: 0.11/0.42%
  525: 
  526: @	a_addr -- w		fig	fetch
  527: w = *a_addr;
  528: 
  529: !	w a_addr --		core,fig	store
  530: *a_addr = w;
  531: 
  532: +!	n a_addr --		core,fig	plus_store
  533: *a_addr += n;
  534: 
  535: c@	c_addr -- c		fig	cfetch
  536: c = *c_addr;
  537: 
  538: c!	c c_addr --		fig	cstore
  539: *c_addr = c;
  540: 
  541: 2!	w1 w2 a_addr --		core	two_store
  542: a_addr[0] = w2;
  543: a_addr[1] = w1;
  544: 
  545: 2@	a_addr -- w1 w2		core	two_fetch
  546: w2 = a_addr[0];
  547: w1 = a_addr[1];
  548: 
  549: d!	d a_addr --		double	d_store
  550: /* !! alignment problems on some machines */
  551: *(DCell *)a_addr = d;
  552: 
  553: d@	a_addr -- d		double	d_fetch
  554: d = *(DCell *)a_addr;
  555: 
  556: cell+	a_addr1 -- a_addr2	core	cell_plus
  557: a_addr2 = a_addr1+1;
  558: 
  559: cells	n1 -- n2		core
  560: n2 = n1 * sizeof(Cell);
  561: 
  562: char+	c_addr1 -- c_addr2	core	care_plus
  563: c_addr2 = c_addr1+1;
  564: 
  565: chars	n1 -- n2		core	cares
  566: n2 = n1 * sizeof(Char);
  567: 
  568: count	c_addr1 -- c_addr2 u	core
  569: u = *c_addr1;
  570: c_addr2 = c_addr1+1;
  571: 
  572: (bye)	n --	toolkit-ext	paren_bye
  573: deprep_terminal();
  574: exit(n);
  575: 
  576: system	c_addr u -- n	own
  577: char pname[u+1];
  578: cstr(pname,c_addr,u);
  579: n=system(pname);
  580: 
  581: popen	c_addr u n -- wfileid	own
  582: char pname[u+1];
  583: static char* mode[2]={"r","w"};
  584: cstr(pname,c_addr,u);
  585: wfileid=(Cell)popen(pname,mode[n]);
  586: 
  587: pclose	wfileid -- wior	own
  588: wior=pclose((FILE *)wfileid);
  589: 
  590: time&date	-- nyear nmonth nday nhour nmin nsec	ansi	time_and_date
  591: struct timeval time1;
  592: struct timezone zone1;
  593: struct tm *ltime;
  594: gettimeofday(&time1,&zone1);
  595: ltime=localtime(&time1.tv_sec);
  596: nyear =ltime->tm_year+1900;
  597: nmonth=ltime->tm_mon;
  598: nday  =ltime->tm_mday;
  599: nhour =ltime->tm_hour;
  600: nmin  =ltime->tm_min;
  601: nsec  =ltime->tm_sec;
  602: 
  603: ms	n --	ansi
  604: struct timeval timeout;
  605: timeout.tv_sec=n/1000;
  606: timeout.tv_usec=1000*(n%1000);
  607: (void)select(0,0,0,0,&timeout);
  608: 
  609: allocate	u -- a_addr wior	memory
  610: a_addr = (Cell *)malloc(u);
  611: wior = a_addr==NULL;	/* !! Define a return code */
  612: 
  613: free		a_addr -- wior		memory
  614: free(a_addr);
  615: wior = 0;
  616: 
  617: resize		a_addr1 u -- a_addr2 wior	memory
  618: a_addr2 = realloc(a_addr1, u);
  619: wior = a_addr2==NULL;	/* !! Define a return code */
  620: 
  621: (f83find)	c_addr u f83name1 -- f83name2	new	paren_f83find
  622: for (; f83name1 != NULL; f83name1 = f83name1->next)
  623:   if (F83NAME_COUNT(f83name1)==u &&
  624:       strncmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
  625:     break;
  626: f83name2=f83name1;
  627: 
  628: (f83casefind)	c_addr u f83name1 -- f83name2	new	paren_f83casefind
  629: for (; f83name1 != NULL; f83name1 = f83name1->next)
  630:   if (F83NAME_COUNT(f83name1)==u &&
  631:       strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
  632:     break;
  633: f83name2=f83name1;
  634: 
  635: (parse-white)	c_addr1 u1 -- c_addr2 u2	new	paren_parse_white
  636: /* use !isgraph instead of isspace? */
  637: Char *endp = c_addr1+u1;
  638: while (c_addr1<endp && isspace(*c_addr1))
  639:   c_addr1++;
  640: if (c_addr1<endp) {
  641:   for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
  642:     ;
  643:   u2 = c_addr1-c_addr2;
  644: }
  645: else {
  646:   c_addr2 = c_addr1;
  647:   u2 = 0;
  648: }
  649: 
  650: close-file	wfileid -- wior	file	close_file
  651: wior = FILEIO(fclose((FILE *)wfileid)==EOF);
  652: 
  653: open-file	c_addr u ntype -- w2 wior	file	open_file
  654: char fname[u+1];
  655: cstr(fname, c_addr, u);
  656: w2 = (Cell)fopen(fname, fileattr[ntype]);
  657: wior =  FILEEXIST(w2 == NULL);
  658: 
  659: create-file	c_addr u ntype -- w2 wior	file	create_file
  660: int	fd;
  661: char fname[u+1];
  662: cstr(fname, c_addr, u);
  663: fd = creat(fname, 0666);
  664: if (fd > -1) {
  665:   w2 = (Cell)fdopen(fd, fileattr[ntype]);
  666:   assert(w2 != NULL);
  667:   wior = 0;
  668: } else {
  669:   assert(fd == -1);
  670:   wior = FILEIO(fd);
  671:   w2 = 0;
  672: }
  673: 
  674: delete-file	c_addr u -- wior		file	delete_file
  675: char fname[u+1];
  676: cstr(fname, c_addr, u);
  677: wior = FILEEXIST(unlink(fname));
  678: 
  679: rename-file	c_addr1 u1 c_addr2 u2 -- wior	file-ext	rename_file
  680: char fname1[u1+1];
  681: char fname2[u2+1];
  682: cstr(fname1, c_addr1, u1);
  683: cstr(fname2, c_addr2, u2);
  684: wior = FILEEXIST(rename(fname1, fname2));
  685: 
  686: file-position	wfileid -- ud wior	file	file_position
  687: /* !! use tell and lseek? */
  688: ud = ftell((FILE *)wfileid);
  689: wior = 0; /* !! or wior = FLAG(ud<0) */
  690: 
  691: reposition-file	ud wfileid -- wior	file	reposition_file
  692: wior = FILEIO(fseek((FILE *)wfileid, (long)ud, SEEK_SET));
  693: 
  694: file-size	wfileid -- ud wior	file	file_size
  695: struct stat buf;
  696: wior = FILEEXIST(fstat(fileno((FILE *)wfileid), &buf));
  697: ud = buf.st_size;
  698: 
  699: resize-file	ud wfileid -- wior	file	resize_file
  700: wior = FILEIO(ftruncate(fileno((FILE *)wfileid), (int)ud));
  701: 
  702: read-file	c_addr u1 wfileid -- u2 wior	file	read_file
  703: /* !! fread does not guarantee enough */
  704: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
  705: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
  706: /* !! who performs clearerr((FILE *)wfileid); ? */
  707: 
  708: read-line	c_addr u1 wfileid -- u2 flag wior	file	read_line
  709: wior=(Cell)fgets(c_addr,u1+1,(FILE *)wfileid);
  710: flag=FLAG(!feof((FILE *)wfileid) && wior);
  711: wior=FILEIO(ferror((FILE *)wfileid)) & flag;
  712: u2=(flag & strlen(c_addr));
  713: u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
  714: 
  715: write-file	c_addr u1 wfileid -- wior	file	write_file
  716: /* !! fwrite does not guarantee enough */
  717: {
  718:   int u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
  719:   wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
  720: }
  721: 
  722: flush-file	wfileid -- wior		file-ext	flush_file
  723: wior = FILEIO(fflush((FILE *) wfileid));
  724: 
  725: comparisons(f, r1 r2, f_, r1, r2, new, new, float, new)
  726: comparisons(f0, r, f_zero_, r, 0., float, new, float, new)
  727: 
  728: d>f		d -- r		float	d_to_f
  729: r = d;
  730: 
  731: f>d		r -- d		float	f_to_d
  732: /* !! basis 15 is not very specific */
  733: d = r;
  734: 
  735: f!		r f_addr --	float	f_store
  736: *f_addr = r;
  737: 
  738: f@		f_addr -- r	float	f_fetch
  739: r = *f_addr;
  740: 
  741: df@		df_addr -- r	float-ext	d_f_fetch
  742: #ifdef IEEE_FP
  743: r = *df_addr;
  744: #else
  745: !! df@
  746: #endif
  747: 
  748: df!		r df_addr --	float-ext	d_f_store
  749: #ifdef IEEE_FP
  750: *df_addr = r;
  751: #else
  752: !! df!
  753: #endif
  754: 
  755: sf@		sf_addr -- r	float-ext	s_f_fetch
  756: #ifdef IEEE_FP
  757: r = *sf_addr;
  758: #else
  759: !! sf@
  760: #endif
  761: 
  762: sf!		r sf_addr --	float-ext	s_f_store
  763: #ifdef IEEE_FP
  764: *sf_addr = r;
  765: #else
  766: !! sf!
  767: #endif
  768: 
  769: f+		r1 r2 -- r3	float	f_plus
  770: r3 = r1+r2;
  771: 
  772: f-		r1 r2 -- r3	float	f_minus
  773: r3 = r1-r2;
  774: 
  775: f*		r1 r2 -- r3	float	f_star
  776: r3 = r1*r2;
  777: 
  778: f/		r1 r2 -- r3	float	f_slash
  779: r3 = r1/r2;
  780: 
  781: f**		r1 r2 -- r3	float-ext	f_star_star
  782: r3 = pow(r1,r2);
  783: 
  784: fnegate		r1 -- r2	float
  785: r2 = - r1;
  786: 
  787: fdrop		r --		float
  788: 
  789: fdup		r -- r r	float
  790: 
  791: fswap		r1 r2 -- r2 r1	float
  792: 
  793: fover		r1 r2 -- r1 r2 r1	float
  794: 
  795: frot		r1 r2 r3 -- r2 r3 r1	float
  796: 
  797: float+		f_addr1 -- f_addr2	float	float_plus
  798: f_addr2 = f_addr1+1;
  799: 
  800: floats		n1 -- n2	float
  801: n2 = n1*sizeof(Float);
  802: 
  803: floor		r1 -- r2	float
  804: /* !! unclear wording */
  805: r2 = floor(r1);
  806: 
  807: fround		r1 -- r2	float
  808: /* !! unclear wording */
  809: r2 = rint(r1);
  810: 
  811: fmax		r1 r2 -- r3	float
  812: if (r1<r2)
  813:   r3 = r2;
  814: else
  815:   r3 = r1;
  816: 
  817: fmin		r1 r2 -- r3	float
  818: if (r1<r2)
  819:   r3 = r1;
  820: else
  821:   r3 = r2;
  822: 
  823: represent		r c_addr u -- n f1 f2	float
  824: char *sig;
  825: int flag;
  826: int decpt;
  827: sig=ecvt(r, u, &decpt, &flag);
  828: n=decpt;
  829: f1=FLAG(flag!=0);
  830: f2=FLAG(isdigit(sig[0])!=0);
  831: memmove(c_addr,sig,u);
  832: 
  833: >float	c_addr u -- flag	float	to_float
  834: /* real signature: c_addr u -- r t / f */
  835: Float r;
  836: char number[u+1];
  837: char *endconv;
  838: cstr(number, c_addr, u);
  839: r=strtod(number,&endconv);
  840: if((flag=FLAG(!(int)*endconv)))
  841: {
  842: 	IF_FTOS(fp[0] = FTOS);
  843: 	fp += -1;
  844: 	FTOS = r;
  845: }
  846: else if(*endconv=='d' || *endconv=='D')
  847: {
  848: 	*endconv='E';
  849: 	r=strtod(number,&endconv);
  850: 	if((flag=FLAG(!(int)*endconv)))
  851: 	{
  852: 		IF_FTOS(fp[0] = FTOS);
  853: 		fp += -1;
  854: 		FTOS = r;
  855: 	}
  856: }
  857: 
  858: fabs		r1 -- r2	float-ext
  859: r2 = fabs(r1);
  860: 
  861: facos		r1 -- r2	float-ext
  862: r2 = acos(r1);
  863: 
  864: fasin		r1 -- r2	float-ext
  865: r2 = asin(r1);
  866: 
  867: fatan		r1 -- r2	float-ext
  868: r2 = atan(r1);
  869: 
  870: fatan2		r1 r2 -- r3	float-ext
  871: r3 = atan2(r1,r2);
  872: 
  873: fcos		r1 -- r2	float-ext
  874: r2 = cos(r1);
  875: 
  876: fexp		r1 -- r2	float-ext
  877: r2 = exp(r1);
  878: 
  879: fexpm1		r1 -- r2	float-ext
  880: r2 =
  881: #ifdef expm1
  882: 	expm1(r1);
  883: #else
  884: 	exp(r1)-1;
  885: #endif
  886: 
  887: fln		r1 -- r2	float-ext
  888: r2 = log(r1);
  889: 
  890: flnp1		r1 -- r2	float-ext
  891: r2 =
  892: #ifdef log1p
  893: 	log1p(r1);
  894: #else
  895: 	log(r1+1);
  896: #endif
  897: 
  898: flog		r1 -- r2	float-ext
  899: r2 = log10(r1);
  900: 
  901: fsin		r1 -- r2	float-ext
  902: r2 = sin(r1);
  903: 
  904: fsincos		r1 -- r2 r3	float-ext
  905: r2 = sin(r1);
  906: r3 = cos(r1);
  907: 
  908: fsqrt		r1 -- r2	float-ext
  909: r2 = sqrt(r1);
  910: 
  911: ftan		r1 -- r2	float-ext
  912: r2 = tan(r1);
  913: 
  914: \ The following words access machine/OS/installation-dependent ANSI
  915: \   figForth internals
  916: \ !! how about environmental queries DIRECT-THREADED,
  917: \   INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
  918: 
  919: >body		xt -- a_addr	core	to_body
  920: a_addr = PFA(xt);
  921: 
  922: >code-address		xt -- c_addr		new	to_code_address
  923: ""c_addr is the code address of the word xt""
  924: /* !! This behaves installation-dependently for DOES-words */
  925: c_addr = CODE_ADDRESS(xt);
  926: 
  927: >does-code	xt -- a_addr		new	to_does_code
  928: ""If xt ist the execution token of a defining-word-defined word,
  929: a_addr is the start of the Forth code after the DOES>; Otherwise the
  930: behaviour is uundefined""
  931: /* !! there is currently no way to determine whether a word is
  932: defining-word-defined */
  933: a_addr = DOES_CODE(xt);
  934: 
  935: code-address!		n xt --	new	code_address_store
  936: ""Creates a code field with code address c_addr at xt""
  937: MAKE_CF(xt, symbols[CF(n)]);
  938: CACHE_FLUSH(xt,PFA(0));
  939: 
  940: does-code!	a_addr xt --		new	does_code_store
  941: ""creates a code field at xt for a defining-word-defined word; a_addr
  942: is the start of the Forth code after DOES>""
  943: MAKE_DOES_CF(xt, a_addr);
  944: CACHE_FLUSH(xt,PFA(0));
  945: 
  946: does-handler!	a_addr --	new	does_jump_store
  947: ""creates a DOES>-handler at address a_addr. a_addr usually points
  948: just behind a DOES>.""
  949: MAKE_DOES_HANDLER(a_addr);
  950: CACHE_FLUSH(a_addr,DOES_HANDLER_SIZE);
  951: 
  952: /does-handler	-- n	new	slash_does_handler
  953: ""the size of a does-handler (includes possible padding)""
  954: /* !! a constant or environmental query might be better */
  955: n = DOES_HANDLER_SIZE;
  956: 
  957: toupper	c1 -- c2	new
  958: c2 = toupper(c1);
  959: 
  960: \ local variable implementation primitives
  961: @local#		-- w	new	fetch_local_number
  962: w = *(Cell *)(lp+(int)(*ip++));
  963: 
  964: @local0	-- w	new	fetch_local_zero
  965: w = *(Cell *)(lp+0);
  966: 
  967: @local4	-- w	new	fetch_local_four
  968: w = *(Cell *)(lp+4);
  969: 
  970: @local8	-- w	new	fetch_local_eight
  971: w = *(Cell *)(lp+8);
  972: 
  973: @local12	-- w	new	fetch_local_twelve
  974: w = *(Cell *)(lp+12);
  975: 
  976: f@local#	-- r	new	f_fetch_local_number
  977: r = *(Float *)(lp+(int)(*ip++));
  978: 
  979: f@local0	-- r	new	f_fetch_local_zero
  980: r = *(Float *)(lp+0);
  981: 
  982: f@local8	-- r	new	f_fetch_local_eight
  983: r = *(Float *)(lp+8);
  984: 
  985: laddr#		-- c_addr	new	laddr_number
  986: /* this can also be used to implement lp@ */
  987: c_addr = (Char *)(lp+(int)(*ip++));
  988: 
  989: lp+!#	--	new	lp_plus_store_number
  990: ""used with negative immediate values it allocates memory on the
  991: local stack, a positive immediate argument drops memory from the local
  992: stack""
  993: lp += (int)(*ip++);
  994: 
  995: -4lp+!	--	new	minus_four_lp_plus_store
  996: lp += -4;
  997: 
  998: 8lp+!	--	new	eight_lp_plus_store
  999: lp += 8;
 1000: 
 1001: 16lp+!	--	new	sixteen_lp_plus_store
 1002: lp += 16;
 1003: 
 1004: lp!	c_addr --	new	lp_store
 1005: lp = (Address)c_addr;
 1006: 
 1007: >l	w --	new	to_l
 1008: lp -= sizeof(Cell);
 1009: *(Cell *)lp = w;
 1010: 
 1011: f>l	r --	new	f_to_l
 1012: lp -= sizeof(Float);
 1013: *(Float *)lp = r;
 1014: 
 1015: up!	a_addr --	new	up_store
 1016: up=(char *)a_addr;

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