gemstone/tests/ast/print_graphviz.c

56 lines
1.3 KiB
C
Raw Normal View History

2024-05-08 11:49:06 +00:00
//
// Created by servostar on 5/8/24.
//
#include <ast/ast.h>
#include <sys/log.h>
2024-06-05 18:00:14 +00:00
#include <mem/cache.h>
2024-05-08 11:49:06 +00:00
void generate_statement(const AST_NODE_PTR stmt) {
2024-07-02 14:43:59 +00:00
const AST_NODE_PTR add = AST_new_node(empty_location(NULL), AST_Add, NULL);
2024-05-08 11:49:06 +00:00
2024-07-02 14:43:59 +00:00
AST_push_node(add, AST_new_node(empty_location(NULL), AST_Int, "3"));
AST_push_node(add, AST_new_node(empty_location(NULL), AST_Int, "6"));
2024-05-08 11:49:06 +00:00
AST_push_node(stmt, add);
}
void generate_branch(const AST_NODE_PTR stmt) {
2024-07-02 14:43:59 +00:00
const AST_NODE_PTR branch = AST_new_node(empty_location(NULL), AST_If, NULL);
const AST_NODE_PTR gt = AST_new_node(empty_location(NULL), AST_Greater, NULL);
2024-05-08 11:49:06 +00:00
AST_push_node(branch, gt);
2024-07-02 14:43:59 +00:00
AST_push_node(gt, AST_new_node(empty_location(NULL), AST_Float, "2.3"));
AST_push_node(gt, AST_new_node(empty_location(NULL), AST_Float, "0.79"));
2024-05-08 11:49:06 +00:00
AST_push_node(stmt, branch);
generate_statement(branch);
}
int main(void) {
2024-06-05 18:00:14 +00:00
mem_init();
2024-05-08 11:49:06 +00:00
AST_init();
2024-07-02 14:43:59 +00:00
const AST_NODE_PTR root = AST_new_node(empty_location(NULL), AST_Stmt, NULL);
2024-05-08 11:49:06 +00:00
generate_branch(root);
FILE* output = fopen("tmp/graph.gv", "w");
if (output == NULL) {
PANIC("unable to open file");
}
AST_fprint_graphviz(output, root);
fflush(output);
fclose(output);
AST_delete_node(root);
return 0;
}