--- ray/src/common/calexpr.c 1989/06/14 22:29:01 1.3 +++ ray/src/common/calexpr.c 1991/04/23 16:56:39 1.11 @@ -39,9 +39,10 @@ extern double atof(), pow(); extern char *fgets(), *savestr(); extern char *emalloc(), *ecalloc(); extern EPNODE *curfunc; -extern double efunc(), evariable(), enumber(), euminus(), echannel(); -extern double eargument(), eadd(), esubtr(), emult(), edivi(), epow(); -extern double ebotch(); +extern double efunc(), evariable(); +static double euminus(), echannel(), eargument(), enumber(); +static double eadd(), esubtr(), emult(), edivi(), epow(); +static double ebotch(); extern int errno; int nextc; /* lookahead character */ @@ -77,16 +78,19 @@ double (*eoper[])() = { /* expression operations */ esubtr, 0, edivi, - 0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, ebotch, + 0,0, + ebotch, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, epow, }; -static char *infile; /* input file name */ static FILE *infp; /* input file pointer */ static char *linbuf; /* line buffer */ +static char *infile; /* input file name */ +static int lineno; /* input line number */ static int linepos; /* position in buffer */ @@ -96,7 +100,7 @@ char *expr; { EPNODE *ep; - initstr(NULL, expr); + initstr(expr, NULL, 0); #if defined(VARIABLE) && defined(FUNCTION) curfunc = NULL; #endif @@ -291,27 +295,31 @@ register EPNODE *ep; } -initfile(file, fp) /* prepare input file */ -char *file; +initfile(fp, fn, ln) /* prepare input file */ FILE *fp; +char *fn; +int ln; { static char inpbuf[MAXLINE]; - infile = file; infp = fp; linbuf = inpbuf; + infile = fn; + lineno = ln; linepos = 0; inpbuf[0] = '\0'; scan(); } -initstr(file, s) /* prepare input string */ -char *file; +initstr(s, fn, ln) /* prepare input string */ char *s; +char *fn; +int ln; { - infile = file; infp = NULL; + infile = fn; + lineno = ln; linbuf = s; linepos = 0; scan(); @@ -326,6 +334,7 @@ scan() /* scan next character */ nextc = EOF; else { nextc = linbuf[0]; + lineno++; linepos = 1; } else @@ -343,21 +352,51 @@ scan() /* scan next character */ } +char * +ltoa(l) /* convert long to ascii */ +long l; +{ + static char buf[16]; + register char *cp; + int neg = 0; + + if (l == 0) + return("0"); + if (l < 0) { + l = -l; + neg++; + } + cp = buf + sizeof(buf); + *--cp = '\0'; + while (l) { + *--cp = l % 10 + '0'; + l /= 10; + } + if (neg) + *--cp = '-'; + return(cp); +} + + syntax(err) /* report syntax error and quit */ char *err; { register int i; + if (infile != NULL || lineno != 0) { + if (infile != NULL) eputs(infile); + if (lineno != 0) { + eputs(infile != NULL ? ", line " : "line "); + eputs(ltoa((long)lineno)); + } + eputs(": syntax error:\n"); + } eputs(linbuf); - if (linbuf[0] == '\0' || linbuf[strlen(linbuf)-1] != '\n') + if (linbuf[strlen(linbuf)-1] != '\n') eputs("\n"); for (i = 0; i < linepos-1; i++) eputs(linbuf[i] == '\t' ? "\t" : " "); eputs("^ "); - if (infile != NULL) { - eputs(infile); - eputs(": "); - } eputs(err); eputs("\n"); quit(1); @@ -491,23 +530,23 @@ getE2() /* E2 -> E2 MULOP E3 */ EPNODE * -getE3() /* E3 -> E3 ^ E4 */ +getE3() /* E3 -> E4 ^ E3 */ /* E4 */ { register EPNODE *ep1, *ep2; ep1 = getE4(); - while (nextc == '^') { + if (nextc == '^') { ep2 = newnode(); ep2->type = nextc; scan(); addekid(ep2, ep1); - addekid(ep2, getE4()); + addekid(ep2, getE3()); #ifdef RCONST if (ep1->type == NUM && ep1->sibling->type == NUM) ep2 = rconst(ep2); #endif - ep1 = ep2; + return(ep2); } return(ep1); } @@ -604,6 +643,10 @@ getE5() /* E5 -> (E1) */ syntax("'(' expected"); #endif #endif +#ifdef RCONST + if (isconstvar(ep1)) + ep1 = rconst(ep1); +#endif return(ep1); } #endif @@ -635,4 +678,57 @@ register EPNODE *epar; return(ep); } + + +isconstvar(ep) /* is ep linked to a constant expression? */ +register EPNODE *ep; +{ +#ifdef VARIABLE + register EPNODE *ep1; +#ifdef FUNCTION + + if (ep->type == FUNC) { + if (!isconstfun(ep->v.kid)) + return(0); + for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) + if (ep1->type != NUM && !isconstfun(ep1)) + return(0); + return(1); + } +#endif + if (ep->type != VAR) + return(0); + ep1 = ep->v.ln->def; + if (ep1 == NULL || ep1->type != ':') + return(0); +#ifdef FUNCTION + if (ep1->v.kid->type != SYM) + return(0); +#endif + return(1); +#else + return(ep->type == FUNC); +#endif +} + + +#if defined(FUNCTION) && defined(VARIABLE) +isconstfun(ep) /* is ep linked to a constant function? */ +register EPNODE *ep; +{ + register EPNODE *dp; + register LIBR *lp; + + if (ep->type != VAR) + return(0); + dp = ep->v.ln->def; + if (dp != NULL && dp->type != ':') + return(0); + if ((dp == NULL || dp->v.kid->type != FUNC) + && ((lp = liblookup(ep->v.ln->name)) == NULL + || lp->atyp != ':')) + return(0); + return(1); +} +#endif #endif