Fixed print formatting.

This commit is contained in:
Jacob Signorovitch 2024-10-31 12:52:39 -04:00
parent 1c0dd7aa0b
commit 950c25bace
3 changed files with 22 additions and 1 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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) {