commit b23fdf8985b7608028a5fc9ebea279d220e0cacd
parent eef28cc7de6008131ae438047c4c353ce1d45bc9
Author: citbl <citbl@citbl.org>
Date: Wed, 20 May 2026 22:53:03 +1000
fix up lexer
Diffstat:
1 file changed, 9 insertions(+), 19 deletions(-)
diff --git a/mtcc/src/lexer.c b/mtcc/src/lexer.c
@@ -26,19 +26,20 @@ lexer_lex(struct lexer *lexer) {
size_t start_col = 0;
struct span ident;
struct token tok;
- enum token_type ttype;
+ enum token_type type;
while (i < len) {
c = lexer->src[i];
if (is_space(c)) {
if (c == '\n') {
- i++;
col = 1;
+ line++;
} else {
col++;
}
i++;
+ continue;
}
start = i;
@@ -50,12 +51,10 @@ lexer_lex(struct lexer *lexer) {
col++;
}
ident = (struct span){
- .filename = lexer->filename, .col = col, .line = line, .start = start, .stop = i};
- ttype = compare_span_to_token(lexer, ident);
- tok = (struct token){.span = ident, .token_type = ttype};
+ .filename = lexer->filename, .col = start_col, .line = line, .start = start, .stop = i};
+ type = compare_span_to_token(lexer, ident);
+ tok = (struct token){.span = ident, .token_type = type};
add_token(lexer, tok);
- i++;
- col++;
continue;
}
@@ -69,20 +68,11 @@ lexer_lex(struct lexer *lexer) {
continue;
}
- switch (c) {
- case '\n':
- case '\t':
- case '\r':
- case ' ':
- i++;
- col++;
- continue;
- }
-
i++;
col++;
}
- print_tokens(
+
+ print_tokens(lexer);
}
static bool
@@ -130,7 +120,7 @@ print_tokens(struct lexer *lexer) {
struct token t;
for (i = 0; i < lexer->tok_len; i++) {
t = lexer->tokens[i];
- printf("L%zu:%zu \t%-14s '", t.span.line + 1, t.span.col + 1, NAMES_TOKEN[t.token_type]);
+ printf("L%zu:%zu \t%-14s '", t.span.line, t.span.col, NAMES_TOKEN[t.token_type]);
fwrite(src + t.span.start, 1, t.span.stop - t.span.start, stdout);
printf("'\n");
}