Compare commits
2 Commits
7a04ccfd9f
...
68fc644ea6
Author | SHA1 | Date | |
---|---|---|---|
68fc644ea6 | |||
ca9d2aabe4 |
2
Makefile
2
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.
|
||||
|
23
src/include/ast.h
Normal file
23
src/include/ast.h
Normal file
@ -0,0 +1,23 @@
|
||||
#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
|
@ -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
|
||||
|
@ -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);
|
||||
|
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_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]);
|
||||
}
|
||||
|
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); }
|
||||
|
||||
#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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user