diff --git a/src/tinyexpr.cpp b/src/tinyexpr.cpp index 810b1e7e0..e76955d45 100644 --- a/src/tinyexpr.cpp +++ b/src/tinyexpr.cpp @@ -380,6 +380,14 @@ static te_expr *base(state *s) { ret = new_expr(TE_CONSTANT, nullptr); ret->value = s->value; next_token(s); + if (s->type == TOK_NUMBER || s->type == TE_FUNCTION0) { + // Two numbers after each other: + // math '5 2' + // math '3 pi' + // (of course 3 pi could also be interpreted as 3 x pi) + s->type = TOK_ERROR; + s->error = TE_ERROR_MISSING_OPERATOR; + } break; case TE_FUNCTION0: @@ -623,10 +631,7 @@ te_expr *te_compile(const wchar_t *expression, te_error_t *error) { error->type = s.error; } else { // If we're not at the end but there's no error, then that means we have a - // superfluous token that we have no idea what to do with. This occurs in e.g. `2 + - // 2 4` - the "4" is just not part of the expression. We can report either "too many - // arguments" or "expected operator", but the operator should be reported between - // the "2" and the "4". So we report TOO_MANY_ARGS on the "4". + // superfluous token that we have no idea what to do with. error->type = TE_ERROR_TOO_MANY_ARGS; } } diff --git a/tests/checks/math.fish b/tests/checks/math.fish index b8f519df1..6f04c54d7 100644 --- a/tests/checks/math.fish +++ b/tests/checks/math.fish @@ -126,17 +126,17 @@ not math 'sin()' # CHECKERR: 'sin()' # CHECKERR: ^ not math '2 + 2 4' -# CHECKERR: math: Error: Too many arguments +# CHECKERR: math: Error: Missing operator # CHECKERR: '2 + 2 4' -# CHECKERR: ^ +# CHECKERR: ^ not math '(1 2)' -# CHECKERR: math: Error: Too many arguments +# CHECKERR: math: Error: Missing operator # CHECKERR: '(1 2)' -# CHECKERR: ^ +# CHECKERR: ^ not math '(1 pi)' -# CHECKERR: math: Error: Too many arguments +# CHECKERR: math: Error: Missing operator # CHECKERR: '(1 pi)' -# CHECKERR: ^ +# CHECKERR: ^ not math '(1 pow 1,2)' # CHECKERR: math: Error: Too many arguments # CHECKERR: '(1 pow 1,2)'