| 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 |
> |
int |
| 327 |
> |
scan() /* scan next character, return literal next */ |
| 328 |
|
{ |
| 329 |
+ |
register int lnext = 0; |
| 330 |
+ |
|
| 331 |
|
do { |
| 332 |
|
if (linbuf[linepos] == '\0') |
| 333 |
|
if (infp == NULL || fgets(linbuf, MAXLINE, infp) == NULL) |
| 339 |
|
} |
| 340 |
|
else |
| 341 |
|
nextc = linbuf[linepos++]; |
| 342 |
+ |
if (!lnext) |
| 343 |
+ |
lnext = nextc; |
| 344 |
|
if (nextc == '{') { |
| 345 |
|
scan(); |
| 346 |
|
while (nextc != '}') |
| 351 |
|
scan(); |
| 352 |
|
} |
| 353 |
|
} while (isspace(nextc)); |
| 354 |
+ |
return(lnext); |
| 355 |
|
} |
| 356 |
|
|
| 357 |
|
|
| 386 |
|
{ |
| 387 |
|
register int i; |
| 388 |
|
|
| 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("^ "); |
| 389 |
|
if (infile != NULL || lineno != 0) { |
| 391 |
– |
eputs("\n"); |
| 390 |
|
if (infile != NULL) eputs(infile); |
| 391 |
|
if (lineno != 0) { |
| 392 |
|
eputs(infile != NULL ? ", line " : "line "); |
| 393 |
|
eputs(ltoa((long)lineno)); |
| 394 |
|
} |
| 395 |
< |
eputs(": "); |
| 395 |
> |
eputs(": syntax error:\n"); |
| 396 |
|
} |
| 397 |
+ |
eputs(linbuf); |
| 398 |
+ |
if (linbuf[strlen(linbuf)-1] != '\n') |
| 399 |
+ |
eputs("\n"); |
| 400 |
+ |
for (i = 0; i < linepos-1; i++) |
| 401 |
+ |
eputs(linbuf[i] == '\t' ? "\t" : " "); |
| 402 |
+ |
eputs("^ "); |
| 403 |
|
eputs(err); |
| 404 |
|
eputs("\n"); |
| 405 |
|
quit(1); |
| 425 |
|
getname() /* scan an identifier */ |
| 426 |
|
{ |
| 427 |
|
static char str[MAXWORD+1]; |
| 428 |
< |
register int i; |
| 428 |
> |
register int i, lnext; |
| 429 |
|
|
| 430 |
< |
for (i = 0; i < MAXWORD && isid(nextc); i++, scan()) |
| 431 |
< |
str[i] = nextc; |
| 430 |
> |
lnext = nextc; |
| 431 |
> |
for (i = 0; i < MAXWORD && isid(lnext); i++, lnext = scan()) |
| 432 |
> |
str[i] = lnext; |
| 433 |
|
str[i] = '\0'; |
| 434 |
|
|
| 435 |
|
return(str); |
| 439 |
|
int |
| 440 |
|
getinum() /* scan a positive integer */ |
| 441 |
|
{ |
| 442 |
< |
register int n; |
| 442 |
> |
register int n, lnext; |
| 443 |
|
|
| 444 |
|
n = 0; |
| 445 |
< |
while (isdigit(nextc)) { |
| 446 |
< |
n = n * 10 + nextc - '0'; |
| 447 |
< |
scan(); |
| 445 |
> |
lnext = nextc; |
| 446 |
> |
while (isdigit(lnext)) { |
| 447 |
> |
n = n * 10 + lnext - '0'; |
| 448 |
> |
lnext = scan(); |
| 449 |
|
} |
| 450 |
|
return(n); |
| 451 |
|
} |
| 454 |
|
double |
| 455 |
|
getnum() /* scan a positive float */ |
| 456 |
|
{ |
| 457 |
< |
register int i; |
| 457 |
> |
register int i, lnext; |
| 458 |
|
char str[MAXWORD+1]; |
| 459 |
|
|
| 460 |
|
i = 0; |
| 461 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
| 462 |
< |
str[i++] = nextc; |
| 463 |
< |
scan(); |
| 461 |
> |
lnext = nextc; |
| 462 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
| 463 |
> |
str[i++] = lnext; |
| 464 |
> |
lnext = scan(); |
| 465 |
|
} |
| 466 |
< |
if (nextc == '.' && i < MAXWORD) { |
| 467 |
< |
str[i++] = nextc; |
| 468 |
< |
scan(); |
| 469 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
| 470 |
< |
str[i++] = nextc; |
| 471 |
< |
scan(); |
| 466 |
> |
if (lnext == '.' && i < MAXWORD) { |
| 467 |
> |
str[i++] = lnext; |
| 468 |
> |
lnext = scan(); |
| 469 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
| 470 |
> |
str[i++] = lnext; |
| 471 |
> |
lnext = scan(); |
| 472 |
|
} |
| 473 |
|
} |
| 474 |
< |
if ((nextc == 'e' || nextc == 'E') && i < MAXWORD) { |
| 475 |
< |
str[i++] = nextc; |
| 476 |
< |
scan(); |
| 477 |
< |
if ((nextc == '-' || nextc == '+') && i < MAXWORD) { |
| 478 |
< |
str[i++] = nextc; |
| 479 |
< |
scan(); |
| 474 |
> |
if ((lnext == 'e' || lnext == 'E') && i < MAXWORD) { |
| 475 |
> |
str[i++] = lnext; |
| 476 |
> |
lnext = scan(); |
| 477 |
> |
if ((lnext == '-' || lnext == '+') && i < MAXWORD) { |
| 478 |
> |
str[i++] = lnext; |
| 479 |
> |
lnext = scan(); |
| 480 |
|
} |
| 481 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
| 482 |
< |
str[i++] = nextc; |
| 483 |
< |
scan(); |
| 481 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
| 482 |
> |
str[i++] = lnext; |
| 483 |
> |
lnext = scan(); |
| 484 |
|
} |
| 485 |
|
} |
| 486 |
|
str[i] = '\0'; |
| 536 |
|
|
| 537 |
|
|
| 538 |
|
EPNODE * |
| 539 |
< |
getE3() /* E3 -> E3 ^ E4 */ |
| 539 |
> |
getE3() /* E3 -> E4 ^ E3 */ |
| 540 |
|
/* E4 */ |
| 541 |
|
{ |
| 542 |
|
register EPNODE *ep1, *ep2; |
| 543 |
|
|
| 544 |
|
ep1 = getE4(); |
| 545 |
< |
while (nextc == '^') { |
| 545 |
> |
if (nextc == '^') { |
| 546 |
|
ep2 = newnode(); |
| 547 |
|
ep2->type = nextc; |
| 548 |
|
scan(); |
| 549 |
|
addekid(ep2, ep1); |
| 550 |
< |
addekid(ep2, getE4()); |
| 550 |
> |
addekid(ep2, getE3()); |
| 551 |
|
#ifdef RCONST |
| 552 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
| 553 |
|
ep2 = rconst(ep2); |
| 554 |
|
#endif |
| 555 |
< |
ep1 = ep2; |
| 555 |
> |
return(ep2); |
| 556 |
|
} |
| 557 |
|
return(ep1); |
| 558 |
|
} |
| 649 |
|
syntax("'(' expected"); |
| 650 |
|
#endif |
| 651 |
|
#endif |
| 652 |
+ |
#ifdef RCONST |
| 653 |
+ |
if (isconstvar(ep1)) |
| 654 |
+ |
ep1 = rconst(ep1); |
| 655 |
+ |
#endif |
| 656 |
|
return(ep1); |
| 657 |
|
} |
| 658 |
|
#endif |
| 684 |
|
|
| 685 |
|
return(ep); |
| 686 |
|
} |
| 687 |
+ |
|
| 688 |
+ |
|
| 689 |
+ |
isconstvar(ep) /* is ep linked to a constant expression? */ |
| 690 |
+ |
register EPNODE *ep; |
| 691 |
+ |
{ |
| 692 |
+ |
#ifdef VARIABLE |
| 693 |
+ |
register EPNODE *ep1; |
| 694 |
+ |
#ifdef FUNCTION |
| 695 |
+ |
|
| 696 |
+ |
if (ep->type == FUNC) { |
| 697 |
+ |
if (!isconstfun(ep->v.kid)) |
| 698 |
+ |
return(0); |
| 699 |
+ |
for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) |
| 700 |
+ |
if (ep1->type != NUM && !isconstfun(ep1)) |
| 701 |
+ |
return(0); |
| 702 |
+ |
return(1); |
| 703 |
+ |
} |
| 704 |
+ |
#endif |
| 705 |
+ |
if (ep->type != VAR) |
| 706 |
+ |
return(0); |
| 707 |
+ |
ep1 = ep->v.ln->def; |
| 708 |
+ |
if (ep1 == NULL || ep1->type != ':') |
| 709 |
+ |
return(0); |
| 710 |
+ |
#ifdef FUNCTION |
| 711 |
+ |
if (ep1->v.kid->type != SYM) |
| 712 |
+ |
return(0); |
| 713 |
+ |
#endif |
| 714 |
+ |
return(1); |
| 715 |
+ |
#else |
| 716 |
+ |
return(ep->type == FUNC); |
| 717 |
+ |
#endif |
| 718 |
+ |
} |
| 719 |
+ |
|
| 720 |
+ |
|
| 721 |
+ |
#if defined(FUNCTION) && defined(VARIABLE) |
| 722 |
+ |
isconstfun(ep) /* is ep linked to a constant function? */ |
| 723 |
+ |
register EPNODE *ep; |
| 724 |
+ |
{ |
| 725 |
+ |
register EPNODE *dp; |
| 726 |
+ |
register LIBR *lp; |
| 727 |
+ |
|
| 728 |
+ |
if (ep->type != VAR) |
| 729 |
+ |
return(0); |
| 730 |
+ |
dp = ep->v.ln->def; |
| 731 |
+ |
if (dp != NULL && dp->type != ':') |
| 732 |
+ |
return(0); |
| 733 |
+ |
if ((dp == NULL || dp->v.kid->type != FUNC) |
| 734 |
+ |
&& ((lp = liblookup(ep->v.ln->name)) == NULL |
| 735 |
+ |
|| lp->atyp != ':')) |
| 736 |
+ |
return(0); |
| 737 |
+ |
return(1); |
| 738 |
+ |
} |
| 739 |
+ |
#endif |
| 740 |
|
#endif |