Skip to content

Commit

Permalink
minor fixes and consistancy changes
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyInfin8 committed Apr 23, 2021
1 parent 8345297 commit 7805d3c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 97 deletions.
56 changes: 13 additions & 43 deletions src/vm/wren_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,14 +780,8 @@ static void readNumber(Parser* parser, int base)
nextChar(parser);
continue;
}
else
{
break;
}
if (c >= base)
{
break;
}
else break;
if (c >= base) break;
hasDigits = true;
if (mantDigits < maxMant)
{
Expand All @@ -797,10 +791,7 @@ static void readNumber(Parser* parser, int base)
mantDigits++;
}
}
else
{
e++;
}
else e++;
nextChar(parser);
}
if (base == 10)
Expand All @@ -811,7 +802,7 @@ static void readNumber(Parser* parser, int base)
for (;;)
{
c = peekChar(parser);
if (c >= '0' && c <= '9')
if (isDigit(c))
{
c -= '0';
}
Expand All @@ -820,17 +811,11 @@ static void readNumber(Parser* parser, int base)
nextChar(parser);
continue;
}
else
{
break;
}
else break;
if (mantDigits < maxMant)
{
num = num * 10 + c;
if (num > 0)
{
mantDigits++;
}
if (num > 0) mantDigits++;
e--;
}
nextChar(parser);
Expand All @@ -847,14 +832,11 @@ static void readNumber(Parser* parser, int base)
expNeg = true;
nextChar(parser);
}
else if (c == '+')
{
nextChar(parser);
}
else if (c == '+') nextChar(parser);
for (;;)
{
c = peekChar(parser);
if (c >= '0' && c <= '9')
if (isDigit(c))
{
expNum = expNum * 10 + (c - '0');
expHasDigits = true;
Expand All @@ -865,25 +847,16 @@ static void readNumber(Parser* parser, int base)
nextChar(parser);
continue;
}
else
{
break;
}
else break;
}
if (!expHasDigits)
{
lexError(parser, "Unterminated scientific notation.");
parser->next.value = NUM_VAL(0);
return;
}
else if (expNeg)
{
e -= expNum;
}
else
{
e += expNum;
}
else if (expNeg) e -= expNum;
else e += expNum;
}
}
else if (!hasDigits)
Expand All @@ -899,11 +872,8 @@ static void readNumber(Parser* parser, int base)
lexError(parser, "Number literal was too large (%d).", sizeof(double));
parser->next.value = NUM_VAL(0);
}
else
{
parser->next.value = NUM_VAL(f);
makeToken(parser, TOKEN_NUMBER);
}
parser->next.value = NUM_VAL(f);
makeToken(parser, TOKEN_NUMBER);
}

// Finishes lexing an identifier. Handles reserved words.
Expand Down
69 changes: 15 additions & 54 deletions src/vm/wren_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,7 @@ DEF_PRIMITIVE(num_fromString)
neg = true;
i++;
}
else if (c == '+')
{
i++;
}
else if (c == '+') i++;
if (i >= string->length) goto end;
long long maxMant = 16, num = 0;
if (c == '0')
Expand Down Expand Up @@ -683,53 +680,38 @@ DEF_PRIMITIVE(num_fromString)
if (mantDigits < maxMant)
{
num = num * base + c;
if (num > 0)
{
mantDigits++;
}
}
else
{
e++;
if (num > 0) mantDigits++;
}
else e++;
i++;
}
if (i >= string->length) goto end;
if (base == 10)
{
if (str[i] == '.' && str[i+1] >= '0' && str[i+1] <= '9')
if (str[i] == '.' && isdigit(str[i+1]))
{
i++;
for(;i < string->length;)
{
c = str[i];
if (isdigit(c))
{
c -= '0';
}
if (isdigit(c)) c -= '0';
else if(c == '_')
{
i++;
continue;
}
else
{
break;
}
else break;
if (mantDigits < maxMant)
{
num = num * 10 + c;
if (num > 0)
{
mantDigits++;
}
if (num > 0) mantDigits++;
e--;
}
i++;
}
}
if (i >= string->length) goto end;
if(str[i] == 'e' || c == 'E')
if(str[i] == 'e' || str[i] == 'E')
{
i++;
if (i >= string->length) goto eEnd;
Expand All @@ -740,10 +722,7 @@ DEF_PRIMITIVE(num_fromString)
expNeg = true;
i++;
}
else if (str[i] == '+')
{
i++;
}
else if (str[i] == '+') i++;
for(;i < string->length;)
{
c = str[i];
Expand All @@ -758,45 +737,27 @@ DEF_PRIMITIVE(num_fromString)
i++;
continue;
}
else
{
break;
}
else break;
}
eEnd:
if (!expHasDigits)
{
RETURN_NULL;
}
else if (expNeg)
{
e -= expNum;
}
else
{
e += expNum;
}
else if (expNeg) e -= expNum;
else e += expNum;
}
}
while(i < string->length && isspace(str[i])) i++;
end:
if (base != 10 && !hasDigits)
{
RETURN_NULL;
}
if (base != 10 && !hasDigits) RETURN_NULL;
// We must have consumed the entire string. Otherwise, it contains non-number
// characters and we can't parse it.
if (i < string->length) RETURN_NULL;
double f = (double)(num) *
(double)(powl)((long double) base, (long double) e);
if (f > DBL_MAX || (f < DBL_MIN && num > 0))
{
RETURN_ERROR("Number literal is too large.");
}
else
{
RETURN_NUM(neg ? f * -1 : f);
}
if (f > DBL_MAX || (f < DBL_MIN && num > 0)) RETURN_ERROR("Number literal is too large.");
RETURN_NUM(neg ? f * -1 : f);
}

// Defines a primitive on Num that calls infix [op] and returns [type].
Expand Down

0 comments on commit 7805d3c

Please sign in to comment.