harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit af6683cc526eacf346515c9757f80d321b5b8e22
parent cedb44f6e9d288ea5cd770a5eef36ee424e62b7b
Author: Drew DeVault <sir@cmpwn.com>
Date:   Tue,  2 Feb 2021 15:52:59 -0500

lex: fix non-decimal literals

Diffstat:
Msrc/lex.c | 13+++++++++++++
1 file changed, 13 insertions(+), 0 deletions(-)

diff --git a/src/lex.c b/src/lex.c @@ -266,6 +266,7 @@ lex_name(struct lexer *lexer, struct token *out) static uint32_t lex_literal(struct lexer *lexer, struct token *out) { + bool neg = false; uint32_t c = next(lexer, &out->loc, true); if (c == '-') { c = next(lexer, NULL, true); @@ -278,14 +279,17 @@ lex_literal(struct lexer *lexer, struct token *out) case 'b': base = 2; basechrs = "01"; + consume(lexer, 2); break; case 'o': base = 8; basechrs = "01234567"; + consume(lexer, 2); break; case 'x': base = 16; basechrs = "0123456789ABCDEFabcdef"; + consume(lexer, 2); break; default: push(lexer, c, true); @@ -399,6 +403,9 @@ finalize: for (uintmax_t i = 0; i < exponent; i++) { out->uval *= 10; } + if (neg) { + out->uval = -out->uval; + } break; case TYPE_STORAGE_I8: case TYPE_STORAGE_I16: @@ -409,10 +416,16 @@ finalize: for (uintmax_t i = 0; i < exponent; i++) { out->ival *= 10; } + if (neg) { + out->ival = -out->ival; + } break; case TYPE_STORAGE_F32: case TYPE_STORAGE_F64: out->fval = strtod(lexer->buf, NULL); + if (neg) { + out->fval = -out->fval; + } break; default: assert(0);