--- ray/src/common/calexpr.c 1989/06/29 09:44:33 1.4 +++ ray/src/common/calexpr.c 1990/08/18 16:28:52 1.7 @@ -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 */ @@ -84,9 +85,10 @@ double (*eoper[])() = { /* expression operations */ 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 +98,7 @@ char *expr; { EPNODE *ep; - initstr(NULL, expr); + initstr(expr, NULL, 0); #if defined(VARIABLE) && defined(FUNCTION) curfunc = NULL; #endif @@ -291,27 +293,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 +332,7 @@ scan() /* scan next character */ nextc = EOF; else { nextc = linbuf[0]; + lineno++; linepos = 1; } else @@ -343,21 +350,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[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);