1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
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(); |
35 |
> |
#ifndef atof |
36 |
> |
extern double atof(); |
37 |
> |
#endif |
38 |
> |
extern double pow(); |
39 |
|
extern char *fgets(), *savestr(); |
40 |
|
extern char *emalloc(), *ecalloc(); |
41 |
|
extern EPNODE *curfunc; |
78 |
|
esubtr, |
79 |
|
0, |
80 |
|
edivi, |
81 |
< |
0,0,0,0,0,0,0,0,0,0,0,0,0, |
81 |
> |
0,0,0,0,0,0,0,0,0,0, |
82 |
|
ebotch, |
83 |
+ |
0,0, |
84 |
+ |
ebotch, |
85 |
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
86 |
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
87 |
|
epow, |
326 |
|
} |
327 |
|
|
328 |
|
|
329 |
< |
scan() /* scan next character */ |
329 |
> |
getscanpos(fnp, lnp, spp, fpp) /* return current scan position */ |
330 |
> |
char **fnp; |
331 |
> |
int *lnp; |
332 |
> |
char **spp; |
333 |
> |
FILE **fpp; |
334 |
|
{ |
335 |
+ |
if (fnp != NULL) *fnp = infile; |
336 |
+ |
if (lnp != NULL) *lnp = lineno; |
337 |
+ |
if (spp != NULL) *spp = linbuf+linepos; |
338 |
+ |
if (fpp != NULL) *fpp = infp; |
339 |
+ |
} |
340 |
+ |
|
341 |
+ |
|
342 |
+ |
int |
343 |
+ |
scan() /* scan next character, return literal next */ |
344 |
+ |
{ |
345 |
+ |
register int lnext = 0; |
346 |
+ |
|
347 |
|
do { |
348 |
|
if (linbuf[linepos] == '\0') |
349 |
|
if (infp == NULL || fgets(linbuf, MAXLINE, infp) == NULL) |
355 |
|
} |
356 |
|
else |
357 |
|
nextc = linbuf[linepos++]; |
358 |
+ |
if (!lnext) |
359 |
+ |
lnext = nextc; |
360 |
|
if (nextc == '{') { |
361 |
|
scan(); |
362 |
|
while (nextc != '}') |
367 |
|
scan(); |
368 |
|
} |
369 |
|
} while (isspace(nextc)); |
370 |
+ |
return(lnext); |
371 |
|
} |
372 |
|
|
373 |
|
|
402 |
|
{ |
403 |
|
register int i; |
404 |
|
|
384 |
– |
eputs(linbuf); |
385 |
– |
if (linbuf[strlen(linbuf)-1] != '\n') |
386 |
– |
eputs("\n"); |
387 |
– |
for (i = 0; i < linepos-1; i++) |
388 |
– |
eputs(linbuf[i] == '\t' ? "\t" : " "); |
389 |
– |
eputs("^ "); |
405 |
|
if (infile != NULL || lineno != 0) { |
391 |
– |
eputs("\n"); |
406 |
|
if (infile != NULL) eputs(infile); |
407 |
|
if (lineno != 0) { |
408 |
|
eputs(infile != NULL ? ", line " : "line "); |
409 |
|
eputs(ltoa((long)lineno)); |
410 |
|
} |
411 |
< |
eputs(": "); |
411 |
> |
eputs(": syntax error:\n"); |
412 |
|
} |
413 |
+ |
eputs(linbuf); |
414 |
+ |
if (linbuf[strlen(linbuf)-1] != '\n') |
415 |
+ |
eputs("\n"); |
416 |
+ |
for (i = 0; i < linepos-1; i++) |
417 |
+ |
eputs(linbuf[i] == '\t' ? "\t" : " "); |
418 |
+ |
eputs("^ "); |
419 |
|
eputs(err); |
420 |
|
eputs("\n"); |
421 |
|
quit(1); |
441 |
|
getname() /* scan an identifier */ |
442 |
|
{ |
443 |
|
static char str[MAXWORD+1]; |
444 |
< |
register int i; |
444 |
> |
register int i, lnext; |
445 |
|
|
446 |
< |
for (i = 0; i < MAXWORD && isid(nextc); i++, scan()) |
447 |
< |
str[i] = nextc; |
446 |
> |
lnext = nextc; |
447 |
> |
for (i = 0; i < MAXWORD && isid(lnext); i++, lnext = scan()) |
448 |
> |
str[i] = lnext; |
449 |
|
str[i] = '\0'; |
450 |
+ |
while (isid(lnext)) /* skip rest of name */ |
451 |
+ |
lnext = scan(); |
452 |
|
|
453 |
|
return(str); |
454 |
|
} |
457 |
|
int |
458 |
|
getinum() /* scan a positive integer */ |
459 |
|
{ |
460 |
< |
register int n; |
460 |
> |
register int n, lnext; |
461 |
|
|
462 |
|
n = 0; |
463 |
< |
while (isdigit(nextc)) { |
464 |
< |
n = n * 10 + nextc - '0'; |
465 |
< |
scan(); |
463 |
> |
lnext = nextc; |
464 |
> |
while (isdigit(lnext)) { |
465 |
> |
n = n * 10 + lnext - '0'; |
466 |
> |
lnext = scan(); |
467 |
|
} |
468 |
|
return(n); |
469 |
|
} |
472 |
|
double |
473 |
|
getnum() /* scan a positive float */ |
474 |
|
{ |
475 |
< |
register int i; |
475 |
> |
register int i, lnext; |
476 |
|
char str[MAXWORD+1]; |
477 |
|
|
478 |
|
i = 0; |
479 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
480 |
< |
str[i++] = nextc; |
481 |
< |
scan(); |
479 |
> |
lnext = nextc; |
480 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
481 |
> |
str[i++] = lnext; |
482 |
> |
lnext = scan(); |
483 |
|
} |
484 |
< |
if (nextc == '.' && i < MAXWORD) { |
485 |
< |
str[i++] = nextc; |
486 |
< |
scan(); |
487 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
488 |
< |
str[i++] = nextc; |
489 |
< |
scan(); |
484 |
> |
if (lnext == '.' && i < MAXWORD) { |
485 |
> |
str[i++] = lnext; |
486 |
> |
lnext = scan(); |
487 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
488 |
> |
str[i++] = lnext; |
489 |
> |
lnext = scan(); |
490 |
|
} |
491 |
|
} |
492 |
< |
if ((nextc == 'e' || nextc == 'E') && i < MAXWORD) { |
493 |
< |
str[i++] = nextc; |
494 |
< |
scan(); |
495 |
< |
if ((nextc == '-' || nextc == '+') && i < MAXWORD) { |
496 |
< |
str[i++] = nextc; |
497 |
< |
scan(); |
492 |
> |
if ((lnext == 'e' || lnext == 'E') && i < MAXWORD) { |
493 |
> |
str[i++] = lnext; |
494 |
> |
lnext = scan(); |
495 |
> |
if ((lnext == '-' || lnext == '+') && i < MAXWORD) { |
496 |
> |
str[i++] = lnext; |
497 |
> |
lnext = scan(); |
498 |
|
} |
499 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
500 |
< |
str[i++] = nextc; |
501 |
< |
scan(); |
499 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
500 |
> |
str[i++] = lnext; |
501 |
> |
lnext = scan(); |
502 |
|
} |
503 |
|
} |
504 |
|
str[i] = '\0'; |
554 |
|
|
555 |
|
|
556 |
|
EPNODE * |
557 |
< |
getE3() /* E3 -> E3 ^ E4 */ |
557 |
> |
getE3() /* E3 -> E4 ^ E3 */ |
558 |
|
/* E4 */ |
559 |
|
{ |
560 |
|
register EPNODE *ep1, *ep2; |
561 |
|
|
562 |
|
ep1 = getE4(); |
563 |
< |
while (nextc == '^') { |
563 |
> |
if (nextc == '^') { |
564 |
|
ep2 = newnode(); |
565 |
|
ep2->type = nextc; |
566 |
|
scan(); |
567 |
|
addekid(ep2, ep1); |
568 |
< |
addekid(ep2, getE4()); |
568 |
> |
addekid(ep2, getE3()); |
569 |
|
#ifdef RCONST |
570 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
571 |
|
ep2 = rconst(ep2); |
572 |
|
#endif |
573 |
< |
ep1 = ep2; |
573 |
> |
return(ep2); |
574 |
|
} |
575 |
|
return(ep1); |
576 |
|
} |
589 |
|
ep2->v.num = -ep2->v.num; |
590 |
|
return(ep2); |
591 |
|
} |
592 |
+ |
if (ep2->type == UMINUS) { /* don't generate -(-E5) */ |
593 |
+ |
efree((char *)ep2); |
594 |
+ |
return(ep2->v.kid); |
595 |
+ |
} |
596 |
|
ep1 = newnode(); |
597 |
|
ep1->type = UMINUS; |
598 |
|
addekid(ep1, ep2); |
613 |
|
/* ARG */ |
614 |
|
{ |
615 |
|
int i; |
616 |
+ |
char *nam; |
617 |
|
register EPNODE *ep1, *ep2; |
618 |
|
|
619 |
|
if (nextc == '(') { |
636 |
|
#endif |
637 |
|
|
638 |
|
#if defined(VARIABLE) || defined(FUNCTION) |
639 |
< |
if (isalpha(nextc)) { |
640 |
< |
ep1 = newnode(); |
611 |
< |
ep1->type = VAR; |
612 |
< |
ep1->v.ln = varinsert(getname()); |
613 |
< |
|
639 |
> |
if (isalpha(nextc) || nextc == CNTXMARK) { |
640 |
> |
nam = getname(); |
641 |
|
#if defined(VARIABLE) && defined(FUNCTION) |
642 |
+ |
ep1 = NULL; |
643 |
|
if (curfunc != NULL) |
644 |
|
for (i = 1, ep2 = curfunc->v.kid->sibling; |
645 |
|
ep2 != NULL; i++, ep2 = ep2->sibling) |
646 |
< |
if (!strcmp(ep2->v.name, ep1->v.ln->name)) { |
619 |
< |
epfree(ep1); |
646 |
> |
if (!strcmp(ep2->v.name, nam)) { |
647 |
|
ep1 = newnode(); |
648 |
|
ep1->type = ARG; |
649 |
|
ep1->v.chan = i; |
650 |
|
break; |
651 |
|
} |
652 |
+ |
if (ep1 == NULL) |
653 |
|
#endif |
654 |
+ |
{ |
655 |
+ |
ep1 = newnode(); |
656 |
+ |
ep1->type = VAR; |
657 |
+ |
ep1->v.ln = varinsert(nam); |
658 |
+ |
} |
659 |
|
#ifdef FUNCTION |
660 |
|
if (nextc == '(') { |
661 |
|
ep2 = newnode(); |
675 |
|
syntax("'(' expected"); |
676 |
|
#endif |
677 |
|
#endif |
678 |
+ |
#ifdef RCONST |
679 |
+ |
if (isconstvar(ep1)) |
680 |
+ |
ep1 = rconst(ep1); |
681 |
+ |
#endif |
682 |
|
return(ep1); |
683 |
|
} |
684 |
|
#endif |
710 |
|
|
711 |
|
return(ep); |
712 |
|
} |
713 |
+ |
|
714 |
+ |
|
715 |
+ |
isconstvar(ep) /* is ep linked to a constant expression? */ |
716 |
+ |
register EPNODE *ep; |
717 |
+ |
{ |
718 |
+ |
#ifdef VARIABLE |
719 |
+ |
register EPNODE *ep1; |
720 |
+ |
#ifdef FUNCTION |
721 |
+ |
|
722 |
+ |
if (ep->type == FUNC) { |
723 |
+ |
if (!isconstfun(ep->v.kid)) |
724 |
+ |
return(0); |
725 |
+ |
for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) |
726 |
+ |
if (ep1->type != NUM && !isconstfun(ep1)) |
727 |
+ |
return(0); |
728 |
+ |
return(1); |
729 |
+ |
} |
730 |
+ |
#endif |
731 |
+ |
if (ep->type != VAR) |
732 |
+ |
return(0); |
733 |
+ |
ep1 = ep->v.ln->def; |
734 |
+ |
if (ep1 == NULL || ep1->type != ':') |
735 |
+ |
return(0); |
736 |
+ |
#ifdef FUNCTION |
737 |
+ |
if (ep1->v.kid->type != SYM) |
738 |
+ |
return(0); |
739 |
+ |
#endif |
740 |
+ |
return(1); |
741 |
+ |
#else |
742 |
+ |
return(ep->type == FUNC); |
743 |
+ |
#endif |
744 |
+ |
} |
745 |
+ |
|
746 |
+ |
|
747 |
+ |
#if defined(FUNCTION) && defined(VARIABLE) |
748 |
+ |
isconstfun(ep) /* is ep linked to a constant function? */ |
749 |
+ |
register EPNODE *ep; |
750 |
+ |
{ |
751 |
+ |
register EPNODE *dp; |
752 |
+ |
register LIBR *lp; |
753 |
+ |
|
754 |
+ |
if (ep->type != VAR) |
755 |
+ |
return(0); |
756 |
+ |
if ((dp = ep->v.ln->def) != NULL && dp->v.kid->type == FUNC) |
757 |
+ |
return(dp->type == ':'); |
758 |
+ |
if ((lp = liblookup(ep->v.ln->name)) != NULL) |
759 |
+ |
return(lp->atyp == ':'); |
760 |
+ |
return(0); |
761 |
+ |
} |
762 |
+ |
#endif |
763 |
|
#endif |