Fixed more printing, added basic ast.
This commit is contained in:
parent
7a04ccfd9f
commit
ca9d2aabe4
2
Makefile
2
Makefile
@ -33,7 +33,7 @@ release: CFLAGS = -Wall -O2
|
|||||||
release: $(TARGET)
|
release: $(TARGET)
|
||||||
|
|
||||||
run: $(TARGET)
|
run: $(TARGET)
|
||||||
@ echo "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)"
|
@ echo -e "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)"
|
||||||
@ ./$(TARGET)
|
@ ./$(TARGET)
|
||||||
|
|
||||||
# Link to final binary.
|
# Link to final binary.
|
||||||
|
8
src/include/ast.h
Normal file
8
src/include/ast.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef AST_H
|
||||||
|
#define AST_H
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
|
||||||
|
} AST;
|
||||||
|
|
||||||
|
#endif
|
@ -65,9 +65,6 @@ void lexer_print(Lexer* lexer);
|
|||||||
void lexer_print_i(Lexer* lexer, int ilvl);
|
void lexer_print_i(Lexer* lexer, int ilvl);
|
||||||
|
|
||||||
// Print a representation of a LexerState.
|
// Print a representation of a LexerState.
|
||||||
void lexerstate_print(LexerState s);
|
void lexerstate_print_raw(LexerState s);
|
||||||
|
|
||||||
// Print a representation of a LexerState at the specified indentation level.
|
|
||||||
void lexerstate_print_i(LexerState s, int ilvl);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -24,16 +24,20 @@
|
|||||||
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.
|
||||||
#define INDENT_TITLE(THING, WHERE) \
|
#define INDENT_TITLE(THING, WHERE) \
|
||||||
printf("%s" THING " @ %p\n", INDENT_spacing->buf, WHERE);
|
printf("%s" THING " @ %p\n", INDENT_spacing->buf, WHERE);
|
||||||
|
|
||||||
|
// 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 " 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, \
|
printf("%s " FIELD ":\n %s " VAL "\n", INDENT_spacing->buf, \
|
||||||
INDENT_spacing->buf, __VA_ARGS__);
|
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_FIELD_NONL(FIELD) printf("%s " FIELD ": ", INDENT_spacing->buf);
|
||||||
|
|
||||||
#define INDENT_END dstr_destroy(INDENT_spacing);
|
#define INDENT_END dstr_destroy(INDENT_spacing);
|
||||||
|
43
src/lexer.c
43
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(Lexer* lexer) { lexer_print_i(lexer, 0); }
|
||||||
|
|
||||||
void lexer_print_i(Lexer* lexer, int ilvl) {
|
void lexer_print_i(Lexer* lexer, int ilvl) {
|
||||||
Dstr* spacing = dstr_init();
|
INDENT_BEGIN(ilvl);
|
||||||
char* sp = spacing->buf;
|
INDENT_TITLE("Lexer", lexer);
|
||||||
for (int i = 0; i < ilvl; i++) dstr_appendch(spacing, ' ');
|
INDENT_FIELD_NONL("state");
|
||||||
|
lexerstate_print_raw(lexer->state); putchar('\n');
|
||||||
printf("%sLexer @ %p\n", sp, lexer);
|
INDENT_FIELD("srcln", "%ld", lexer->srcln);
|
||||||
printf("%s state:\n", sp);
|
INDENT_FIELD_NL("src", "\"%s\"", lexer->src);
|
||||||
lexerstate_print_i(lexer->state, ilvl + 2);
|
INDENT_FIELD("cchar", "'%c'", *lexer->cchar);
|
||||||
printf("%s srcln:\n", sp);
|
INDENT_FIELD("ntokens", "%ld", lexer->ntokens);
|
||||||
printf("%s %ld\n", sp, lexer->srcln);
|
printf("%s tokens: [\n", INDENT_spacing->buf);
|
||||||
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);
|
|
||||||
|
|
||||||
for (int i = 0; i < lexer->ntokens; i++) {
|
for (int i = 0; i < lexer->ntokens; i++) {
|
||||||
token_print_i(lexer->tokens[i], ilvl + 2);
|
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_raw(LexerState s) {
|
||||||
|
if (s > LEXER_STATE_MAX)
|
||||||
void lexerstate_print_i(LexerState s, int ilvl) {
|
printf("Unknown (%d)", s) && log_dbgf("%d is not a valid LexerState (max: %d)", s, TOKEN_TYPE_MAX)
|
||||||
Dstr* spacing = dstr_init();
|
else printf("%s", lexerstate_names[s]);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
20
src/token.c
20
src/token.c
@ -26,29 +26,15 @@ void token_destroy(Token* t) {
|
|||||||
|
|
||||||
void token_print(Token* token) { token_print_i(token, 0); }
|
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) {
|
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("type");
|
||||||
tokentype_print_raw(token->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) {
|
void tokentype_print_raw(TokenType t) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user