harec

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

commit 20c0f4c858c27d197dafb0d8ac810879bb69a6eb
parent 9852393e73225ebb7ca9483cea0345c2c8dc92e8
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Sat, 16 Jan 2021 12:55:27 -0500

lex: handle negative integer literals

Previously, they would lex as a unary minus followed by a positive
integer literal. This causes issues with, for example,
-9223372036854775808i64 because the relevant positive integer literal is
too large to fit in an i64.

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

diff --git a/src/lex.c b/src/lex.c @@ -266,6 +266,9 @@ static uint32_t lex_literal(struct lexer *lexer, struct token *out) { uint32_t c = next(lexer, &out->loc, true); + if (c == '-') { + c = next(lexer, NULL, true); + } assert(c != UTF8_INVALID && c <= 0x7F && isdigit(c)); int base = 10; @@ -703,6 +706,11 @@ lex2(struct lexer *lexer, struct token *out, uint32_t c) out->token = T_MINUSMINUS; break; default: + if (c != UTF8_INVALID && c <= 0x7F && isdigit(c)) { + push(lexer, c, false); + push(lexer, '-', false); + return lex_literal(lexer, out); + } push(lexer, c, false); out->token = T_MINUS; break;