FUCK YEAH, it works, it was the is_valid thing fo slices that was inverted, did the test by brute force and damn, is the expected AST

This commit is contained in:
2026-03-25 10:22:54 -06:00
parent f24671bd19
commit 845673fb0e
4 changed files with 50 additions and 34 deletions

View File

@@ -9,7 +9,6 @@ int main(void) {
tokenize("3 + 4 * 5", &context);
AST tree = parse(&context);
print_AST(tree);
printf("Hola\n");

View File

@@ -74,7 +74,7 @@ ASTNode *parse_expr(ASTNodeSlice *slice, uint8_t min_bp) {
*left_side = ASTNodeSlice_next(slice);
while (true) {
if (ASTNodeSlice_is_valid(slice)) {
if (!ASTNodeSlice_is_valid(slice)) {
break;
}
@@ -102,35 +102,5 @@ ASTNode *parse_expr(ASTNodeSlice *slice, uint8_t min_bp) {
return left_side;
}
void print_AST(AST tree) {
print_node(tree.head, 0);
}
void print_node(ASTNode *node, int depth) {
if (node == NULL) {
return;
}
if (node->type == NODE_BINARY_OP) {
print_node(node->data.binary.right, depth + 1);
}
for (int i = 0; i < depth; i++) {
printf(" ");
}
switch (node->type) {
case NODE_INTEGER:
printf("%ld\n", node->data.integer);
break;
case NODE_BINARY_OP:
printf("%c\n", operator_to_char(node->data.binary.op));
break;
}
if (node->type == NODE_BINARY_OP) {
print_node(node->data.binary.left, depth + 1);
}
}