| 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(); |
| 36 |
|
extern char *fgets(), *savestr(); |
| 37 |
|
extern char *emalloc(), *ecalloc(); |
| 38 |
|
extern EPNODE *curfunc; |
| 39 |
< |
extern double efunc(), evariable(), enumber(), euminus(), echannel(); |
| 40 |
< |
extern double eargument(), eadd(), esubtr(), emult(), edivi(), epow(); |
| 41 |
< |
extern double ebotch(); |
| 39 |
> |
extern double efunc(), evariable(); |
| 40 |
> |
static double euminus(), echannel(), eargument(), enumber(); |
| 41 |
> |
static double eadd(), esubtr(), emult(), edivi(), epow(); |
| 42 |
> |
static double ebotch(); |
| 43 |
|
extern int errno; |
| 44 |
|
|
| 45 |
|
int nextc; /* lookahead character */ |
| 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, |
| 85 |
|
}; |
| 86 |
|
|
| 87 |
– |
static char *infile; /* input file name */ |
| 87 |
|
static FILE *infp; /* input file pointer */ |
| 88 |
|
static char *linbuf; /* line buffer */ |
| 89 |
+ |
static char *infile; /* input file name */ |
| 90 |
+ |
static int lineno; /* input line number */ |
| 91 |
|
static int linepos; /* position in buffer */ |
| 92 |
|
|
| 93 |
|
|
| 97 |
|
{ |
| 98 |
|
EPNODE *ep; |
| 99 |
|
|
| 100 |
< |
initstr(NULL, expr); |
| 100 |
> |
initstr(expr, NULL, 0); |
| 101 |
|
#if defined(VARIABLE) && defined(FUNCTION) |
| 102 |
|
curfunc = NULL; |
| 103 |
|
#endif |
| 292 |
|
} |
| 293 |
|
|
| 294 |
|
|
| 295 |
< |
initfile(file, fp) /* prepare input file */ |
| 295 |
< |
char *file; |
| 295 |
> |
initfile(fp, fn, ln) /* prepare input file */ |
| 296 |
|
FILE *fp; |
| 297 |
+ |
char *fn; |
| 298 |
+ |
int ln; |
| 299 |
|
{ |
| 300 |
|
static char inpbuf[MAXLINE]; |
| 301 |
|
|
| 300 |
– |
infile = file; |
| 302 |
|
infp = fp; |
| 303 |
|
linbuf = inpbuf; |
| 304 |
+ |
infile = fn; |
| 305 |
+ |
lineno = ln; |
| 306 |
|
linepos = 0; |
| 307 |
|
inpbuf[0] = '\0'; |
| 308 |
|
scan(); |
| 309 |
|
} |
| 310 |
|
|
| 311 |
|
|
| 312 |
< |
initstr(file, s) /* prepare input string */ |
| 310 |
< |
char *file; |
| 312 |
> |
initstr(s, fn, ln) /* prepare input string */ |
| 313 |
|
char *s; |
| 314 |
+ |
char *fn; |
| 315 |
+ |
int ln; |
| 316 |
|
{ |
| 313 |
– |
infile = file; |
| 317 |
|
infp = NULL; |
| 318 |
+ |
infile = fn; |
| 319 |
+ |
lineno = ln; |
| 320 |
|
linbuf = s; |
| 321 |
|
linepos = 0; |
| 322 |
|
scan(); |
| 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) |
| 347 |
|
nextc = EOF; |
| 348 |
|
else { |
| 349 |
|
nextc = linbuf[0]; |
| 350 |
+ |
lineno++; |
| 351 |
|
linepos = 1; |
| 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 |
|
|
| 371 |
+ |
char * |
| 372 |
+ |
ltoa(l) /* convert long to ascii */ |
| 373 |
+ |
long l; |
| 374 |
+ |
{ |
| 375 |
+ |
static char buf[16]; |
| 376 |
+ |
register char *cp; |
| 377 |
+ |
int neg = 0; |
| 378 |
+ |
|
| 379 |
+ |
if (l == 0) |
| 380 |
+ |
return("0"); |
| 381 |
+ |
if (l < 0) { |
| 382 |
+ |
l = -l; |
| 383 |
+ |
neg++; |
| 384 |
+ |
} |
| 385 |
+ |
cp = buf + sizeof(buf); |
| 386 |
+ |
*--cp = '\0'; |
| 387 |
+ |
while (l) { |
| 388 |
+ |
*--cp = l % 10 + '0'; |
| 389 |
+ |
l /= 10; |
| 390 |
+ |
} |
| 391 |
+ |
if (neg) |
| 392 |
+ |
*--cp = '-'; |
| 393 |
+ |
return(cp); |
| 394 |
+ |
} |
| 395 |
+ |
|
| 396 |
+ |
|
| 397 |
|
syntax(err) /* report syntax error and quit */ |
| 398 |
|
char *err; |
| 399 |
|
{ |
| 400 |
|
register int i; |
| 401 |
|
|
| 402 |
+ |
if (infile != NULL || lineno != 0) { |
| 403 |
+ |
if (infile != NULL) eputs(infile); |
| 404 |
+ |
if (lineno != 0) { |
| 405 |
+ |
eputs(infile != NULL ? ", line " : "line "); |
| 406 |
+ |
eputs(ltoa((long)lineno)); |
| 407 |
+ |
} |
| 408 |
+ |
eputs(": syntax error:\n"); |
| 409 |
+ |
} |
| 410 |
|
eputs(linbuf); |
| 411 |
< |
if (linbuf[0] == '\0' || linbuf[strlen(linbuf)-1] != '\n') |
| 411 |
> |
if (linbuf[strlen(linbuf)-1] != '\n') |
| 412 |
|
eputs("\n"); |
| 413 |
|
for (i = 0; i < linepos-1; i++) |
| 414 |
|
eputs(linbuf[i] == '\t' ? "\t" : " "); |
| 415 |
|
eputs("^ "); |
| 357 |
– |
if (infile != NULL) { |
| 358 |
– |
eputs(infile); |
| 359 |
– |
eputs(": "); |
| 360 |
– |
} |
| 416 |
|
eputs(err); |
| 417 |
|
eputs("\n"); |
| 418 |
|
quit(1); |
| 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'; |
| 549 |
|
|
| 550 |
|
|
| 551 |
|
EPNODE * |
| 552 |
< |
getE3() /* E3 -> E3 ^ E4 */ |
| 552 |
> |
getE3() /* E3 -> E4 ^ E3 */ |
| 553 |
|
/* E4 */ |
| 554 |
|
{ |
| 555 |
|
register EPNODE *ep1, *ep2; |
| 556 |
|
|
| 557 |
|
ep1 = getE4(); |
| 558 |
< |
while (nextc == '^') { |
| 558 |
> |
if (nextc == '^') { |
| 559 |
|
ep2 = newnode(); |
| 560 |
|
ep2->type = nextc; |
| 561 |
|
scan(); |
| 562 |
|
addekid(ep2, ep1); |
| 563 |
< |
addekid(ep2, getE4()); |
| 563 |
> |
addekid(ep2, getE3()); |
| 564 |
|
#ifdef RCONST |
| 565 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
| 566 |
|
ep2 = rconst(ep2); |
| 567 |
|
#endif |
| 568 |
< |
ep1 = ep2; |
| 568 |
> |
return(ep2); |
| 569 |
|
} |
| 570 |
|
return(ep1); |
| 571 |
|
} |
| 575 |
|
getE4() /* E4 -> ADDOP E5 */ |
| 576 |
|
/* E5 */ |
| 577 |
|
{ |
| 578 |
< |
register EPNODE *ep1; |
| 578 |
> |
register EPNODE *ep1, *ep2; |
| 579 |
|
|
| 580 |
|
if (nextc == '-') { |
| 581 |
|
scan(); |
| 582 |
< |
ep1 = newnode(); |
| 583 |
< |
#ifndef RCONST |
| 584 |
< |
if (isdecimal(nextc)) { |
| 585 |
< |
ep1->type = NUM; |
| 528 |
< |
ep1->v.num = -getnum(); |
| 529 |
< |
return(ep1); |
| 582 |
> |
ep2 = getE5(); |
| 583 |
> |
if (ep2->type == NUM) { |
| 584 |
> |
ep2->v.num = -ep2->v.num; |
| 585 |
> |
return(ep2); |
| 586 |
|
} |
| 587 |
< |
#endif |
| 587 |
> |
ep1 = newnode(); |
| 588 |
|
ep1->type = UMINUS; |
| 589 |
< |
addekid(ep1, getE5()); |
| 534 |
< |
#ifdef RCONST |
| 535 |
< |
if (ep1->v.kid->type == NUM) |
| 536 |
< |
ep1 = rconst(ep1); |
| 537 |
< |
#endif |
| 589 |
> |
addekid(ep1, ep2); |
| 590 |
|
return(ep1); |
| 591 |
|
} |
| 592 |
|
if (nextc == '+') |
| 662 |
|
syntax("'(' expected"); |
| 663 |
|
#endif |
| 664 |
|
#endif |
| 665 |
+ |
#ifdef RCONST |
| 666 |
+ |
if (isconstvar(ep1)) |
| 667 |
+ |
ep1 = rconst(ep1); |
| 668 |
+ |
#endif |
| 669 |
|
return(ep1); |
| 670 |
|
} |
| 671 |
|
#endif |
| 697 |
|
|
| 698 |
|
return(ep); |
| 699 |
|
} |
| 700 |
+ |
|
| 701 |
+ |
|
| 702 |
+ |
isconstvar(ep) /* is ep linked to a constant expression? */ |
| 703 |
+ |
register EPNODE *ep; |
| 704 |
+ |
{ |
| 705 |
+ |
#ifdef VARIABLE |
| 706 |
+ |
register EPNODE *ep1; |
| 707 |
+ |
#ifdef FUNCTION |
| 708 |
+ |
|
| 709 |
+ |
if (ep->type == FUNC) { |
| 710 |
+ |
if (!isconstfun(ep->v.kid)) |
| 711 |
+ |
return(0); |
| 712 |
+ |
for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) |
| 713 |
+ |
if (ep1->type != NUM && !isconstfun(ep1)) |
| 714 |
+ |
return(0); |
| 715 |
+ |
return(1); |
| 716 |
+ |
} |
| 717 |
+ |
#endif |
| 718 |
+ |
if (ep->type != VAR) |
| 719 |
+ |
return(0); |
| 720 |
+ |
ep1 = ep->v.ln->def; |
| 721 |
+ |
if (ep1 == NULL || ep1->type != ':') |
| 722 |
+ |
return(0); |
| 723 |
+ |
#ifdef FUNCTION |
| 724 |
+ |
if (ep1->v.kid->type != SYM) |
| 725 |
+ |
return(0); |
| 726 |
+ |
#endif |
| 727 |
+ |
return(1); |
| 728 |
+ |
#else |
| 729 |
+ |
return(ep->type == FUNC); |
| 730 |
+ |
#endif |
| 731 |
+ |
} |
| 732 |
+ |
|
| 733 |
+ |
|
| 734 |
+ |
#if defined(FUNCTION) && defined(VARIABLE) |
| 735 |
+ |
isconstfun(ep) /* is ep linked to a constant function? */ |
| 736 |
+ |
register EPNODE *ep; |
| 737 |
+ |
{ |
| 738 |
+ |
register EPNODE *dp; |
| 739 |
+ |
register LIBR *lp; |
| 740 |
+ |
|
| 741 |
+ |
if (ep->type != VAR) |
| 742 |
+ |
return(0); |
| 743 |
+ |
dp = ep->v.ln->def; |
| 744 |
+ |
if (dp != NULL && dp->type != ':') |
| 745 |
+ |
return(0); |
| 746 |
+ |
if ((dp == NULL || dp->v.kid->type != FUNC) |
| 747 |
+ |
&& ((lp = liblookup(ep->v.ln->name)) == NULL |
| 748 |
+ |
|| lp->atyp != ':')) |
| 749 |
+ |
return(0); |
| 750 |
+ |
return(1); |
| 751 |
+ |
} |
| 752 |
+ |
#endif |
| 753 |
|
#endif |