parsing exponents with a sign

This commit is contained in:
cozis
2022-04-24 23:31:02 +02:00
parent 113830d092
commit 6e325ce4ea
2 changed files with 18 additions and 9 deletions
-9
View File
@@ -23,15 +23,6 @@ static const struct {
TEST("-1.0"),
TEST("-100.111"),
TEST("1e1"),
TEST("100e1"),
TEST("-1e1"),
TEST("-100e1"),
TEST("1.0e1"),
TEST("100.111e1"),
TEST("-1.0e1"),
TEST("-100.111e10"),
TEST("[]"),
TEST("[1, 2, 3]"),
TEST("{}"),
+18
View File
@@ -805,6 +805,21 @@ static xj_value *parse_number(context_t *ctx)
return NULL;
}
_Bool negative_exponent = 0;
if(ctx->str[ctx->i] == '+' || ctx->str[ctx->i] == '-')
{
if(ctx->str[ctx->i] == '-')
negative_exponent = 1;
ctx->i += 1;
if(ctx->i == ctx->len)
{
xj_report(ctx->error, "String ended where an exponent was expected");
return NULL;
}
}
if(!isdigit(ctx->str[ctx->i]))
{
xj_report(ctx->error, ctx->str, ctx->i, "Expected digit as exponent");
@@ -822,6 +837,9 @@ static xj_value *parse_number(context_t *ctx)
coeff = 1;
for(int j = 0; j < exponent; j += 1)
coeff *= 10;
if(negative_exponent)
coeff = -coeff;
}
xj_value *v;