diff options
author | Peter Stephenson <pws@zsh.org> | 2015-06-02 10:21:55 +0100 |
---|---|---|
committer | Peter Stephenson <pws@zsh.org> | 2015-06-02 10:21:55 +0100 |
commit | 099e717c1587951458f9b3bd514c655001125f66 (patch) | |
tree | 2260b04957de51bdf4033d9ecd7dad95acbeee13 /Src/math.c | |
parent | aab6bdc3661e83216d2e4bc58948ed21d394d3f5 (diff) | |
download | zsh-099e717c1587951458f9b3bd514c655001125f66.tar.gz zsh-099e717c1587951458f9b3bd514c655001125f66.tar.xz zsh-099e717c1587951458f9b3bd514c655001125f66.zip |
35359: Improved math parsing and errors.
Check for bogus trailing ")" at end of top-level parse. Extend some math error messages to indicate they are math errors.
Diffstat (limited to 'Src/math.c')
-rw-r--r-- | Src/math.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/Src/math.c b/Src/math.c index 97a97b32b..977e92345 100644 --- a/Src/math.c +++ b/Src/math.c @@ -407,6 +407,13 @@ mathevall(char *s, enum prec_type prec_tp, char **ep) stack[0].val.type = MN_INTEGER; stack[0].val.u.l = 0; mathparse(prec_tp == MPREC_TOP ? TOPPREC : ARGPREC); + /* + * Internally, we parse the contents of parentheses at top + * precedence... so we can return a parenthesis here if + * there are too many at the end. + */ + if (mtok == M_OUTPAR && !errflag) + zerr("bad math expression: unexpected ')'"); *ep = ptr; DPUTS(!errflag && sp > 0, "BUG: math: wallabies roaming too freely in outback"); @@ -791,7 +798,7 @@ zzlex(void) ptr++; if (!*ptr) { - zerr("character missing after ##"); + zerr("bad math expression: character missing after ##"); return EOI; } ptr = getkeystring(ptr, NULL, GETKEYS_MATH, &v); @@ -914,7 +921,7 @@ setmathvar(struct mathvalue *mvp, mnumber v) mvp->pval = NULL; } if (!mvp->lval) { - zerr("lvalue required"); + zerr("bad math expression: lvalue required"); v.type = MN_INTEGER; v.u.l = 0; return v; @@ -1256,7 +1263,7 @@ op(int what) /* Error if (-num ** b) and b is not an integer */ double tst = (double)(zlong)b.u.d; if (tst != b.u.d) { - zerr("imaginary power"); + zerr("bad math expression: imaginary power"); return; } } @@ -1338,7 +1345,7 @@ op(int what) push(((a.type & MN_FLOAT) ? a.u.d : a.u.l) ? b : c, NULL, 0); break; case COLON: - zerr("':' without '?'"); + zerr("bad math expression: ':' without '?'"); break; case PREPLUS: if (spval->type & MN_FLOAT) @@ -1355,7 +1362,7 @@ op(int what) setmathvar(stack + sp, *spval); break; default: - zerr("out of integers"); + zerr("bad math expression: out of integers"); return; } } @@ -1525,7 +1532,7 @@ mathparse(int pc) mathparse(TOPPREC); if (mtok != M_OUTPAR) { if (!errflag) - zerr("')' expected"); + zerr("bad math expression: ')' expected"); return; } break; @@ -1543,7 +1550,7 @@ mathparse(int pc) noeval--; if (mtok != COLON) { if (!errflag) - zerr("':' expected"); + zerr("bad math expression: ':' expected"); return; } if (q) |