[tinyexpr] Let specific errors take precedence over generic ones

Fixes the case where `sin()` reported the generic "bogus" error
instead of "too few arguments".

Also rename the constant to "TE_ERROR_UNKNOWN".
This commit is contained in:
Fabian Homborg 2018-02-25 21:41:38 +01:00
parent 4f39cc4d82
commit d90d0ee08e
3 changed files with 14 additions and 15 deletions

View file

@ -124,7 +124,7 @@ wcstring math_describe_error(te_error_t& error) {
case TE_ERROR_TOO_FEW_ARGS: return _(L"Too few arguments"); case TE_ERROR_TOO_FEW_ARGS: return _(L"Too few arguments");
case TE_ERROR_TOO_MANY_ARGS: return _(L"Too many arguments"); case TE_ERROR_TOO_MANY_ARGS: return _(L"Too many arguments");
case TE_ERROR_MISSING_OPERATOR: return _(L"Missing operator"); case TE_ERROR_MISSING_OPERATOR: return _(L"Missing operator");
case TE_ERROR_BOGUS: return _(L"Expression is bogus"); case TE_ERROR_UNKNOWN: return _(L"Expression is bogus");
default: return L""; default: return L"";
} }
} }

View file

@ -239,8 +239,9 @@ void next_token(state *s) {
s->function = var->address; s->function = var->address;
break; break;
} }
} else if (s->type != TOK_ERROR) { } else if (s->type != TOK_ERROR
// TODO: Better error - "Not a variable"? || s->error == TE_ERROR_UNKNOWN) {
// Our error is more specific, so it takes precedence.
s->type = TOK_ERROR; s->type = TOK_ERROR;
s->error = TE_ERROR_UNKNOWN_VARIABLE; s->error = TE_ERROR_UNKNOWN_VARIABLE;
} }
@ -295,8 +296,8 @@ static te_expr *base(state *s) {
next_token(s); next_token(s);
if (s->type == TOK_CLOSE) { if (s->type == TOK_CLOSE) {
next_token(s); next_token(s);
} else if (s->type != TOK_ERROR) { } else if (s->type != TOK_ERROR
// TODO: Better error - "Missing closing parenthesis"? || s->error == TE_ERROR_UNKNOWN) {
s->type = TOK_ERROR; s->type = TOK_ERROR;
s->error = TE_ERROR_MISSING_CLOSING_PAREN; s->error = TE_ERROR_MISSING_CLOSING_PAREN;
} }
@ -323,15 +324,14 @@ static te_expr *base(state *s) {
} }
if(s->type == TOK_CLOSE && i == arity - 1) { if(s->type == TOK_CLOSE && i == arity - 1) {
next_token(s); next_token(s);
} else if (s->type != TOK_ERROR) { } else if (s->type != TOK_ERROR
// TODO: Either a closing paren was needed, || s->error == TE_ERROR_UNKNOWN) {
// or too few arguments were given?
s->type = TOK_ERROR; s->type = TOK_ERROR;
s->error = i < arity ? TE_ERROR_TOO_FEW_ARGS s->error = i < arity ? TE_ERROR_TOO_FEW_ARGS
: TE_ERROR_TOO_MANY_ARGS; : TE_ERROR_TOO_MANY_ARGS;
} }
} else if (s->type != TOK_ERROR) { } else if (s->type != TOK_ERROR
// TODO: Better error - "Expected opening parenthesis"? || s->error == TE_ERROR_UNKNOWN) {
s->type = TOK_ERROR; s->type = TOK_ERROR;
s->error = TE_ERROR_MISSING_OPENING_PAREN; s->error = TE_ERROR_MISSING_OPENING_PAREN;
} }
@ -343,8 +343,8 @@ static te_expr *base(state *s) {
ret = expr(s); ret = expr(s);
if (s->type == TOK_CLOSE) { if (s->type == TOK_CLOSE) {
next_token(s); next_token(s);
} else if (s->type != TOK_ERROR) { } else if (s->type != TOK_ERROR
// TODO: Error - missing closing paren? || s->error == TE_ERROR_UNKNOWN) {
s->type = TOK_ERROR; s->type = TOK_ERROR;
s->error = TE_ERROR_MISSING_CLOSING_PAREN; s->error = TE_ERROR_MISSING_CLOSING_PAREN;
} }
@ -352,9 +352,8 @@ static te_expr *base(state *s) {
default: default:
ret = new_expr(0, 0); ret = new_expr(0, 0);
// TODO: Error - expression is bogus?
s->type = TOK_ERROR; s->type = TOK_ERROR;
s->error = TE_ERROR_BOGUS; s->error = TE_ERROR_UNKNOWN;
ret->value = NAN; ret->value = NAN;
break; break;
} }

View file

@ -41,7 +41,7 @@ extern "C" {
TE_ERROR_TOO_FEW_ARGS = 4, TE_ERROR_TOO_FEW_ARGS = 4,
TE_ERROR_TOO_MANY_ARGS = 5, TE_ERROR_TOO_MANY_ARGS = 5,
TE_ERROR_MISSING_OPERATOR = 6, TE_ERROR_MISSING_OPERATOR = 6,
TE_ERROR_BOGUS = 7 TE_ERROR_UNKNOWN = 7
}; };
typedef int te_error_type_t; typedef int te_error_type_t;