From c0d8439f3a5391b2980a0302401e00365a47e5e4 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 1 Nov 2019 08:40:56 +0100 Subject: [PATCH] math: Print special error for logical operators Until now, something like `math '7 = 2'` would complain about a "missing" operator. Now we print an error about logical operators not being supported and point the user towards `test`. Fixes #6096 --- src/builtin_math.cpp | 2 ++ src/tinyexpr.cpp | 9 +++++++++ src/tinyexpr.h | 3 ++- tests/checks/math.fish | 5 +++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/builtin_math.cpp b/src/builtin_math.cpp index 822040930..611c29141 100644 --- a/src/builtin_math.cpp +++ b/src/builtin_math.cpp @@ -149,6 +149,8 @@ static const wchar_t *math_describe_error(te_error_t &error) { return _(L"Missing operator"); case TE_ERROR_UNEXPECTED_TOKEN: return _(L"Unexpected token"); + case TE_ERROR_LOGICAL_OPERATOR: + return _(L"Logical operations are not supported, use `test` instead"); case TE_ERROR_UNKNOWN: return _(L"Expression is bogus"); default: diff --git a/src/tinyexpr.cpp b/src/tinyexpr.cpp index 61c007e17..e16b87b6f 100644 --- a/src/tinyexpr.cpp +++ b/src/tinyexpr.cpp @@ -306,6 +306,15 @@ void next_token(state *s) { case '\n': case '\r': break; + case '=': + case '>': + case '<': + case '&': + case '|': + case '!': + s->type = TOK_ERROR; + s->error = TE_ERROR_LOGICAL_OPERATOR; + break; default: s->type = TOK_ERROR; s->error = TE_ERROR_MISSING_OPERATOR; diff --git a/src/tinyexpr.h b/src/tinyexpr.h index fb945525b..bd6e93409 100644 --- a/src/tinyexpr.h +++ b/src/tinyexpr.h @@ -36,7 +36,8 @@ typedef enum { TE_ERROR_TOO_MANY_ARGS = 5, TE_ERROR_MISSING_OPERATOR = 6, TE_ERROR_UNEXPECTED_TOKEN = 7, - TE_ERROR_UNKNOWN = 8 + TE_ERROR_LOGICAL_OPERATOR = 8, + TE_ERROR_UNKNOWN = 9 } te_error_type_t; typedef struct te_error_t { diff --git a/tests/checks/math.fish b/tests/checks/math.fish index a66e9937a..a4fa92f69 100644 --- a/tests/checks/math.fish +++ b/tests/checks/math.fish @@ -126,3 +126,8 @@ math 0x 3 # CHECK: 20 # CHECK: 8 # CHECK: 0 + +math "42 >= 1337" +# CHECKERR: math: Error: Logical operations are not supported, use `test` instead +# CHECKERR: '42 >= 1337' +# CHECKERR: ^