aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-06 23:45:09 -0500
committerMustafa Quraish <[email protected]>2022-02-07 03:18:08 -0500
commit273671ff063c62af3a133f3dda67f33867f225e2 (patch)
tree02269d9d069a26f6048851f5f21aecbe72b02915
parent[cup] Add support for string literals (diff)
downloadcup-273671ff063c62af3a133f3dda67f33867f225e2.tar.xz
cup-273671ff063c62af3a133f3dda67f33867f225e2.zip
[cup] Add support for `here` keyword + fix `putu_buffer` bug
-rw-r--r--compiler/lexer.cup16
-rw-r--r--std/common.cup6
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;
}