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_value*, fields);
14 VECTOR_MAKE(ast_function*, functions);
16 uint32_t opts_flags[1 + (COUNT_FLAGS / 32)];
17 uint32_t opts_warn [1 + (COUNT_WARNINGS / 32)];
20 const char *opts_output = "progs.dat";
21 int opts_standard = COMPILER_GMQCC;
22 bool opts_debug = false;
23 bool opts_memchk = false;
25 #include "ast-macros.h"
48 /* opts_debug = true; */
50 BUILTIN(print, TYPE_VOID, -1);
51 PARAM(TYPE_STRING, text);
54 BUILTIN(ftos, TYPE_STRING, -2);
55 PARAM(TYPE_FLOAT, value);
58 BUILTIN(spawn, TYPE_ENTITY, -3);
65 VAR(TYPE_STRING, sHello);
67 FIELD(TYPE_FLOAT, mema);
69 MKCONSTFLOAT(f0, 0.0);
70 MKCONSTFLOAT(f1, 1.0);
71 MKCONSTFLOAT(f5, 5.0);
72 MKCONSTSTRING(sHello, "Hello, World\n");
74 FUNCTION(foo, TYPE_VOID);
77 FUNCTION(main, TYPE_VOID);
81 VAR(TYPE_ENTITY, pawn);
87 STATE(ASSIGN(STORE_F, vi, f0));
88 WHILE(BIN(LT, vi, f5));
89 STATE(ASSIGN(STORE_F, vx, BIN(MUL_F, vi, f5)));
90 STATE(ASSIGN(STORE_F, vi, BIN(ADD_F, vi, f1)));
98 ENDCALLWITH(newent, STATE(ASSIGN(STORE_ENT, pawn, newent)));
100 STATE(ASSIGN(STOREP_F, ENTFIELD(pawn, mema), f5));
102 CALLPARAM(ENTFIELD(pawn, mema))
104 ENDCALLWITH(output, STATE(ASSIGN(STORE_F, vi, output)));
119 ir = ir_builder_new("ast_test");
123 for (i = 0; i < fields_elements; ++i) {
124 if (!ast_global_codegen(fields_data[i], ir)) {
125 assert(!"failed to generate field");
129 for (i = 0; i < globals_elements; ++i) {
130 if (!ast_global_codegen(globals_data[i], ir)) {
131 assert(!"failed to generate global");
136 for (i = 0; i < functions_elements; ++i) {
137 if (!ast_function_codegen(functions_data[i], ir)) {
138 assert(!"failed to generate function");
140 if (!ir_function_finalize(functions_data[i]->ir_func))
141 assert(!"finalize on function failed...");
146 ir_builder_dump(ir, printf);
148 /* Now create a file */
149 if (!ir_builder_generate(ir, "test_ast.dat"))
150 printf("*** failed to generate code\n");
153 ir_builder_delete(ir);
156 /* Functions must be deleted FIRST since their expressions
157 * reference global variables.
159 for (i = 0; i < functions_elements; ++i) {
160 ast_function_delete(functions_data[i]);
163 mem_d(functions_data);
165 /* We must delete not only globals, but also the functions'
166 * ast_values (their type and name), that's why we added them to the globals vector.
168 for (i = 0; i < globals_elements; ++i) {
169 ast_value_delete(globals_data[i]);