From 273671ff063c62af3a133f3dda67f33867f225e2 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Sun, 6 Feb 2022 23:45:09 -0500 Subject: [cup] Add support for `here` keyword + fix `putu_buffer` bug --- compiler/lexer.cup | 16 ++++++++++++++++ std/common.cup | 6 +++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/compiler/lexer.cup b/compiler/lexer.cup index 1bcbb35..50b06e2 100644 --- a/compiler/lexer.cup +++ b/compiler/lexer.cup @@ -184,6 +184,22 @@ fn lexer_next(lexer: Lexer*, token: Token*) { } else { + // Handle the `here` keyword at lex-time + if (lexer_starts_with(lexer, "here")) { + let s: char* = malloc(sizeof(char) * 128); // Should be enough + // Print the location into the string + let buf: char[32]; + strcpy(s, lexer.filename); strcat(s, ":"); + putu_buffer(lexer.line+1, buf); strcat(s, buf); strcat(s, ":"); + putu_buffer(lexer.col+1, buf); strcat(s, buf); + + // Make the token + let loc: Location; + lexer_loc(lexer, &loc); + lexer_advance(lexer, 4); + return token_from_string(token, s, &loc); + } + // Parse the keywords... for (let i = TOKEN__KEYWORD_BEGIN+1; i < TOKEN__KEYWORD_END; ++i) { let str = keyword_to_string(i); diff --git a/std/common.cup b/std/common.cup index 3d0b269..dac8148 100644 --- a/std/common.cup +++ b/std/common.cup @@ -157,10 +157,10 @@ fn putu_buffer(n: int, buf: char*): int { if (i == 0) { buf[i] = '0'; i = i + 1; - } else if (i > 1) { - buf[i] = 0; - strrev(buf); } + buf[i] = 0; + if (i > 1) + strrev(buf); return i; } -- cgit v1.2.3