Fixed block parsing for real this time.

I am such an idiot, ast_block_data_init() just ignored the given array
lol.
This commit is contained in:
Jacob Signorovitch 2025-02-04 16:50:04 -05:00
parent be3baee74e
commit 78e31c3e27
6 changed files with 56 additions and 37 deletions

View File

@ -7,10 +7,8 @@
extern AST* root; extern AST* root;
static char* asttype_names[] = { static char* asttype_names[] = {
[AST_TYPE_CALL] = "FUNC CALL", [AST_TYPE_CALL] = "FUNC CALL", [AST_TYPE_NUM] = "NUMBER",
[AST_TYPE_NUM] = "NUMBER", [AST_TYPE_VREF] = "VAR REFERENCE", [AST_TYPE_VDEF] = "VAR DEFINITION",
[AST_TYPE_VREF] = "VAR REFERENCE",
[AST_TYPE_VDEF] = "VAR DEFINITION",
[AST_TYPE_BLOCK] = "BLOCK", [AST_TYPE_BLOCK] = "BLOCK",
}; };
@ -50,10 +48,11 @@ void ast_print_i(AST* ast, int i) {
case AST_TYPE_NUM: case AST_TYPE_NUM:
printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data); printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data);
break; break;
case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break; case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break;
case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break; case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break;
case AST_TYPE_VDEF: ast_vdef_print(ast->data, i + 2); break; case AST_TYPE_VDEF: ast_vdef_print(ast->data, i + 2); break;
default: exit(1); case AST_TYPE_BLOCK: ast_block_print(ast->data, i + 2); break;
default: exit(1);
} }
INDENT_FIELD_NONL_END; INDENT_FIELD_NONL_END;
INDENT_END; INDENT_END;
@ -129,7 +128,7 @@ void ast_vdef_print(ASTVDefData* vdef, int depth) {
INDENT_TITLE("ASTVDefData", vdef); INDENT_TITLE("ASTVDefData", vdef);
INDENT_FIELD("name", "%s", vdef->name); INDENT_FIELD("name", "%s", vdef->name);
INDENT_FIELD_EXT_NONL_START("val"); INDENT_FIELD_EXT_NONL_START("val");
ast_print_i(vdef->val, depth + 2); // 2 because already indented. ast_print_i(vdef->val, depth + 2); // 2 because already indented.
INDENT_FIELD_NONL_END; INDENT_FIELD_NONL_END;
INDENT_END; INDENT_END;
@ -160,25 +159,23 @@ void ast_vref_print(ASTVrefData* data, int i) {
ASTBlockData* ast_block_data_init(AST** inside, size_t ln) { ASTBlockData* ast_block_data_init(AST** inside, size_t ln) {
ASTBlockData* block = malloc(sizeof(ASTBlockData)); ASTBlockData* block = malloc(sizeof(ASTBlockData));
block->inside = calloc(ln, sizeof(AST)); block->inside = inside;
block->ln = ln; block->ln = ln;
return block; return block;
} }
void ast_block_data_destroy(ASTBlockData* block) { void ast_block_data_destroy(ASTBlockData* block) {
for (size_t i = 0; i < block->ln; i++) { for (size_t i = 0; i < block->ln; i++) { ast_destroy(block->inside[i]); }
ast_destroy(block->inside[i]);
}
free(block->inside); free(block->inside);
free(block); free(block);
} }
void ast_block_data_print(ASTBlockData* data, int depth) { void ast_block_print(ASTBlockData* data, int depth) {
INDENT_BEGIN(depth); INDENT_BEGIN(depth);
INDENT_TITLE("BLOCK", data); INDENT_TITLE("ASTBlockData", data);
INDENT_FIELD("ln", "%ld", data->ln); INDENT_FIELD("ln", "%ld", data->ln);
INDENT_FIELD_LIST("inside", data->inside, data->ln, ast_print_i); INDENT_FIELD_LIST("inside", data->inside, data->ln, ast_print_i);

View File

@ -28,7 +28,8 @@ void dlist_check_resz(DList* dlist) {
dlist->sz *= 2; dlist->sz *= 2;
dlist->buf = realloc(dlist->buf, dlist->sz); dlist->buf = realloc(dlist->buf, dlist->sz);
log_dbgf( log_dbgf(
"dlist @ %p doubled from %ld to %ld", dlist, dlist->sz / 2, dlist->sz "dlist @ %p doubled from %ld to %ld", dlist, dlist->sz / 2,
dlist->sz
); );
} }
} }
@ -36,6 +37,8 @@ void dlist_check_resz(DList* dlist) {
void dlist_append(DList* dest, void* src) { void dlist_append(DList* dest, void* src) {
dlist_check_resz(dest); dlist_check_resz(dest);
log_dbgf("added %p to dlist@%p", src, dest);
dest->buf[dest->ln] = src; dest->buf[dest->ln] = src;
dest->ln++; dest->ln++;
} }

View File

@ -100,11 +100,10 @@ block:
exp: exp:
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); } NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
| BLOCKS exp BLOCKE { $$ = $2; } //| BLOCKS exp BLOCKE { $$ = $2; }
| BLOCKS block BLOCKE { | BLOCKS block BLOCKE {
size_t i = $2->ln - 1; $$ = ast_init(AST_TYPE_BLOCK, ast_block_data_init($2->buf, $2->ln));
$$ = $2->buf[i];
} }
| SUB exp { | SUB exp {

View File

@ -66,6 +66,6 @@ typedef struct {
ASTBlockData* ast_block_data_init(AST** inside, size_t ln); ASTBlockData* ast_block_data_init(AST** inside, size_t ln);
// Destroy a block. Also destroy all ASTs inside. // Destroy a block. Also destroy all ASTs inside.
void ast_block_data_destroy(ASTBlockData* block); void ast_block_data_destroy(ASTBlockData* block);
void ast_block_data_print(ASTBlockData*, int i); void ast_block_print(ASTBlockData*, int i);
#endif #endif

View File

@ -5,13 +5,17 @@
// Log a message. // Log a message.
#define log_dbg(msg) \ #define log_dbg(msg) \
printf("\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \ printf( \
__func__); "\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \
__func__ \
);
// Log a message with formatting. // Log a message with formatting.
#define log_dbgf(msg, ...) \ #define log_dbgf(msg, ...) \
printf("\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \ printf( \
__func__, __VA_ARGS__); "\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \
__func__, __VA_ARGS__ \
);
#else // ifdef DBG #else // ifdef DBG
#define log_dbg(msg) #define log_dbg(msg)
@ -50,23 +54,30 @@
// Print & indent the title of a section. // Print & indent the title of a section.
#define INDENT_TITLE(THING, WHERE) \ #define INDENT_TITLE(THING, WHERE) \
printf("%s" COL_BCYA THING COL_RESET " @" COL_MAG " %p\n" COL_RESET, INDENT_spacing->buf, WHERE); printf( \
"%s" COL_BCYA THING COL_RESET " @" COL_MAG " %p\n" COL_RESET, \
INDENT_spacing->buf, WHERE \
);
// Print & indent a thing. // Print & indent a thing.
#define INDENT_FIELD(FIELD, VAL, ...) \ #define INDENT_FIELD(FIELD, VAL, ...) \
printf("%s " COL_BWHI FIELD ": " COL_RESET COL_WHI VAL COL_RESET "\n", \ printf( \
INDENT_spacing->buf, __VA_ARGS__); "%s " COL_BWHI FIELD ": " COL_RESET COL_WHI VAL COL_RESET "\n", \
INDENT_spacing->buf, __VA_ARGS__ \
);
// Print & indent a thing with a newline before the val. // Print & indent a thing with a newline before the val.
#define INDENT_FIELD_NL(FIELD, VAL, ...) \ #define INDENT_FIELD_NL(FIELD, VAL, ...) \
printf("%s " COL_BWHI FIELD ":" COL_RESET "\n %s " COL_WHI VAL COL_RESET \ printf( \
"\n", \ "%s " COL_BWHI FIELD ":" COL_RESET "\n %s " COL_WHI VAL COL_RESET \
INDENT_spacing->buf, INDENT_spacing->buf, __VA_ARGS__); "\n", \
INDENT_spacing->buf, INDENT_spacing->buf, __VA_ARGS__ \
);
// Print & indent a thing without any newline. // Print & indent a thing without any newline.
#define INDENT_FIELD_EXT_NONL_START(FIELD) \ #define INDENT_FIELD_EXT_NONL_START(FIELD) \
printf("%s " COL_BWHI FIELD ":\n" COL_RESET COL_WHI, INDENT_spacing->buf); printf("%s " COL_BWHI FIELD ":\n" COL_RESET COL_WHI, INDENT_spacing->buf);
#define INDENT_FIELD_NONL_END printf( "\n" COL_RESET); #define INDENT_FIELD_NONL_END printf("\n" COL_RESET);
// Print an array A of N things, by calling the function F. // Print an array A of N things, by calling the function F.
#define INDENT_FIELD_LIST(FIELD, A, N, F) \ #define INDENT_FIELD_LIST(FIELD, A, N, F) \
@ -77,7 +88,15 @@
printf(COL_BWHI "%s ]\n" COL_RESET, INDENT_spacing->buf); printf(COL_BWHI "%s ]\n" COL_RESET, INDENT_spacing->buf);
// End an indent block. // End an indent block.
#define INDENT_END printf(COL_RESET); dstr_destroy(INDENT_spacing); #define INDENT_END \
printf(COL_RESET); \
dstr_destroy(INDENT_spacing);
#define INDENT_FIELD_LIST_OPEN(FIELD) \
printf("%s " COL_BWHI FIELD ": [\n" COL_RESET, INDENT_spacing->buf);
#define INDENT_FIELD_LIST_CLOSE \
printf(COL_BWHI "%s ]\n" COL_RESET, INDENT_spacing->buf);
// Allocate a pointer with a type. // Allocate a pointer with a type.
#define talloc(T, X) T* X = malloc(sizeof(T)); #define talloc(T, X) T* X = malloc(sizeof(T));

View File

@ -37,7 +37,7 @@ int main(int argc, char** argv) {
switch (c) { switch (c) {
case EOF: dstr_destroy(ln); goto lnskip; case EOF: dstr_destroy(ln); goto lnskip;
case '\n': goto lnend; case '\n': goto lnend;
default: dstr_appendch(ln, c); log_dbgf("cchar: %c", c); default: dstr_appendch(ln, c);
} }
} while (1); } while (1);
@ -51,10 +51,11 @@ int main(int argc, char** argv) {
log_dbg("Parsed successfully!\n"); log_dbg("Parsed successfully!\n");
} else printf("Parse error.\n"); } else printf("Parse error.\n");
#ifdef DBG
ast_print(root);
#endif
exec_print(exec_start(root)); exec_print(exec_start(root));
#ifdef DBG
ast_print(root);
#endif
ast_destroy(root); ast_destroy(root);
} }