4 /* NOTE: it's a test - I'll abort() on epic-failure */
9 /* (note: 'do {} while(0)' forces the need for a semicolon after assert() */
10 #define assert(x) do { if ( !(x) ) { printf("Assertion failed: %s\n", #x); abort(); } } while(0)
12 VECTOR_MAKE(ast_value*, globals);
13 VECTOR_MAKE(ast_function*, functions);
15 #include "ast-macros.h"
35 MKCONSTFLOAT(f0, 0.0);
36 MKCONSTFLOAT(f1, 1.0);
37 MKCONSTFLOAT(f5, 5.0);
47 STATE(ASSIGN(STORE_F, vi, f0));
48 WHILE(BIN(LT, vi, f5));
49 STATE(ASSIGN(STORE_F, vx, BIN(MUL_F, vi, f5)));
50 STATE(ASSIGN(STORE_F, vi, BIN(ADD_F, vi, f1)));
55 ir = ir_builder_new("ast_test");
59 for (i = 0; i < globals_elements; ++i) {
60 if (!ast_global_codegen(globals_data[i], ir)) {
61 assert(!"failed to generate global");
66 for (i = 0; i < functions_elements; ++i) {
67 if (!ast_function_codegen(functions_data[i], ir)) {
68 assert(!"failed to generate function");
70 if (!ir_function_finalize(functions_data[i]->ir_func))
71 assert(!"finalize on function failed...");
76 ir_builder_dump(ir, printf);
78 /* Now create a file */
79 if (!ir_builder_generate(ir, "test_ast.dat"))
80 printf("*** failed to generate code\n");
83 ir_builder_delete(ir);
86 /* Functions must be deleted FIRST since their expressions
87 * reference global variables.
89 for (i = 0; i < functions_elements; ++i) {
90 ast_function_delete(functions_data[i]);
93 mem_d(functions_data);
95 /* We must delete not only globals, but also the functions'
96 * ast_values (their type and name), that's why we added them to the globals vector.
98 for (i = 0; i < globals_elements; ++i) {
99 ast_value_delete(globals_data[i]);