1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: calfunc.c,v 2.15 2006/05/10 15:21:20 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* calfunc.c - routines for calcomp using functions. |
6 |
* |
7 |
* If VARIABLE is not set, only library functions |
8 |
* can be accessed. |
9 |
* |
10 |
* 2/19/03 Eliminated conditional compiles in favor of esupport extern. |
11 |
*/ |
12 |
|
13 |
#include "copyright.h" |
14 |
|
15 |
#include <stdio.h> |
16 |
#include <string.h> |
17 |
#include <errno.h> |
18 |
#include <math.h> |
19 |
|
20 |
#include "rterror.h" |
21 |
#include "calcomp.h" |
22 |
|
23 |
/* bits in argument flag (better be right!) */ |
24 |
#define AFLAGSIZ (8*sizeof(unsigned long)) |
25 |
#define ALISTSIZ 6 /* maximum saved argument list */ |
26 |
|
27 |
typedef struct activation { |
28 |
char *name; /* function name */ |
29 |
struct activation *prev; /* previous activation */ |
30 |
double *ap; /* argument list */ |
31 |
unsigned long an; /* computed argument flags */ |
32 |
EPNODE *fun; /* argument function */ |
33 |
} ACTIVATION; /* an activation record */ |
34 |
|
35 |
static ACTIVATION *curact = NULL; |
36 |
|
37 |
static double libfunc(char *fname, VARDEF *vp); |
38 |
|
39 |
#ifndef MAXLIB |
40 |
#define MAXLIB 64 /* maximum number of library functions */ |
41 |
#endif |
42 |
|
43 |
static double l_if(char *), l_select(char *), l_rand(char *); |
44 |
static double l_floor(char *), l_ceil(char *); |
45 |
static double l_sqrt(char *); |
46 |
static double l_sin(char *), l_cos(char *), l_tan(char *); |
47 |
static double l_asin(char *), l_acos(char *), l_atan(char *), l_atan2(char *); |
48 |
static double l_exp(char *), l_log(char *), l_log10(char *); |
49 |
|
50 |
/* functions must be listed alphabetically */ |
51 |
static LIBR library[MAXLIB] = { |
52 |
{ "acos", 1, ':', l_acos }, |
53 |
{ "asin", 1, ':', l_asin }, |
54 |
{ "atan", 1, ':', l_atan }, |
55 |
{ "atan2", 2, ':', l_atan2 }, |
56 |
{ "ceil", 1, ':', l_ceil }, |
57 |
{ "cos", 1, ':', l_cos }, |
58 |
{ "exp", 1, ':', l_exp }, |
59 |
{ "floor", 1, ':', l_floor }, |
60 |
{ "if", 3, ':', l_if }, |
61 |
{ "log", 1, ':', l_log }, |
62 |
{ "log10", 1, ':', l_log10 }, |
63 |
{ "rand", 1, ':', l_rand }, |
64 |
{ "select", 1, ':', l_select }, |
65 |
{ "sin", 1, ':', l_sin }, |
66 |
{ "sqrt", 1, ':', l_sqrt }, |
67 |
{ "tan", 1, ':', l_tan }, |
68 |
}; |
69 |
|
70 |
static int libsize = 16; |
71 |
|
72 |
#define resolve(ep) ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan)) |
73 |
|
74 |
|
75 |
int |
76 |
fundefined( /* return # of arguments for function */ |
77 |
char *fname |
78 |
) |
79 |
{ |
80 |
LIBR *lp; |
81 |
VARDEF *vp; |
82 |
|
83 |
if ((vp = varlookup(fname)) != NULL && vp->def != NULL |
84 |
&& vp->def->v.kid->type == FUNC) |
85 |
return(nekids(vp->def->v.kid) - 1); |
86 |
lp = vp != NULL ? vp->lib : liblookup(fname); |
87 |
if (lp == NULL) |
88 |
return(0); |
89 |
return(lp->nargs); |
90 |
} |
91 |
|
92 |
|
93 |
double |
94 |
funvalue( /* return a function value to the user */ |
95 |
char *fname, |
96 |
int n, |
97 |
double *a |
98 |
) |
99 |
{ |
100 |
ACTIVATION act; |
101 |
VARDEF *vp; |
102 |
double rval; |
103 |
/* push environment */ |
104 |
act.name = fname; |
105 |
act.prev = curact; |
106 |
act.ap = a; |
107 |
if (n >= AFLAGSIZ) |
108 |
act.an = ~0; |
109 |
else |
110 |
act.an = (1L<<n)-1; |
111 |
act.fun = NULL; |
112 |
curact = &act; |
113 |
|
114 |
if ((vp = varlookup(fname)) == NULL || vp->def == NULL |
115 |
|| vp->def->v.kid->type != FUNC) |
116 |
rval = libfunc(fname, vp); |
117 |
else |
118 |
rval = evalue(vp->def->v.kid->sibling); |
119 |
|
120 |
curact = act.prev; /* pop environment */ |
121 |
return(rval); |
122 |
} |
123 |
|
124 |
|
125 |
void |
126 |
funset( /* set a library function */ |
127 |
char *fname, |
128 |
int nargs, |
129 |
int assign, |
130 |
double (*fptr)(char *) |
131 |
) |
132 |
{ |
133 |
int oldlibsize = libsize; |
134 |
char *cp; |
135 |
LIBR *lp; |
136 |
/* check for context */ |
137 |
for (cp = fname; *cp; cp++) |
138 |
; |
139 |
if (cp == fname) |
140 |
return; |
141 |
if (cp[-1] == CNTXMARK) |
142 |
*--cp = '\0'; |
143 |
if ((lp = liblookup(fname)) == NULL) { /* insert */ |
144 |
if (libsize >= MAXLIB) { |
145 |
eputs("Too many library functons!\n"); |
146 |
quit(1); |
147 |
} |
148 |
for (lp = &library[libsize]; lp > library; lp--) |
149 |
if (strcmp(lp[-1].fname, fname) > 0) { |
150 |
lp[0].fname = lp[-1].fname; |
151 |
lp[0].nargs = lp[-1].nargs; |
152 |
lp[0].atyp = lp[-1].atyp; |
153 |
lp[0].f = lp[-1].f; |
154 |
} else |
155 |
break; |
156 |
libsize++; |
157 |
} |
158 |
if (fptr == NULL) { /* delete */ |
159 |
while (lp < &library[libsize-1]) { |
160 |
lp[0].fname = lp[1].fname; |
161 |
lp[0].nargs = lp[1].nargs; |
162 |
lp[0].atyp = lp[1].atyp; |
163 |
lp[0].f = lp[1].f; |
164 |
lp++; |
165 |
} |
166 |
libsize--; |
167 |
} else { /* or assign */ |
168 |
lp[0].fname = fname; /* string must be static! */ |
169 |
lp[0].nargs = nargs; |
170 |
lp[0].atyp = assign; |
171 |
lp[0].f = fptr; |
172 |
} |
173 |
if (libsize != oldlibsize) |
174 |
libupdate(fname); /* relink library */ |
175 |
} |
176 |
|
177 |
|
178 |
int |
179 |
nargum(void) /* return number of available arguments */ |
180 |
{ |
181 |
int n; |
182 |
|
183 |
if (curact == NULL) |
184 |
return(0); |
185 |
if (curact->fun == NULL) { |
186 |
for (n = 0; (1L<<n) & curact->an; n++) |
187 |
; |
188 |
return(n); |
189 |
} |
190 |
return(nekids(curact->fun) - 1); |
191 |
} |
192 |
|
193 |
|
194 |
double |
195 |
argument(int n) /* return nth argument for active function */ |
196 |
{ |
197 |
ACTIVATION *actp = curact; |
198 |
EPNODE *ep = NULL; |
199 |
double aval; |
200 |
|
201 |
if (actp == NULL || --n < 0) { |
202 |
eputs("Bad call to argument!\n"); |
203 |
quit(1); |
204 |
} |
205 |
/* already computed? */ |
206 |
if (n < AFLAGSIZ && 1L<<n & actp->an) |
207 |
return(actp->ap[n]); |
208 |
|
209 |
if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) { |
210 |
eputs(actp->name); |
211 |
eputs(": too few arguments\n"); |
212 |
quit(1); |
213 |
} |
214 |
curact = actp->prev; /* pop environment */ |
215 |
aval = evalue(ep); /* compute argument */ |
216 |
curact = actp; /* push back environment */ |
217 |
if (n < ALISTSIZ) { /* save value */ |
218 |
actp->ap[n] = aval; |
219 |
actp->an |= 1L<<n; |
220 |
} |
221 |
return(aval); |
222 |
} |
223 |
|
224 |
|
225 |
VARDEF * |
226 |
argf(int n) /* return function def for nth argument */ |
227 |
{ |
228 |
ACTIVATION *actp; |
229 |
EPNODE *ep; |
230 |
|
231 |
for (actp = curact; actp != NULL; actp = actp->prev) { |
232 |
|
233 |
if (n <= 0) |
234 |
break; |
235 |
|
236 |
if (actp->fun == NULL) |
237 |
goto badarg; |
238 |
|
239 |
if ((ep = ekid(actp->fun, n)) == NULL) { |
240 |
eputs(actp->name); |
241 |
eputs(": too few arguments\n"); |
242 |
quit(1); |
243 |
} |
244 |
if (ep->type == VAR) |
245 |
return(ep->v.ln); /* found it */ |
246 |
|
247 |
if (ep->type != ARG) |
248 |
goto badarg; |
249 |
|
250 |
n = ep->v.chan; /* try previous context */ |
251 |
} |
252 |
eputs("Bad call to argf!\n"); |
253 |
quit(1); |
254 |
|
255 |
badarg: |
256 |
eputs(actp->name); |
257 |
eputs(": argument not a function\n"); |
258 |
quit(1); |
259 |
return NULL; /* pro forma return */ |
260 |
} |
261 |
|
262 |
|
263 |
char * |
264 |
argfun(int n) /* return function name for nth argument */ |
265 |
{ |
266 |
return(argf(n)->name); |
267 |
} |
268 |
|
269 |
|
270 |
double |
271 |
efunc(EPNODE *ep) /* evaluate a function */ |
272 |
{ |
273 |
ACTIVATION act; |
274 |
double alist[ALISTSIZ]; |
275 |
double rval; |
276 |
VARDEF *dp; |
277 |
/* push environment */ |
278 |
dp = resolve(ep->v.kid); |
279 |
act.name = dp->name; |
280 |
act.prev = curact; |
281 |
act.ap = alist; |
282 |
act.an = 0; |
283 |
act.fun = ep; |
284 |
curact = &act; |
285 |
|
286 |
if (dp->def == NULL || dp->def->v.kid->type != FUNC) |
287 |
rval = libfunc(act.name, dp); |
288 |
else |
289 |
rval = evalue(dp->def->v.kid->sibling); |
290 |
|
291 |
curact = act.prev; /* pop environment */ |
292 |
return(rval); |
293 |
} |
294 |
|
295 |
|
296 |
LIBR * |
297 |
liblookup(char *fname) /* look up a library function */ |
298 |
{ |
299 |
int upper, lower; |
300 |
int cm, i; |
301 |
|
302 |
lower = 0; |
303 |
upper = cm = libsize; |
304 |
|
305 |
while ((i = (lower + upper) >> 1) != cm) { |
306 |
cm = strcmp(fname, library[i].fname); |
307 |
if (cm > 0) |
308 |
lower = i; |
309 |
else if (cm < 0) |
310 |
upper = i; |
311 |
else |
312 |
return(&library[i]); |
313 |
cm = i; |
314 |
} |
315 |
return(NULL); |
316 |
} |
317 |
|
318 |
|
319 |
/* |
320 |
* The following routines are for internal use: |
321 |
*/ |
322 |
|
323 |
|
324 |
static double |
325 |
libfunc( /* execute library function */ |
326 |
char *fname, |
327 |
VARDEF *vp |
328 |
) |
329 |
{ |
330 |
LIBR *lp; |
331 |
double d; |
332 |
int lasterrno; |
333 |
|
334 |
if (vp != NULL) |
335 |
lp = vp->lib; |
336 |
else |
337 |
lp = liblookup(fname); |
338 |
if (lp == NULL) { |
339 |
eputs(fname); |
340 |
eputs(": undefined function\n"); |
341 |
quit(1); |
342 |
} |
343 |
lasterrno = errno; |
344 |
errno = 0; |
345 |
d = (*lp->f)(lp->fname); |
346 |
#ifdef isnan |
347 |
if (errno == 0) |
348 |
if (isnan(d)) |
349 |
errno = EDOM; |
350 |
else if (isinf(d)) |
351 |
errno = ERANGE; |
352 |
#endif |
353 |
if (errno == EDOM || errno == ERANGE) { |
354 |
wputs(fname); |
355 |
if (errno == EDOM) |
356 |
wputs(": domain error\n"); |
357 |
else if (errno == ERANGE) |
358 |
wputs(": range error\n"); |
359 |
else |
360 |
wputs(": error in call\n"); |
361 |
return(0.0); |
362 |
} |
363 |
errno = lasterrno; |
364 |
return(d); |
365 |
} |
366 |
|
367 |
|
368 |
/* |
369 |
* Library functions: |
370 |
*/ |
371 |
|
372 |
|
373 |
static double |
374 |
l_if(char *nm) /* if(cond, then, else) conditional expression */ |
375 |
/* cond evaluates true if greater than zero */ |
376 |
{ |
377 |
if (argument(1) > 0.0) |
378 |
return(argument(2)); |
379 |
else |
380 |
return(argument(3)); |
381 |
} |
382 |
|
383 |
|
384 |
static double |
385 |
l_select(char *nm) /* return argument #(A1+1) */ |
386 |
{ |
387 |
int n; |
388 |
|
389 |
n = (int)(argument(1) + .5); |
390 |
if (n == 0) |
391 |
return(nargum()-1); |
392 |
if (n < 1 || n > nargum()-1) { |
393 |
errno = EDOM; |
394 |
return(0.0); |
395 |
} |
396 |
return(argument(n+1)); |
397 |
} |
398 |
|
399 |
|
400 |
static double |
401 |
l_rand(char *nm) /* random function between 0 and 1 */ |
402 |
{ |
403 |
double x; |
404 |
|
405 |
x = argument(1); |
406 |
x *= 1.0/(1.0 + x*x) + 2.71828182845904; |
407 |
x += .785398163397447 - floor(x); |
408 |
x = 1e5 / x; |
409 |
return(x - floor(x)); |
410 |
} |
411 |
|
412 |
|
413 |
static double |
414 |
l_floor(char *nm) /* return largest integer not greater than arg1 */ |
415 |
{ |
416 |
return(floor(argument(1))); |
417 |
} |
418 |
|
419 |
|
420 |
static double |
421 |
l_ceil(char *nm) /* return smallest integer not less than arg1 */ |
422 |
{ |
423 |
return(ceil(argument(1))); |
424 |
} |
425 |
|
426 |
|
427 |
static double |
428 |
l_sqrt(char *nm) |
429 |
{ |
430 |
return(sqrt(argument(1))); |
431 |
} |
432 |
|
433 |
|
434 |
static double |
435 |
l_sin(char *nm) |
436 |
{ |
437 |
return(sin(argument(1))); |
438 |
} |
439 |
|
440 |
|
441 |
static double |
442 |
l_cos(char *nm) |
443 |
{ |
444 |
return(cos(argument(1))); |
445 |
} |
446 |
|
447 |
|
448 |
static double |
449 |
l_tan(char *nm) |
450 |
{ |
451 |
return(tan(argument(1))); |
452 |
} |
453 |
|
454 |
|
455 |
static double |
456 |
l_asin(char *nm) |
457 |
{ |
458 |
return(asin(argument(1))); |
459 |
} |
460 |
|
461 |
|
462 |
static double |
463 |
l_acos(char *nm) |
464 |
{ |
465 |
return(acos(argument(1))); |
466 |
} |
467 |
|
468 |
|
469 |
static double |
470 |
l_atan(char *nm) |
471 |
{ |
472 |
return(atan(argument(1))); |
473 |
} |
474 |
|
475 |
|
476 |
static double |
477 |
l_atan2(char *nm) |
478 |
{ |
479 |
return(atan2(argument(1), argument(2))); |
480 |
} |
481 |
|
482 |
|
483 |
static double |
484 |
l_exp(char *nm) |
485 |
{ |
486 |
return(exp(argument(1))); |
487 |
} |
488 |
|
489 |
|
490 |
static double |
491 |
l_log(char *nm) |
492 |
{ |
493 |
return(log(argument(1))); |
494 |
} |
495 |
|
496 |
|
497 |
static double |
498 |
l_log10(char *nm) |
499 |
{ |
500 |
return(log10(argument(1))); |
501 |
} |