15 |
|
#include <stdio.h> |
16 |
|
#include <string.h> |
17 |
|
#include <errno.h> |
18 |
+ |
#include <stdlib.h> |
19 |
|
#include <math.h> |
20 |
|
|
21 |
|
#include "rterror.h" |
23 |
|
|
24 |
|
/* bits in argument flag (better be right!) */ |
25 |
|
#define AFLAGSIZ (8*sizeof(unsigned long)) |
26 |
< |
#define ALISTSIZ 8 /* maximum saved argument list */ |
26 |
> |
#define ALISTSIZ 10 /* maximum saved argument list */ |
27 |
|
|
28 |
|
typedef struct activation { |
29 |
|
char *name; /* function name */ |
41 |
|
#define MAXLIB 64 /* maximum number of library functions */ |
42 |
|
#endif |
43 |
|
|
44 |
< |
static double l_if(char *), l_select(char *), l_rand(char *); |
44 |
> |
static double l_if(char *), l_select(char *); |
45 |
> |
static double l_min(char *), l_max(char *); |
46 |
> |
static double l_rand(char *); |
47 |
|
static double l_floor(char *), l_ceil(char *); |
48 |
|
static double l_sqrt(char *); |
49 |
|
static double l_sin(char *), l_cos(char *), l_tan(char *); |
63 |
|
{ "if", 3, ':', l_if }, |
64 |
|
{ "log", 1, ':', l_log }, |
65 |
|
{ "log10", 1, ':', l_log10 }, |
66 |
+ |
{ "max", 1, ':', l_max }, |
67 |
+ |
{ "min", 1, ':', l_min }, |
68 |
|
{ "rand", 1, ':', l_rand }, |
69 |
|
{ "select", 1, ':', l_select }, |
70 |
|
{ "sin", 1, ':', l_sin }, |
72 |
|
{ "tan", 1, ':', l_tan }, |
73 |
|
}; |
74 |
|
|
75 |
< |
static int libsize = 16; |
75 |
> |
static int libsize = 18; |
76 |
|
|
77 |
|
#define resolve(ep) ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan)) |
78 |
|
|
79 |
|
|
80 |
|
int |
81 |
< |
fundefined( /* return # of arguments for function */ |
81 |
> |
fundefined( /* return # of req'd arguments for function */ |
82 |
|
char *fname |
83 |
|
) |
84 |
|
{ |
146 |
|
; |
147 |
|
if (cp == fname) |
148 |
|
return; |
149 |
< |
if (cp[-1] == CNTXMARK) |
149 |
> |
while (cp[-1] == CNTXMARK) { |
150 |
|
*--cp = '\0'; |
151 |
+ |
if (cp == fname) return; |
152 |
+ |
} |
153 |
|
if ((lp = liblookup(fname)) == NULL) { /* insert */ |
154 |
+ |
if (fptr == NULL) |
155 |
+ |
return; /* nothing! */ |
156 |
|
if (libsize >= MAXLIB) { |
157 |
|
eputs("Too many library functons!\n"); |
158 |
|
quit(1); |
201 |
|
argument(int n) /* return nth argument for active function */ |
202 |
|
{ |
203 |
|
ACTIVATION *actp = curact; |
204 |
< |
EPNODE *ep = NULL; |
204 |
> |
EPNODE *ep; |
205 |
|
double aval; |
206 |
|
|
207 |
|
if (!actp | (--n < 0)) { |
208 |
|
eputs("Bad call to argument!\n"); |
209 |
|
quit(1); |
210 |
|
} |
211 |
< |
if ((n < AFLAGSIZ) & (actp->an >> n)) /* already computed? */ |
211 |
> |
if ((n < AFLAGSIZ) & actp->an >> n) /* already computed? */ |
212 |
|
return(actp->ap[n]); |
213 |
|
|
214 |
|
if (!actp->fun || !(ep = ekid(actp->fun, n+1))) { |
218 |
|
} |
219 |
|
curact = actp->prev; /* previous context */ |
220 |
|
aval = evalue(ep); /* compute argument */ |
221 |
< |
curact = actp; /* push back context */ |
222 |
< |
if (n < ALISTSIZ) { /* save value if we can */ |
221 |
> |
curact = actp; /* put back calling context */ |
222 |
> |
if (n < ALISTSIZ) { /* save value if room */ |
223 |
|
actp->ap[n] = aval; |
224 |
|
actp->an |= 1L<<n; |
225 |
|
} |
298 |
|
} |
299 |
|
|
300 |
|
|
301 |
+ |
double |
302 |
+ |
eargument( /* evaluate an argument */ |
303 |
+ |
EPNODE *ep |
304 |
+ |
) |
305 |
+ |
{ |
306 |
+ |
if ((ep->v.chan <= AFLAGSIZ) & curact->an >> (ep->v.chan-1)) |
307 |
+ |
return(curact->ap[ep->v.chan-1]); |
308 |
+ |
|
309 |
+ |
return(argument(ep->v.chan)); |
310 |
+ |
} |
311 |
+ |
|
312 |
+ |
|
313 |
|
LIBR * |
314 |
|
liblookup(char *fname) /* look up a library function */ |
315 |
|
{ |
368 |
|
errno = ERANGE; |
369 |
|
} |
370 |
|
#endif |
371 |
< |
if (errno == EDOM || errno == ERANGE) { |
371 |
> |
if ((errno == EDOM) | (errno == ERANGE)) { |
372 |
|
wputs(fname); |
373 |
|
if (errno == EDOM) |
374 |
|
wputs(": domain error\n"); |
402 |
|
static double |
403 |
|
l_select(char *nm) /* return argument #(A1+1) */ |
404 |
|
{ |
405 |
< |
int n; |
405 |
> |
int narg = nargum(); |
406 |
> |
double a1 = argument(1); |
407 |
> |
int n = (int)(a1 + .5); |
408 |
|
|
409 |
< |
n = (int)(argument(1) + .5); |
387 |
< |
if (n == 0) |
388 |
< |
return(nargum()-1); |
389 |
< |
if (n < 1 || n > nargum()-1) { |
409 |
> |
if ((a1 < -.5) | (n >= narg)) { |
410 |
|
errno = EDOM; |
411 |
|
return(0.0); |
412 |
|
} |
413 |
+ |
if (!n) /* asking max index? */ |
414 |
+ |
return(narg-1); |
415 |
|
return(argument(n+1)); |
416 |
+ |
} |
417 |
+ |
|
418 |
+ |
|
419 |
+ |
static double |
420 |
+ |
l_max(char *nm) /* general maximum function */ |
421 |
+ |
{ |
422 |
+ |
int n = nargum(); |
423 |
+ |
double vmax = argument(1); |
424 |
+ |
|
425 |
+ |
while (n > 1) { |
426 |
+ |
double v = argument(n--); |
427 |
+ |
if (vmax < v) |
428 |
+ |
vmax = v; |
429 |
+ |
} |
430 |
+ |
return(vmax); |
431 |
+ |
} |
432 |
+ |
|
433 |
+ |
|
434 |
+ |
static double |
435 |
+ |
l_min(char *nm) /* general minimum function */ |
436 |
+ |
{ |
437 |
+ |
int n = nargum(); |
438 |
+ |
double vmin = argument(1); |
439 |
+ |
|
440 |
+ |
while (n > 1) { |
441 |
+ |
double v = argument(n--); |
442 |
+ |
if (vmin > v) |
443 |
+ |
vmin = v; |
444 |
+ |
} |
445 |
+ |
return(vmin); |
446 |
|
} |
447 |
|
|
448 |
|
|