Fixed more printing, added basic ast.

This commit is contained in:
Jacob Signorovitch 2024-10-25 11:20:07 -04:00
parent 7a04ccfd9f
commit ca9d2aabe4
6 changed files with 32 additions and 54 deletions

View File

@ -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.

8
src/include/ast.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef AST_H
#define AST_H
typedef struct {
} AST;
#endif

View File

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

View File

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

View File

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

View File

@ -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);
// <spacing> <field>:<value>
}
#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) {