From ca9d2aabe43e4338d3baf02dfcf5546adceb52d8 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Fri, 25 Oct 2024 11:20:07 -0400 Subject: [PATCH] Fixed more printing, added basic ast. --- Makefile | 2 +- src/include/ast.h | 8 ++++++++ src/include/lexer.h | 5 +---- src/include/util.h | 8 ++++++-- src/lexer.c | 43 +++++++++++++------------------------------ src/token.c | 20 +++----------------- 6 files changed, 32 insertions(+), 54 deletions(-) create mode 100644 src/include/ast.h diff --git a/Makefile b/Makefile index 739efb1..bc39823 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ release: CFLAGS = -Wall -O2 release: $(TARGET) run: $(TARGET) - @ echo "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)" + @ echo -e "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)" @ ./$(TARGET) # Link to final binary. diff --git a/src/include/ast.h b/src/include/ast.h new file mode 100644 index 0000000..ba5ea16 --- /dev/null +++ b/src/include/ast.h @@ -0,0 +1,8 @@ +#ifndef AST_H +#define AST_H + +typedef struct { + +} AST; + +#endif diff --git a/src/include/lexer.h b/src/include/lexer.h index cda7aa7..b52aff3 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -65,9 +65,6 @@ void lexer_print(Lexer* lexer); void lexer_print_i(Lexer* lexer, int ilvl); // Print a representation of a LexerState. -void lexerstate_print(LexerState s); - -// Print a representation of a LexerState at the specified indentation level. -void lexerstate_print_i(LexerState s, int ilvl); +void lexerstate_print_raw(LexerState s); #endif diff --git a/src/include/util.h b/src/include/util.h index d9a7f49..d96ded8 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -24,16 +24,20 @@ 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); +// Print & indent a thing. #define INDENT_FIELD(FIELD, VAL, ...) \ printf("%s " FIELD ": " VAL "\n", INDENT_spacing->buf, __VA_ARGS__); -#define INDENT_FIELD_NL(FIELD, VAL) \ - printf("%s " FIELD ":\n%s " VAL "\n", INDENT_spacing->buf, \ +// Print & indent a thing with a newline before the val. +#define INDENT_FIELD_NL(FIELD, VAL, ...) \ + printf("%s " FIELD ":\n %s " VAL "\n", INDENT_spacing->buf, \ INDENT_spacing->buf, __VA_ARGS__); +// Print & indent a thing without any newline. #define INDENT_FIELD_NONL(FIELD) printf("%s " FIELD ": ", INDENT_spacing->buf); #define INDENT_END dstr_destroy(INDENT_spacing); diff --git a/src/lexer.c b/src/lexer.c index dda0d01..44c7598 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -111,20 +111,15 @@ void lexer_add_token(Lexer* lexer, Token* token) { void lexer_print(Lexer* lexer) { lexer_print_i(lexer, 0); } void lexer_print_i(Lexer* lexer, int ilvl) { - Dstr* spacing = dstr_init(); - char* sp = spacing->buf; - for (int i = 0; i < ilvl; i++) dstr_appendch(spacing, ' '); - - printf("%sLexer @ %p\n", sp, lexer); - printf("%s state:\n", sp); - lexerstate_print_i(lexer->state, ilvl + 2); - printf("%s srcln:\n", sp); - printf("%s %ld\n", sp, lexer->srcln); - printf("%s src:\n", sp); - printf("%s \"%s\"\n", sp, lexer->src); - printf("%s cchar: \'%c\'\n", sp, *lexer->cchar); - printf("%s ntokens: %ld\n", sp, lexer->ntokens); - printf("%s tokens: [\n", sp); + INDENT_BEGIN(ilvl); + INDENT_TITLE("Lexer", lexer); + INDENT_FIELD_NONL("state"); + lexerstate_print_raw(lexer->state); putchar('\n'); + INDENT_FIELD("srcln", "%ld", lexer->srcln); + INDENT_FIELD_NL("src", "\"%s\"", lexer->src); + INDENT_FIELD("cchar", "'%c'", *lexer->cchar); + INDENT_FIELD("ntokens", "%ld", lexer->ntokens); + printf("%s tokens: [\n", INDENT_spacing->buf); for (int i = 0; i < lexer->ntokens; i++) { token_print_i(lexer->tokens[i], ilvl + 2); @@ -132,20 +127,8 @@ void lexer_print_i(Lexer* lexer, int ilvl) { } } -void lexerstate_print(LexerState s) { lexerstate_print_i(s, 0); } - -void lexerstate_print_i(LexerState s, int ilvl) { - Dstr* spacing = dstr_init(); - - for (int j = 0; j < ilvl; j++) dstr_appendch(spacing, ' '); - - if (s > LEXER_STATE_MAX) { - printf("%sUnknown (%d)\n", spacing->buf, s); - log_dbgf("%d is not a valid LexerSate (max: %d)", s, LEXER_STATE_MAX); - return; - } - - printf("%s%s\n", spacing->buf, lexerstate_names[s]); - - dstr_destroy(spacing); +void lexerstate_print_raw(LexerState s) { + if (s > LEXER_STATE_MAX) + printf("Unknown (%d)", s) && log_dbgf("%d is not a valid LexerState (max: %d)", s, TOKEN_TYPE_MAX) + else printf("%s", lexerstate_names[s]); } diff --git a/src/token.c b/src/token.c index 3a537f7..553435b 100644 --- a/src/token.c +++ b/src/token.c @@ -26,29 +26,15 @@ void token_destroy(Token* t) { void token_print(Token* token) { token_print_i(token, 0); } -#if 0 -void token_print_i(Token *token, int ilvl) { - Dstr* spacing = dstr_init(); - for (int j = 0; j < ilvl; j++) dstr_appendch(spacing, ' '); - - printf("%sToken @ %p\n", spacing->buf, token); - printf("%s type:\n", spacing->buf); - tokentype_print_i(token->type, ilvl+1); - printf("%s valn:\n", spacing->buf); - printf("%s %ld\n", spacing->buf, token->valn); - printf("%s val:\n", spacing->buf); - printf("%s \"%s\"\n", spacing->buf, token->val); - - // : -} -#endif - void token_print_i(Token *token, int ilvl) { INDENT_BEGIN(ilvl); INDENT_TITLE("Token", token); INDENT_FIELD_NONL("type"); tokentype_print_raw(token->type); + putchar('\n'); + INDENT_FIELD("valn", "%ld", token->valn); + INDENT_FIELD_NL("val", "\"%s\"", token->val); } void tokentype_print_raw(TokenType t) {