]> git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-test.c
0cb0101d947708771d1a6841a4520377f4d0f0fd
[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_value*, fields);
14 VECTOR_MAKE(ast_function*, functions);
15
16 uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
17 uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
18
19 uint32_t    opts_O        = 1;
20 const char *opts_output   = "progs.dat";
21 int         opts_standard = COMPILER_GMQCC;
22 bool        opts_debug    = false;
23 bool        opts_memchk   = false;
24
25 #include "ast-macros.h"
26
27 int main()
28 {
29     size_t i;
30
31     ir_builder     *ir;
32
33     TESTVARS();
34
35     DEFVAR(vi);
36     DEFVAR(vx);
37     DEFVAR(f0);
38     DEFVAR(f1);
39     DEFVAR(f5);
40     DEFVAR(cv3x4x5);
41     DEFVAR(cv1x1x1);
42     DEFVAR(sHello);
43     DEFVAR(sNL);
44     DEFVAR(print);
45     DEFVAR(ftos);
46     DEFVAR(spawn);
47
48     DEFVAR(mema);
49     DEFVAR(memb);
50     DEFVAR(memv);
51     DEFVAR(pawn);
52
53     /* opts_debug = true; */
54
55 BUILTIN(print, TYPE_VOID, -1);
56 PARAM(TYPE_STRING, text);
57 ENDBUILTIN();
58
59 BUILTIN(ftos, TYPE_STRING, -2);
60 PARAM(TYPE_FLOAT, value);
61 ENDBUILTIN();
62
63 BUILTIN(spawn, TYPE_ENTITY, -3);
64 ENDBUILTIN();
65
66     TESTINIT();
67 VAR(TYPE_FLOAT, f0);
68 VAR(TYPE_FLOAT, f1);
69 VAR(TYPE_FLOAT, f5);
70 VAR(TYPE_STRING, sHello);
71 VAR(TYPE_STRING, sNL);
72 VAR(TYPE_VECTOR, cv3x4x5);
73 VAR(TYPE_VECTOR, cv1x1x1);
74
75 FIELD(TYPE_FLOAT, mema);
76 FIELD(TYPE_FLOAT, memb);
77 FIELD(TYPE_VECTOR, memv);
78
79 MKCONSTFLOAT(f0, 0.0);
80 MKCONSTFLOAT(f1, 1.0);
81 MKCONSTFLOAT(f5, 5.0);
82 MKCONSTSTRING(sHello, "Hello, World\n");
83 MKCONSTSTRING(sNL, "\n");
84 MKCONSTVECTOR(cv3x4x5, 3, 4, 5);
85 MKCONSTVECTOR(cv1x1x1, 1, 1, 1);
86
87 FUNCTION(foo, TYPE_VOID);
88 ENDFUNCTION(foo);
89
90 #define PRINTNL() do { CALL(print) CALLPARAM(sNL) ENDCALL(); } while(0)
91
92 FUNCTION(main, TYPE_VOID);
93
94     VAR(TYPE_FLOAT, vi);
95     VAR(TYPE_FLOAT, vx);
96     VAR(TYPE_ENTITY, pawn);
97
98     MKLOCAL(vi);
99     MKLOCAL(vx);
100     MKLOCAL(pawn);
101
102     STATE(ASSIGN(STORE_F, vi, f0));
103     WHILE(BIN(LT, vi, f5));
104         STATE(ASSIGN(STORE_F, vx, BIN(MUL_F, vi, f5)));
105         STATE(ASSIGN(STORE_F, vi, BIN(ADD_F, vi, f1)));
106     ENDWHILE();
107
108     CALL(print)
109     CALLPARAM(sHello)
110     ENDCALL();
111
112     CALL(spawn)
113     ENDCALLWITH(newent, STATE(ASSIGN(STORE_ENT, pawn, newent)));
114
115     STATE(ASSIGN(STOREP_F, ENTFIELD(pawn, mema), f5));
116     STATE(ASSIGN(STOREP_F, ENTFIELD(pawn, memb), f1));
117     STATE(ASSIGN(STOREP_V, ENTFIELD(pawn, memv), cv3x4x5));
118     CALL(ftos)
119     CALLPARAM(ENTFIELD(pawn, mema))
120     ENDCALLWITH(output,
121         CALL(print)
122         CALLPARAM(output)
123         CALLPARAM(sNL)
124         ENDCALL();
125     );
126     CALL(ftos)
127     CALLPARAM(ENTFIELD(pawn, memb))
128     ENDCALLWITH(output,
129         CALL(print)
130         CALLPARAM(output)
131         CALLPARAM(sNL)
132         ENDCALL();
133     );
134
135 ENDFUNCTION(main);
136
137     ir = ir_builder_new("ast_test");
138     assert(ir);
139
140     /* gen fields */
141     for (i = 0; i < fields_elements; ++i) {
142         if (!ast_global_codegen(fields_data[i], ir)) {
143             assert(!"failed to generate field");
144         }
145     }
146     /* gen globals */
147     for (i = 0; i < globals_elements; ++i) {
148         if (!ast_global_codegen(globals_data[i], ir)) {
149             assert(!"failed to generate global");
150         }
151     }
152
153     /* gen functions */
154     for (i = 0; i < functions_elements; ++i) {
155         if (!ast_function_codegen(functions_data[i], ir)) {
156             assert(!"failed to generate function");
157         }
158         if (!ir_function_finalize(functions_data[i]->ir_func))
159             assert(!"finalize on function failed...");
160     }
161
162
163     /* dump */
164     ir_builder_dump(ir, printf);
165
166     /* Now create a file */
167     if (!ir_builder_generate(ir, "test_ast.dat"))
168         printf("*** failed to generate code\n");
169
170     /* ir cleanup */
171     ir_builder_delete(ir);
172
173     /* cleanup */
174     /* Functions must be deleted FIRST since their expressions
175      * reference global variables.
176      */
177     for (i = 0; i < functions_elements; ++i) {
178         ast_function_delete(functions_data[i]);
179     }
180     if (functions_data)
181         mem_d(functions_data);
182
183     /* We must delete not only globals, but also the functions'
184      * ast_values (their type and name), that's why we added them to the globals vector.
185      */
186     for (i = 0; i < globals_elements; ++i) {
187         ast_value_delete(globals_data[i]);
188     }
189     if (globals_data)
190         mem_d(globals_data);
191     return 0;
192 }