Fixed more print formatting.

This commit is contained in:
Jacob Signorovitch 2024-10-31 16:05:04 -04:00
parent 950c25bace
commit 2662f54c51
3 changed files with 41 additions and 19 deletions

View File

@ -20,40 +20,61 @@
// Resent color code. // Resent color code.
#define COL_RESET "\e[0m" #define COL_RESET "\e[0m"
// Bold while color code.
// Regular color codes.
#define COL_BLA "\e[0;30m"
#define COL_RED "\e[0;31m"
#define COL_GRE "\e[0;32m"
#define COL_YEL "\e[0;33m"
#define COL_BLU "\e[0;34m"
#define COL_MAG "\e[0;35m"
#define COL_CYA "\e[0;36m"
#define COL_WHI "\e[0;37m"
// Bold color codes.
#define COL_BBLA "\e[1;30m"
#define COL_BRED "\e[1;31m"
#define COL_BGRE "\e[1;32m"
#define COL_BYEL "\e[1;33m"
#define COL_BBLU "\e[1;34m"
#define COL_BMAG "\e[1;35m"
#define COL_BCYA "\e[1;36m"
#define COL_BWHI "\e[1;37m" #define COL_BWHI "\e[1;37m"
// Start in indent block. // Start in indent block.
#define INDENT_BEGIN(ILVL) \ #define INDENT_BEGIN(ILVL) \
int INDENT_lvl = ILVL; \ __attribute__((unused)) int INDENT_lvl = ILVL; \
Dstr* INDENT_spacing = dstr_init(); \ Dstr* INDENT_spacing = dstr_init(); \
for (int INDENT_j = 0; INDENT_j < ILVL; INDENT_j++) \ for (int INDENT_j = 0; INDENT_j < ILVL; INDENT_j++) \
dstr_appendch(INDENT_spacing, ' '); dstr_appendch(INDENT_spacing, ' ');
// Print & indent the title of a section. // Print & indent the title of a section.
#define INDENT_TITLE(THING, WHERE) \ #define INDENT_TITLE(THING, WHERE) \
printf("%s" COL_BWHI THING " @ %p\n" COL_RESET, INDENT_spacing->buf, WHERE); printf("%s" COL_BCYA THING " @ %p\n" COL_RESET, INDENT_spacing->buf, WHERE);
// Print & indent a thing. // Print & indent a thing.
#define INDENT_FIELD(FIELD, VAL, ...) \ #define INDENT_FIELD(FIELD, VAL, ...) \
printf("%s " FIELD ": " VAL "\n", INDENT_spacing->buf, __VA_ARGS__); printf("%s " COL_BWHI FIELD ": " COL_RESET COL_WHI VAL COL_RESET "\n", \
INDENT_spacing->buf, __VA_ARGS__);
// Print & indent a thing with a newline before the val. // Print & indent a thing with a newline before the val.
#define INDENT_FIELD_NL(FIELD, VAL, ...) \ #define INDENT_FIELD_NL(FIELD, VAL, ...) \
printf("%s " FIELD ":\n %s " VAL "\n", INDENT_spacing->buf, \ printf("%s " COL_BWHI FIELD ":" COL_RESET "\n %s " COL_WHI VAL COL_RESET \
INDENT_spacing->buf, __VA_ARGS__); "\n", \
INDENT_spacing->buf, INDENT_spacing->buf, __VA_ARGS__);
// Print & indent a thing without any newline. // Print & indent a thing without any newline.
#define INDENT_FIELD_NONL(FIELD) printf("%s " FIELD ": ", INDENT_spacing->buf); #define INDENT_FIELD_NONL_START(FIELD) \
printf("%s " COL_BWHI FIELD ": " COL_RESET COL_WHI, INDENT_spacing->buf);
#define INDENT_FIELD_NONL_END printf( "\n" COL_RESET);
// Print an array A of N things, by calling the function F. // Print an array A of N things, by calling the function F.
#define INDENT_FIELD_LIST(FIELD, A, N, F) \ #define INDENT_FIELD_LIST(FIELD, A, N, F) \
printf("%s " FIELD ": [\n", INDENT_spacing->buf); \ printf("%s " COL_BWHI FIELD ": [\n" COL_RESET, INDENT_spacing->buf); \
for (int INDENT_i = 0; INDENT_i < N; INDENT_i++) { \ for (int INDENT_i = 0; INDENT_i < N; INDENT_i++) { \
F(A[INDENT_i], INDENT_lvl + 2); \ F(A[INDENT_i], INDENT_lvl + 2); \
/*printf("%s \n", INDENT_spacing->buf); */ \
} \ } \
printf("%s ]\n", INDENT_spacing->buf); printf(COL_BWHI "%s ]\n" COL_RESET, INDENT_spacing->buf);
// End an indent block. // End an indent block.
#define INDENT_END dstr_destroy(INDENT_spacing); #define INDENT_END dstr_destroy(INDENT_spacing);

View File

@ -7,7 +7,6 @@
#include "include/dstr.h" #include "include/dstr.h"
#include "include/token.h" #include "include/token.h"
#include "include/util.h" #include "include/util.h"
Lexer* lexer_init(char* src) { Lexer* lexer_init(char* src) {
Lexer* lexer = malloc(sizeof(Lexer)); Lexer* lexer = malloc(sizeof(Lexer));
@ -121,8 +120,9 @@ void lexer_print(Lexer* lexer) { lexer_print_i(lexer, 0); }
void lexer_print_i(Lexer* lexer, int ilvl) { void lexer_print_i(Lexer* lexer, int ilvl) {
INDENT_BEGIN(ilvl); INDENT_BEGIN(ilvl);
INDENT_TITLE("Lexer", lexer); INDENT_TITLE("Lexer", lexer);
INDENT_FIELD_NONL("state"); INDENT_FIELD_NONL_START("state")
lexerstate_print_raw(lexer->state); putchar('\n'); lexerstate_print_raw(lexer->state);
INDENT_FIELD_NONL_END
INDENT_FIELD("srcln", "%ld", lexer->srcln); INDENT_FIELD("srcln", "%ld", lexer->srcln);
INDENT_FIELD_NL("src", "\"%s\"", lexer->src); INDENT_FIELD_NL("src", "\"%s\"", lexer->src);
INDENT_FIELD("cchar", "'%c'", *lexer->cchar); INDENT_FIELD("cchar", "'%c'", *lexer->cchar);
@ -139,7 +139,8 @@ void lexer_print_i(Lexer* lexer, int ilvl) {
} }
void lexerstate_print_raw(LexerState s) { void lexerstate_print_raw(LexerState s) {
if (s > LEXER_STATE_MAX) if (s > LEXER_STATE_MAX) {
printf("Unknown (%d)", s) && log_dbgf("%d is not a valid LexerState (max: %d)", s, TOKEN_TYPE_MAX) printf("Unknown (%d)", s);
else printf("%s", lexerstate_names[s]); log_dbgf("%d is not a valid LexerState (max: %d)", s, TOKEN_TYPE_MAX);
} else printf("%s", lexerstate_names[s]);
} }

View File

@ -30,9 +30,9 @@ void token_print_i(Token *token, int ilvl) {
INDENT_BEGIN(ilvl); INDENT_BEGIN(ilvl);
INDENT_TITLE("Token", token); INDENT_TITLE("Token", token);
INDENT_FIELD_NONL("type"); INDENT_FIELD_NONL_START("type")
tokentype_print_raw(token->type); tokentype_print_raw(token->type);
putchar('\n'); INDENT_FIELD_NONL_END
INDENT_FIELD("valn", "%ld", token->valn); INDENT_FIELD("valn", "%ld", token->valn);
INDENT_FIELD_NL("val", "\"%s\"", token->val); INDENT_FIELD_NL("val", "\"%s\"", token->val);
} }