27 |
|
#include "calcomp.h" |
28 |
|
|
29 |
|
#define MAXLINE 256 /* maximum line length */ |
30 |
– |
#define MAXWORD 64 /* maximum word length */ |
30 |
|
|
31 |
|
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
32 |
|
|
34 |
– |
#define isid(c) (isalnum(c) || (c) == '_' || (c) == '.') |
35 |
– |
|
33 |
|
#define isdecimal(c) (isdigit(c) || (c) == '.') |
34 |
|
|
35 |
|
extern double atof(), pow(); |
75 |
|
esubtr, |
76 |
|
0, |
77 |
|
edivi, |
78 |
< |
0,0,0,0,0,0,0,0,0,0,0,0,0, |
78 |
> |
0,0,0,0,0,0,0,0,0,0, |
79 |
|
ebotch, |
80 |
+ |
0,0, |
81 |
+ |
ebotch, |
82 |
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
83 |
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
84 |
|
epow, |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
< |
scan() /* scan next character */ |
326 |
> |
getscanpos(fnp, lnp, spp, fpp) /* return current scan position */ |
327 |
> |
char **fnp; |
328 |
> |
int *lnp; |
329 |
> |
char **spp; |
330 |
> |
FILE **fpp; |
331 |
|
{ |
332 |
+ |
if (fnp != NULL) *fnp = infile; |
333 |
+ |
if (lnp != NULL) *lnp = lineno; |
334 |
+ |
if (spp != NULL) *spp = linbuf+linepos; |
335 |
+ |
if (fpp != NULL) *fpp = infp; |
336 |
+ |
} |
337 |
+ |
|
338 |
+ |
|
339 |
+ |
int |
340 |
+ |
scan() /* scan next character, return literal next */ |
341 |
+ |
{ |
342 |
+ |
register int lnext = 0; |
343 |
+ |
|
344 |
|
do { |
345 |
|
if (linbuf[linepos] == '\0') |
346 |
|
if (infp == NULL || fgets(linbuf, MAXLINE, infp) == NULL) |
352 |
|
} |
353 |
|
else |
354 |
|
nextc = linbuf[linepos++]; |
355 |
+ |
if (!lnext) |
356 |
+ |
lnext = nextc; |
357 |
|
if (nextc == '{') { |
358 |
|
scan(); |
359 |
|
while (nextc != '}') |
364 |
|
scan(); |
365 |
|
} |
366 |
|
} while (isspace(nextc)); |
367 |
+ |
return(lnext); |
368 |
|
} |
369 |
|
|
370 |
|
|
438 |
|
getname() /* scan an identifier */ |
439 |
|
{ |
440 |
|
static char str[MAXWORD+1]; |
441 |
< |
register int i; |
441 |
> |
register int i, lnext; |
442 |
|
|
443 |
< |
for (i = 0; i < MAXWORD && isid(nextc); i++, scan()) |
444 |
< |
str[i] = nextc; |
443 |
> |
lnext = nextc; |
444 |
> |
for (i = 0; i < MAXWORD && isid(lnext); i++, lnext = scan()) |
445 |
> |
str[i] = lnext; |
446 |
|
str[i] = '\0'; |
447 |
|
|
448 |
|
return(str); |
452 |
|
int |
453 |
|
getinum() /* scan a positive integer */ |
454 |
|
{ |
455 |
< |
register int n; |
455 |
> |
register int n, lnext; |
456 |
|
|
457 |
|
n = 0; |
458 |
< |
while (isdigit(nextc)) { |
459 |
< |
n = n * 10 + nextc - '0'; |
460 |
< |
scan(); |
458 |
> |
lnext = nextc; |
459 |
> |
while (isdigit(lnext)) { |
460 |
> |
n = n * 10 + lnext - '0'; |
461 |
> |
lnext = scan(); |
462 |
|
} |
463 |
|
return(n); |
464 |
|
} |
467 |
|
double |
468 |
|
getnum() /* scan a positive float */ |
469 |
|
{ |
470 |
< |
register int i; |
470 |
> |
register int i, lnext; |
471 |
|
char str[MAXWORD+1]; |
472 |
|
|
473 |
|
i = 0; |
474 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
475 |
< |
str[i++] = nextc; |
476 |
< |
scan(); |
474 |
> |
lnext = nextc; |
475 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
476 |
> |
str[i++] = lnext; |
477 |
> |
lnext = scan(); |
478 |
|
} |
479 |
< |
if (nextc == '.' && i < MAXWORD) { |
480 |
< |
str[i++] = nextc; |
481 |
< |
scan(); |
482 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
483 |
< |
str[i++] = nextc; |
484 |
< |
scan(); |
479 |
> |
if (lnext == '.' && i < MAXWORD) { |
480 |
> |
str[i++] = lnext; |
481 |
> |
lnext = scan(); |
482 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
483 |
> |
str[i++] = lnext; |
484 |
> |
lnext = scan(); |
485 |
|
} |
486 |
|
} |
487 |
< |
if ((nextc == 'e' || nextc == 'E') && i < MAXWORD) { |
488 |
< |
str[i++] = nextc; |
489 |
< |
scan(); |
490 |
< |
if ((nextc == '-' || nextc == '+') && i < MAXWORD) { |
491 |
< |
str[i++] = nextc; |
492 |
< |
scan(); |
487 |
> |
if ((lnext == 'e' || lnext == 'E') && i < MAXWORD) { |
488 |
> |
str[i++] = lnext; |
489 |
> |
lnext = scan(); |
490 |
> |
if ((lnext == '-' || lnext == '+') && i < MAXWORD) { |
491 |
> |
str[i++] = lnext; |
492 |
> |
lnext = scan(); |
493 |
|
} |
494 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
495 |
< |
str[i++] = nextc; |
496 |
< |
scan(); |
494 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
495 |
> |
str[i++] = lnext; |
496 |
> |
lnext = scan(); |
497 |
|
} |
498 |
|
} |
499 |
|
str[i] = '\0'; |
604 |
|
/* ARG */ |
605 |
|
{ |
606 |
|
int i; |
607 |
+ |
char *nam; |
608 |
|
register EPNODE *ep1, *ep2; |
609 |
|
|
610 |
|
if (nextc == '(') { |
628 |
|
|
629 |
|
#if defined(VARIABLE) || defined(FUNCTION) |
630 |
|
if (isalpha(nextc)) { |
631 |
< |
ep1 = newnode(); |
610 |
< |
ep1->type = VAR; |
611 |
< |
ep1->v.ln = varinsert(getname()); |
612 |
< |
|
631 |
> |
nam = getname(); |
632 |
|
#if defined(VARIABLE) && defined(FUNCTION) |
633 |
+ |
ep1 = NULL; |
634 |
|
if (curfunc != NULL) |
635 |
|
for (i = 1, ep2 = curfunc->v.kid->sibling; |
636 |
|
ep2 != NULL; i++, ep2 = ep2->sibling) |
637 |
< |
if (!strcmp(ep2->v.name, ep1->v.ln->name)) { |
618 |
< |
epfree(ep1); |
637 |
> |
if (!strcmp(ep2->v.name, nam)) { |
638 |
|
ep1 = newnode(); |
639 |
|
ep1->type = ARG; |
640 |
|
ep1->v.chan = i; |
641 |
|
break; |
642 |
|
} |
643 |
+ |
if (ep1 == NULL) |
644 |
|
#endif |
645 |
+ |
{ |
646 |
+ |
ep1 = newnode(); |
647 |
+ |
ep1->type = VAR; |
648 |
+ |
ep1->v.ln = varinsert(nam); |
649 |
+ |
} |
650 |
|
#ifdef FUNCTION |
651 |
|
if (nextc == '(') { |
652 |
|
ep2 = newnode(); |
666 |
|
syntax("'(' expected"); |
667 |
|
#endif |
668 |
|
#endif |
669 |
+ |
#ifdef RCONST |
670 |
+ |
if (isconstvar(ep1)) |
671 |
+ |
ep1 = rconst(ep1); |
672 |
+ |
#endif |
673 |
|
return(ep1); |
674 |
|
} |
675 |
|
#endif |
701 |
|
|
702 |
|
return(ep); |
703 |
|
} |
704 |
+ |
|
705 |
+ |
|
706 |
+ |
isconstvar(ep) /* is ep linked to a constant expression? */ |
707 |
+ |
register EPNODE *ep; |
708 |
+ |
{ |
709 |
+ |
#ifdef VARIABLE |
710 |
+ |
register EPNODE *ep1; |
711 |
+ |
#ifdef FUNCTION |
712 |
+ |
|
713 |
+ |
if (ep->type == FUNC) { |
714 |
+ |
if (!isconstfun(ep->v.kid)) |
715 |
+ |
return(0); |
716 |
+ |
for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) |
717 |
+ |
if (ep1->type != NUM && !isconstfun(ep1)) |
718 |
+ |
return(0); |
719 |
+ |
return(1); |
720 |
+ |
} |
721 |
+ |
#endif |
722 |
+ |
if (ep->type != VAR) |
723 |
+ |
return(0); |
724 |
+ |
ep1 = ep->v.ln->def; |
725 |
+ |
if (ep1 == NULL || ep1->type != ':') |
726 |
+ |
return(0); |
727 |
+ |
#ifdef FUNCTION |
728 |
+ |
if (ep1->v.kid->type != SYM) |
729 |
+ |
return(0); |
730 |
+ |
#endif |
731 |
+ |
return(1); |
732 |
+ |
#else |
733 |
+ |
return(ep->type == FUNC); |
734 |
+ |
#endif |
735 |
+ |
} |
736 |
+ |
|
737 |
+ |
|
738 |
+ |
#if defined(FUNCTION) && defined(VARIABLE) |
739 |
+ |
isconstfun(ep) /* is ep linked to a constant function? */ |
740 |
+ |
register EPNODE *ep; |
741 |
+ |
{ |
742 |
+ |
register EPNODE *dp; |
743 |
+ |
register LIBR *lp; |
744 |
+ |
|
745 |
+ |
if (ep->type != VAR) |
746 |
+ |
return(0); |
747 |
+ |
dp = ep->v.ln->def; |
748 |
+ |
if (dp != NULL && dp->type != ':') |
749 |
+ |
return(0); |
750 |
+ |
if ((dp == NULL || dp->v.kid->type != FUNC) |
751 |
+ |
&& ((lp = liblookup(ep->v.ln->name)) == NULL |
752 |
+ |
|| lp->atyp != ':')) |
753 |
+ |
return(0); |
754 |
+ |
return(1); |
755 |
+ |
} |
756 |
+ |
#endif |
757 |
|
#endif |