| 39 |
|
extern char *fgets(), *savestr(); |
| 40 |
|
extern char *emalloc(), *ecalloc(); |
| 41 |
|
extern EPNODE *curfunc; |
| 42 |
< |
extern double efunc(), evariable(), enumber(), euminus(), echannel(); |
| 43 |
< |
extern double eargument(), eadd(), esubtr(), emult(), edivi(), epow(); |
| 44 |
< |
extern double ebotch(); |
| 42 |
> |
extern double efunc(), evariable(); |
| 43 |
> |
static double euminus(), echannel(), eargument(), enumber(); |
| 44 |
> |
static double eadd(), esubtr(), emult(), edivi(), epow(); |
| 45 |
> |
static double ebotch(); |
| 46 |
|
extern int errno; |
| 47 |
|
|
| 48 |
|
int nextc; /* lookahead character */ |
| 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, |
| 88 |
|
}; |
| 89 |
|
|
| 87 |
– |
static char *infile; /* input file name */ |
| 90 |
|
static FILE *infp; /* input file pointer */ |
| 91 |
|
static char *linbuf; /* line buffer */ |
| 92 |
+ |
static char *infile; /* input file name */ |
| 93 |
+ |
static int lineno; /* input line number */ |
| 94 |
|
static int linepos; /* position in buffer */ |
| 95 |
|
|
| 96 |
|
|
| 100 |
|
{ |
| 101 |
|
EPNODE *ep; |
| 102 |
|
|
| 103 |
< |
initstr(NULL, expr); |
| 103 |
> |
initstr(expr, NULL, 0); |
| 104 |
|
#if defined(VARIABLE) && defined(FUNCTION) |
| 105 |
|
curfunc = NULL; |
| 106 |
|
#endif |
| 132 |
|
|
| 133 |
|
switch (epar->type) { |
| 134 |
|
|
| 135 |
+ |
#if defined(VARIABLE) || defined(FUNCTION) |
| 136 |
|
case VAR: |
| 137 |
|
varfree(epar->v.ln); |
| 138 |
|
break; |
| 139 |
+ |
#endif |
| 140 |
|
|
| 141 |
|
case SYM: |
| 142 |
|
freestr(epar->v.name); |
| 295 |
|
} |
| 296 |
|
|
| 297 |
|
|
| 298 |
< |
initfile(file, fp) /* prepare input file */ |
| 293 |
< |
char *file; |
| 298 |
> |
initfile(fp, fn, ln) /* prepare input file */ |
| 299 |
|
FILE *fp; |
| 300 |
+ |
char *fn; |
| 301 |
+ |
int ln; |
| 302 |
|
{ |
| 303 |
|
static char inpbuf[MAXLINE]; |
| 304 |
|
|
| 298 |
– |
infile = file; |
| 305 |
|
infp = fp; |
| 306 |
|
linbuf = inpbuf; |
| 307 |
+ |
infile = fn; |
| 308 |
+ |
lineno = ln; |
| 309 |
|
linepos = 0; |
| 310 |
|
inpbuf[0] = '\0'; |
| 311 |
|
scan(); |
| 312 |
|
} |
| 313 |
|
|
| 314 |
|
|
| 315 |
< |
initstr(file, s) /* prepare input string */ |
| 308 |
< |
char *file; |
| 315 |
> |
initstr(s, fn, ln) /* prepare input string */ |
| 316 |
|
char *s; |
| 317 |
+ |
char *fn; |
| 318 |
+ |
int ln; |
| 319 |
|
{ |
| 311 |
– |
infile = file; |
| 320 |
|
infp = NULL; |
| 321 |
+ |
infile = fn; |
| 322 |
+ |
lineno = ln; |
| 323 |
|
linbuf = s; |
| 324 |
|
linepos = 0; |
| 325 |
|
scan(); |
| 334 |
|
nextc = EOF; |
| 335 |
|
else { |
| 336 |
|
nextc = linbuf[0]; |
| 337 |
+ |
lineno++; |
| 338 |
|
linepos = 1; |
| 339 |
|
} |
| 340 |
|
else |
| 352 |
|
} |
| 353 |
|
|
| 354 |
|
|
| 355 |
+ |
char * |
| 356 |
+ |
ltoa(l) /* convert long to ascii */ |
| 357 |
+ |
long l; |
| 358 |
+ |
{ |
| 359 |
+ |
static char buf[16]; |
| 360 |
+ |
register char *cp; |
| 361 |
+ |
int neg = 0; |
| 362 |
+ |
|
| 363 |
+ |
if (l == 0) |
| 364 |
+ |
return("0"); |
| 365 |
+ |
if (l < 0) { |
| 366 |
+ |
l = -l; |
| 367 |
+ |
neg++; |
| 368 |
+ |
} |
| 369 |
+ |
cp = buf + sizeof(buf); |
| 370 |
+ |
*--cp = '\0'; |
| 371 |
+ |
while (l) { |
| 372 |
+ |
*--cp = l % 10 + '0'; |
| 373 |
+ |
l /= 10; |
| 374 |
+ |
} |
| 375 |
+ |
if (neg) |
| 376 |
+ |
*--cp = '-'; |
| 377 |
+ |
return(cp); |
| 378 |
+ |
} |
| 379 |
+ |
|
| 380 |
+ |
|
| 381 |
|
syntax(err) /* report syntax error and quit */ |
| 382 |
|
char *err; |
| 383 |
|
{ |
| 384 |
|
register int i; |
| 385 |
|
|
| 386 |
+ |
if (infile != NULL || lineno != 0) { |
| 387 |
+ |
if (infile != NULL) eputs(infile); |
| 388 |
+ |
if (lineno != 0) { |
| 389 |
+ |
eputs(infile != NULL ? ", line " : "line "); |
| 390 |
+ |
eputs(ltoa((long)lineno)); |
| 391 |
+ |
} |
| 392 |
+ |
eputs(": syntax error:\n"); |
| 393 |
+ |
} |
| 394 |
|
eputs(linbuf); |
| 395 |
< |
if (linbuf[0] == '\0' || linbuf[strlen(linbuf)-1] != '\n') |
| 395 |
> |
if (linbuf[strlen(linbuf)-1] != '\n') |
| 396 |
|
eputs("\n"); |
| 397 |
|
for (i = 0; i < linepos-1; i++) |
| 398 |
|
eputs(linbuf[i] == '\t' ? "\t" : " "); |
| 399 |
|
eputs("^ "); |
| 355 |
– |
if (infile != NULL) { |
| 356 |
– |
eputs(infile); |
| 357 |
– |
eputs(": "); |
| 358 |
– |
} |
| 400 |
|
eputs(err); |
| 401 |
|
eputs("\n"); |
| 402 |
|
quit(1); |
| 530 |
|
|
| 531 |
|
|
| 532 |
|
EPNODE * |
| 533 |
< |
getE3() /* E3 -> E3 ^ E4 */ |
| 533 |
> |
getE3() /* E3 -> E4 ^ E3 */ |
| 534 |
|
/* E4 */ |
| 535 |
|
{ |
| 536 |
|
register EPNODE *ep1, *ep2; |
| 537 |
|
|
| 538 |
|
ep1 = getE4(); |
| 539 |
< |
while (nextc == '^') { |
| 539 |
> |
if (nextc == '^') { |
| 540 |
|
ep2 = newnode(); |
| 541 |
|
ep2->type = nextc; |
| 542 |
|
scan(); |
| 543 |
|
addekid(ep2, ep1); |
| 544 |
< |
addekid(ep2, getE4()); |
| 544 |
> |
addekid(ep2, getE3()); |
| 545 |
|
#ifdef RCONST |
| 546 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
| 547 |
|
ep2 = rconst(ep2); |
| 548 |
|
#endif |
| 549 |
< |
ep1 = ep2; |
| 549 |
> |
return(ep2); |
| 550 |
|
} |
| 551 |
|
return(ep1); |
| 552 |
|
} |
| 556 |
|
getE4() /* E4 -> ADDOP E5 */ |
| 557 |
|
/* E5 */ |
| 558 |
|
{ |
| 559 |
< |
register EPNODE *ep1; |
| 559 |
> |
register EPNODE *ep1, *ep2; |
| 560 |
|
|
| 561 |
|
if (nextc == '-') { |
| 562 |
|
scan(); |
| 563 |
< |
ep1 = newnode(); |
| 564 |
< |
#ifndef RCONST |
| 565 |
< |
if (isdecimal(nextc)) { |
| 566 |
< |
ep1->type = NUM; |
| 526 |
< |
ep1->v.num = -getnum(); |
| 527 |
< |
return(ep1); |
| 563 |
> |
ep2 = getE5(); |
| 564 |
> |
if (ep2->type == NUM) { |
| 565 |
> |
ep2->v.num = -ep2->v.num; |
| 566 |
> |
return(ep2); |
| 567 |
|
} |
| 568 |
< |
#endif |
| 568 |
> |
ep1 = newnode(); |
| 569 |
|
ep1->type = UMINUS; |
| 570 |
< |
addekid(ep1, getE5()); |
| 532 |
< |
#ifdef RCONST |
| 533 |
< |
if (ep1->v.kid->type == NUM) |
| 534 |
< |
ep1 = rconst(ep1); |
| 535 |
< |
#endif |
| 570 |
> |
addekid(ep1, ep2); |
| 571 |
|
return(ep1); |
| 572 |
|
} |
| 573 |
|
if (nextc == '+') |
| 643 |
|
syntax("'(' expected"); |
| 644 |
|
#endif |
| 645 |
|
#endif |
| 646 |
+ |
#ifdef RCONST |
| 647 |
+ |
if (isconstvar(ep1)) |
| 648 |
+ |
ep1 = rconst(ep1); |
| 649 |
+ |
#endif |
| 650 |
|
return(ep1); |
| 651 |
|
} |
| 652 |
|
#endif |
| 677 |
|
epfree(epar); |
| 678 |
|
|
| 679 |
|
return(ep); |
| 680 |
+ |
} |
| 681 |
+ |
|
| 682 |
+ |
|
| 683 |
+ |
isconstvar(ep) /* is ep linked to a constant? */ |
| 684 |
+ |
register EPNODE *ep; |
| 685 |
+ |
{ |
| 686 |
+ |
#ifdef VARIABLE |
| 687 |
+ |
register EPNODE *ep1; |
| 688 |
+ |
|
| 689 |
+ |
#ifdef FUNCTION |
| 690 |
+ |
if (ep->type == FUNC) { |
| 691 |
+ |
if (ep->v.kid->type != VAR) |
| 692 |
+ |
return(0); |
| 693 |
+ |
ep1 = ep->v.kid->v.ln->def; |
| 694 |
+ |
if (ep1 != NULL && ep1->type != ':') |
| 695 |
+ |
return(0); |
| 696 |
+ |
if ((ep1 == NULL || ep1->v.kid->type != FUNC) |
| 697 |
+ |
&& liblookup(ep->v.kid->v.ln->name) == NULL) |
| 698 |
+ |
return(0); |
| 699 |
+ |
for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) |
| 700 |
+ |
if (ep1->type != NUM) |
| 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 |
|
#endif |