From 7805d3c0476c480cc6d64e4b91085ef5facbb186 Mon Sep 17 00:00:00 2001 From: Crazyinfin8 Date: Thu, 22 Apr 2021 20:38:05 -0700 Subject: [PATCH] minor fixes and consistancy changes --- src/vm/wren_compiler.c | 56 ++++++++-------------------------- src/vm/wren_core.c | 69 +++++++++--------------------------------- 2 files changed, 28 insertions(+), 97 deletions(-) diff --git a/src/vm/wren_compiler.c b/src/vm/wren_compiler.c index ac82ef275..750cfe50c 100644 --- a/src/vm/wren_compiler.c +++ b/src/vm/wren_compiler.c @@ -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) { @@ -797,10 +791,7 @@ static void readNumber(Parser* parser, int base) mantDigits++; } } - else - { - e++; - } + else e++; nextChar(parser); } if (base == 10) @@ -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'; } @@ -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); @@ -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; @@ -865,10 +847,7 @@ static void readNumber(Parser* parser, int base) nextChar(parser); continue; } - else - { - break; - } + else break; } if (!expHasDigits) { @@ -876,14 +855,8 @@ static void readNumber(Parser* parser, int base) 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) @@ -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. diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index 43a7af818..986d6efe4 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -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') @@ -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; @@ -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]; @@ -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].