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"
33 /* opts_debug = true; */
35 BUILTIN(print, TYPE_VOID, -1);
36 PARAM(TYPE_STRING, text);
43 VAR(TYPE_STRING, sHello);
44 MKCONSTFLOAT(f0, 0.0);
45 MKCONSTFLOAT(f1, 1.0);
46 MKCONSTFLOAT(f5, 5.0);
47 MKCONSTSTRING(sHello, "Hello, World\n");
49 FUNCTION(foo, TYPE_VOID);
52 FUNCTION(main, TYPE_VOID);
60 STATE(ASSIGN(STORE_F, vi, f0));
61 WHILE(BIN(LT, vi, f5));
62 STATE(ASSIGN(STORE_F, vx, BIN(MUL_F, vi, f5)));
63 STATE(ASSIGN(STORE_F, vi, BIN(ADD_F, vi, f1)));
72 ir = ir_builder_new("ast_test");
76 for (i = 0; i < globals_elements; ++i) {
77 if (!ast_global_codegen(globals_data[i], ir)) {
78 assert(!"failed to generate global");
83 for (i = 0; i < functions_elements; ++i) {
84 if (!ast_function_codegen(functions_data[i], ir)) {
85 assert(!"failed to generate function");
87 if (!ir_function_finalize(functions_data[i]->ir_func))
88 assert(!"finalize on function failed...");
93 ir_builder_dump(ir, printf);
95 /* Now create a file */
96 if (!ir_builder_generate(ir, "test_ast.dat"))
97 printf("*** failed to generate code\n");
100 ir_builder_delete(ir);
103 /* Functions must be deleted FIRST since their expressions
104 * reference global variables.
106 for (i = 0; i < functions_elements; ++i) {
107 ast_function_delete(functions_data[i]);
110 mem_d(functions_data);
112 /* We must delete not only globals, but also the functions'
113 * ast_values (their type and name), that's why we added them to the globals vector.
115 for (i = 0; i < globals_elements; ++i) {
116 ast_value_delete(globals_data[i]);