From 3a06580b89459553b866f27eb7ee8792b0310a76 Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Sun, 31 Jan 2021 07:44:52 +0200 Subject: [PATCH] Don't use `strtoll()` with explicit base to parse hex, instead use `strtod()` (#826) `strtod()` automatically recognizes the `0x` and converts to hex This has two advantages: 1. Simplify code (no need of argument `isHex` to `makeNumber`, no `if`) 2. Allow numbers larger than `long long` if they fit into `double` --- src/vm/wren_compiler.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/vm/wren_compiler.c b/src/vm/wren_compiler.c index 95c470dd..497978f7 100644 --- a/src/vm/wren_compiler.c +++ b/src/vm/wren_compiler.c @@ -713,18 +713,11 @@ static int readHexDigit(Parser* parser) } // Parses the numeric value of the current token. -static void makeNumber(Parser* parser, bool isHex) +static void makeNumber(Parser* parser) { errno = 0; - if (isHex) - { - parser->next.value = NUM_VAL((double)strtoll(parser->tokenStart, NULL, 16)); - } - else - { - parser->next.value = NUM_VAL(strtod(parser->tokenStart, NULL)); - } + parser->current.value = NUM_VAL(strtod(parser->tokenStart, NULL)); if (errno == ERANGE) { @@ -747,7 +740,7 @@ static void readHexNumber(Parser* parser) // Iterate over all the valid hexadecimal digits found. while (readHexDigit(parser) != -1) continue; - makeNumber(parser, true); + makeNumber(parser); } // Finishes lexing a number literal. @@ -780,7 +773,7 @@ static void readNumber(Parser* parser) while (isDigit(peekChar(parser))) nextChar(parser); } - makeNumber(parser, false); + makeNumber(parser); } // Finishes lexing an identifier. Handles reserved words.