Compare commits

..

No commits in common. "68fc644ea61ef37a7327622b01ec4bb1152bb0cc" and "7a04ccfd9f9843877c718a8b296ea1bb4d04ba1d" have entirely different histories.

6 changed files with 54 additions and 47 deletions

View File

@ -33,7 +33,7 @@ release: CFLAGS = -Wall -O2
release: $(TARGET) release: $(TARGET)
run: $(TARGET) run: $(TARGET)
@ echo -e "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)" @ echo "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)"
@ ./$(TARGET) @ ./$(TARGET)
# Link to final binary. # Link to final binary.

View File

@ -1,23 +0,0 @@
#ifndef AST_H
#define AST_H
typedef enum {
AST_TYPE_NUM,
AST_TYPE,CALL
} ASTType;
typedef struct {
ASTType type;
void* data;
} AST;
typedef struct {
int val;
} ASTTypeNum;
typedef struct {
char* to;
char** args;
} ASTTypeCall;
#endif

View File

@ -65,6 +65,9 @@ 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_raw(LexerState s); void lexerstate_print(LexerState s);
// Print a representation of a LexerState at the specified indentation level.
void lexerstate_print_i(LexerState s, int ilvl);
#endif #endif

View File

@ -24,20 +24,16 @@
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__);
// 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 " 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);

View File

@ -111,15 +111,20 @@ 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) {
INDENT_BEGIN(ilvl); Dstr* spacing = dstr_init();
INDENT_TITLE("Lexer", lexer); char* sp = spacing->buf;
INDENT_FIELD_NONL("state"); for (int i = 0; i < ilvl; i++) dstr_appendch(spacing, ' ');
lexerstate_print_raw(lexer->state); putchar('\n');
INDENT_FIELD("srcln", "%ld", lexer->srcln); printf("%sLexer @ %p\n", sp, lexer);
INDENT_FIELD_NL("src", "\"%s\"", lexer->src); printf("%s state:\n", sp);
INDENT_FIELD("cchar", "'%c'", *lexer->cchar); lexerstate_print_i(lexer->state, ilvl + 2);
INDENT_FIELD("ntokens", "%ld", lexer->ntokens); printf("%s srcln:\n", sp);
printf("%s tokens: [\n", INDENT_spacing->buf); 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);
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);
@ -127,8 +132,20 @@ void lexer_print_i(Lexer* lexer, int ilvl) {
} }
} }
void lexerstate_print_raw(LexerState s) { void lexerstate_print(LexerState s) { lexerstate_print_i(s, 0); }
if (s > LEXER_STATE_MAX)
printf("Unknown (%d)", s) && log_dbgf("%d is not a valid LexerState (max: %d)", s, TOKEN_TYPE_MAX) void lexerstate_print_i(LexerState s, int ilvl) {
else printf("%s", lexerstate_names[s]); 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);
} }

View File

@ -26,15 +26,29 @@ 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) {