]> git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-test.c
Deleting the manually written ast-test which was '#if 0'-ed out
[xonotic/gmqcc.git] / test / ast-test.c
1 #include "gmqcc.h"
2 #include "ast.h"
3
4 /* NOTE: it's a test - I'll abort() on epic-failure */
5
6 #ifdef assert
7 #   undef assert
8 #endif
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)
11
12 VECTOR_MAKE(ast_value*, globals);
13 VECTOR_MAKE(ast_function*, functions);
14
15 #include "ast-macros.h"
16
17 int main()
18 {
19     size_t i;
20
21     ir_builder     *ir;
22
23     TESTVARS();
24
25     DEFVAR(vi);
26     DEFVAR(vx);
27     DEFVAR(f0);
28     DEFVAR(f1);
29     DEFVAR(f5);
30
31     TESTINIT();
32 VAR(TYPE_FLOAT, f0);
33 VAR(TYPE_FLOAT, f1);
34 VAR(TYPE_FLOAT, f5);
35 MKCONSTFLOAT(f0, 0.0);
36 MKCONSTFLOAT(f1, 1.0);
37 MKCONSTFLOAT(f5, 5.0);
38
39 FUNCTION(main);
40
41 VAR(TYPE_FLOAT, vi);
42 VAR(TYPE_FLOAT, vx);
43
44 MKLOCAL(vi);
45 MKLOCAL(vx);
46
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)));
51 ENDWHILE();
52
53 ENDFUNCTION(main);
54
55     ir = ir_builder_new("ast_test");
56     assert(ir);
57
58     /* gen globals */
59     for (i = 0; i < globals_elements; ++i) {
60         if (!ast_global_codegen(globals_data[i], ir)) {
61             assert(!"failed to generate global");
62         }
63     }
64
65     /* gen functions */
66     for (i = 0; i < functions_elements; ++i) {
67         if (!ast_function_codegen(functions_data[i], ir)) {
68             assert(!"failed to generate function");
69         }
70         if (!ir_function_finalize(functions_data[i]->ir_func))
71             assert(!"finalize on function failed...");
72     }
73
74
75     /* dump */
76     ir_builder_dump(ir, printf);
77
78     /* Now create a file */
79     if (!ir_builder_generate(ir, "test_ast.dat"))
80         printf("*** failed to generate code\n");
81
82     /* ir cleanup */
83     ir_builder_delete(ir);
84
85     /* cleanup */
86     /* Functions must be deleted FIRST since their expressions
87      * reference global variables.
88      */
89     for (i = 0; i < functions_elements; ++i) {
90         ast_function_delete(functions_data[i]);
91     }
92     if (functions_data)
93         mem_d(functions_data);
94
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.
97      */
98     for (i = 0; i < globals_elements; ++i) {
99         ast_value_delete(globals_data[i]);
100     }
101     if (globals_data)
102         mem_d(globals_data);
103     return 0;
104 }