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;
static char* asttype_names[] = {
[AST_TYPE_CALL] = "FUNC CALL",
[AST_TYPE_NUM] = "NUMBER",
[AST_TYPE_VREF] = "VAR REFERENCE",
[AST_TYPE_VDEF] = "VAR DEFINITION",
[AST_TYPE_CALL] = "FUNC CALL", [AST_TYPE_NUM] = "NUMBER",
[AST_TYPE_VREF] = "VAR REFERENCE", [AST_TYPE_VDEF] = "VAR DEFINITION",
[AST_TYPE_BLOCK] = "BLOCK",
};
@ -50,10 +48,11 @@ void ast_print_i(AST* ast, int i) {
case AST_TYPE_NUM:
printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data);
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_VDEF: ast_vdef_print(ast->data, i + 2); break;
default: exit(1);
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_VDEF: ast_vdef_print(ast->data, i + 2); break;
case AST_TYPE_BLOCK: ast_block_print(ast->data, i + 2); break;
default: exit(1);
}
INDENT_FIELD_NONL_END;
INDENT_END;
@ -129,7 +128,7 @@ void ast_vdef_print(ASTVDefData* vdef, int depth) {
INDENT_TITLE("ASTVDefData", vdef);
INDENT_FIELD("name", "%s", vdef->name);
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_END;
@ -160,25 +159,23 @@ void ast_vref_print(ASTVrefData* data, int i) {
ASTBlockData* ast_block_data_init(AST** inside, size_t ln) {
ASTBlockData* block = malloc(sizeof(ASTBlockData));
block->inside = calloc(ln, sizeof(AST));
block->inside = inside;
block->ln = ln;
return block;
}
void ast_block_data_destroy(ASTBlockData* block) {
for (size_t i = 0; i < block->ln; i++) {
ast_destroy(block->inside[i]);
}
for (size_t i = 0; i < block->ln; i++) { ast_destroy(block->inside[i]); }
free(block->inside);
free(block);
}
void ast_block_data_print(ASTBlockData* data, int depth) {
void ast_block_print(ASTBlockData* data, int depth) {
INDENT_BEGIN(depth);
INDENT_TITLE("BLOCK", data);
INDENT_TITLE("ASTBlockData", data);
INDENT_FIELD("ln", "%ld", data->ln);
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->buf = realloc(dlist->buf, dlist->sz);
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) {
dlist_check_resz(dest);
log_dbgf("added %p to dlist@%p", src, dest);
dest->buf[dest->ln] = src;
dest->ln++;
}

View File

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

View File

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

View File

@ -5,13 +5,17 @@
// Log a message.
#define log_dbg(msg) \
printf("\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \
__func__);
printf( \
"\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \
__func__ \
);
// Log a message with formatting.
#define log_dbgf(msg, ...) \
printf("\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \
__func__, __VA_ARGS__);
printf( \
"\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \
__func__, __VA_ARGS__ \
);
#else // ifdef DBG
#define log_dbg(msg)
@ -50,23 +54,30 @@
// Print & indent the title of a section.
#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.
#define INDENT_FIELD(FIELD, VAL, ...) \
printf("%s " COL_BWHI FIELD ": " COL_RESET COL_WHI VAL COL_RESET "\n", \
INDENT_spacing->buf, __VA_ARGS__);
printf( \
"%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.
#define INDENT_FIELD_NL(FIELD, VAL, ...) \
printf("%s " COL_BWHI FIELD ":" COL_RESET "\n %s " COL_WHI VAL COL_RESET \
"\n", \
INDENT_spacing->buf, INDENT_spacing->buf, __VA_ARGS__);
printf( \
"%s " COL_BWHI FIELD ":" COL_RESET "\n %s " COL_WHI VAL COL_RESET \
"\n", \
INDENT_spacing->buf, INDENT_spacing->buf, __VA_ARGS__ \
);
// 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);
#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.
#define INDENT_FIELD_LIST(FIELD, A, N, F) \
@ -77,7 +88,15 @@
printf(COL_BWHI "%s ]\n" COL_RESET, INDENT_spacing->buf);
// 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.
#define talloc(T, X) T* X = malloc(sizeof(T));

View File

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