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)));
68 ir = ir_builder_new("ast_test");
72 for (i = 0; i < globals_elements; ++i) {
73 if (!ast_global_codegen(globals_data[i], ir)) {
74 assert(!"failed to generate global");
79 for (i = 0; i < functions_elements; ++i) {
80 if (!ast_function_codegen(functions_data[i], ir)) {
81 assert(!"failed to generate function");
83 if (!ir_function_finalize(functions_data[i]->ir_func))
84 assert(!"finalize on function failed...");
89 ir_builder_dump(ir, printf);
91 /* Now create a file */
92 if (!ir_builder_generate(ir, "test_ast.dat"))
93 printf("*** failed to generate code\n");
96 ir_builder_delete(ir);
99 /* Functions must be deleted FIRST since their expressions
100 * reference global variables.
102 for (i = 0; i < functions_elements; ++i) {
103 ast_function_delete(functions_data[i]);
106 mem_d(functions_data);
108 /* We must delete not only globals, but also the functions'
109 * ast_values (their type and name), that's why we added them to the globals vector.
111 for (i = 0; i < globals_elements; ++i) {
112 ast_value_delete(globals_data[i]);