From 6a38eb4f7dbd3df58bf00260ea4add9740b89b68 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Mon, 26 Feb 2018 13:46:01 +0100 Subject: [PATCH] [math] Better error for `2 - ` Instead of the catch-all, report too few arguments. --- src/tinyexpr.c | 11 +++++++++++ tests/math.err | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/tinyexpr.c b/src/tinyexpr.c index b91717706..259ed1bc8 100755 --- a/src/tinyexpr.c +++ b/src/tinyexpr.c @@ -350,6 +350,17 @@ static te_expr *base(state *s) { } break; + case TOK_END: + // The expression ended before we expected it. + // e.g. `2 - `. + // This means we have too few things. + // Instead of introducing another error, just call it + // "too few args". + ret = new_expr(0, 0); + s->type = TOK_ERROR; + s->error = TE_ERROR_TOO_FEW_ARGS; + ret->value = NAN; + break; default: ret = new_expr(0, 0); s->type = TOK_ERROR; diff --git a/tests/math.err b/tests/math.err index 8282dedae..323287cb6 100644 --- a/tests/math.err +++ b/tests/math.err @@ -7,7 +7,7 @@ #################### # Validate math error reporting -math: Error: Expression is bogus +math: Error: Too few arguments '2 - ' ^ math: Error: Too few arguments