mighty

The mighty programming language, compiler and tools (WIP)
Log | Files | Refs

commit eef28cc7de6008131ae438047c4c353ce1d45bc9
parent 7f607b9091a7808d7808b01f121a54f0558bfd4c
Author: citbl <citbl@citbl.org>
Date:   Wed, 20 May 2026 22:42:31 +1000

print tokens

Diffstat:
Mmtcc/src/lexer.c | 25++++++++++++++++++++-----
1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/mtcc/src/lexer.c b/mtcc/src/lexer.c @@ -11,8 +11,8 @@ static bool is_space(const char c); static bool is_alpha_numeric(const char c); static bool is_dot(const char c); static enum token_type compare_span_to_token(struct lexer *lexer, struct span ident); - static void add_token(struct lexer *, struct token); +static void print_tokens(struct lexer *); void lexer_lex(struct lexer *lexer) { @@ -82,6 +82,7 @@ lexer_lex(struct lexer *lexer) { i++; col++; } + print_tokens( } static bool @@ -115,12 +116,26 @@ is_dot(const char c) { } static const char *NAMES_TOKEN[] = { - /*[TOKEN_IDENT] = "ident/type", - [TOKEN_LPAREN] = "open paren", - [TOKEN_RPAREN] = "close paren", - [TOKEN_LBRACE] = "open brace",*/ + [TOKEN_IDENT] = "ident/type", + [TOKEN_L_PAREN] = "open paren", + [TOKEN_R_PAREN] = "close paren", + [TOKEN_L_BRACE] = "open brace", + [TOKEN_R_BRACE] = "close brace", }; +static void +print_tokens(struct lexer *lexer) { + size_t i; + const char *src = lexer->src; + 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]); + fwrite(src + t.span.start, 1, t.span.stop - t.span.start, stdout); + printf("'\n"); + } +} + static enum token_type compare_span_to_token(struct lexer *lexer, struct span ident) { enum token_type t = TOKEN_IDENT;