From 950c25bace4b5399ad045635c4e2469e8c856014 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Thu, 31 Oct 2024 12:52:39 -0400 Subject: [PATCH] Fixed print formatting. --- src/ast.c | 2 ++ src/include/util.h | 17 ++++++++++++++++- src/lexer.c | 4 ++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ast.c b/src/ast.c index 418b4a7..ec0aa1b 100644 --- a/src/ast.c +++ b/src/ast.c @@ -3,10 +3,12 @@ #include "include/ast.h" #include "include/util.h" +#if 0 static char* asttype_names[] = { [AST_TYPE_CALL] = "CALL", [AST_TYPE_NUM] = "NUMBER", }; +#endif ASTTypeNum* ast_type_num_init(int val) { talloc(ASTTypeNum, num); diff --git a/src/include/util.h b/src/include/util.h index 645f5d1..a9a7ea7 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -18,15 +18,21 @@ #define log_dbgf(msg, ...) #endif // ifdef DBG else +// Resent color code. +#define COL_RESET "\e[0m" +// Bold while color code. +#define COL_BWHI "\e[1;37m" + // Start in indent block. #define INDENT_BEGIN(ILVL) \ + int INDENT_lvl = ILVL; \ Dstr* INDENT_spacing = dstr_init(); \ for (int INDENT_j = 0; INDENT_j < ILVL; INDENT_j++) \ dstr_appendch(INDENT_spacing, ' '); // Print & indent the title of a section. #define INDENT_TITLE(THING, WHERE) \ - printf("%s" THING " @ %p\n", INDENT_spacing->buf, WHERE); + printf("%s" COL_BWHI THING " @ %p\n" COL_RESET, INDENT_spacing->buf, WHERE); // Print & indent a thing. #define INDENT_FIELD(FIELD, VAL, ...) \ @@ -40,6 +46,15 @@ // Print & indent a thing without any newline. #define INDENT_FIELD_NONL(FIELD) printf("%s " FIELD ": ", INDENT_spacing->buf); +// Print an array A of N things, by calling the function F. +#define INDENT_FIELD_LIST(FIELD, A, N, F) \ + printf("%s " FIELD ": [\n", INDENT_spacing->buf); \ + for (int INDENT_i = 0; INDENT_i < N; INDENT_i++) { \ + F(A[INDENT_i], INDENT_lvl + 2); \ + /*printf("%s \n", INDENT_spacing->buf); */ \ + } \ + printf("%s ]\n", INDENT_spacing->buf); + // End an indent block. #define INDENT_END dstr_destroy(INDENT_spacing); diff --git a/src/lexer.c b/src/lexer.c index 42f245d..93eb648 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -5,6 +5,7 @@ #include "include/lexer.h" #include "include/dstr.h" +#include "include/token.h" #include "include/util.h" Lexer* lexer_init(char* src) { @@ -126,12 +127,15 @@ void lexer_print_i(Lexer* lexer, int ilvl) { INDENT_FIELD_NL("src", "\"%s\"", lexer->src); INDENT_FIELD("cchar", "'%c'", *lexer->cchar); INDENT_FIELD("ntokens", "%ld", lexer->ntokens); + INDENT_FIELD_LIST("tokens", lexer->tokens, lexer->ntokens, token_print_i); +#if 0 printf("%s tokens: [\n", INDENT_spacing->buf); for (int i = 0; i < lexer->ntokens; i++) { token_print_i(lexer->tokens[i], ilvl + 2); printf(",\n\n"); } +#endif } void lexerstate_print_raw(LexerState s) {