]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-test.c
Trailing whitespace was imminent, pending editor configuration change to accomodate...
[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 int main()
16 {
17     ast_expression *exp;
18     ast_value      *gfoo    = NULL;
19     ast_value      *gbar    = NULL;
20     ast_value      *const_3 = NULL;
21     ast_value      *const_4 = NULL;
22     ast_value      *vmain   = NULL;
23     ast_function   *fmain   = NULL;
24     ast_block      *ba      = NULL;
25     ast_value      *li      = NULL;
26
27     size_t i;
28     lex_ctx ctx;
29     ctx.file = NULL;
30     ctx.line = 1;
31
32     /* globals */
33     gfoo = ast_value_new(ctx, "foo", TYPE_FLOAT);
34         assert(gfoo);
35     assert(globals_add(gfoo) >= 0);
36
37     gbar = ast_value_new(ctx, "bar", TYPE_FLOAT);
38         assert(gbar);
39     assert(globals_add(gbar) >= 0);
40
41     const_3 = ast_value_new(ctx, "3", TYPE_FLOAT);
42         assert(const_3);
43     assert(globals_add(const_3) >= 0);
44     const_3->isconst = true;
45     const_3->constval.vfloat = 3.0;
46
47     const_4 = ast_value_new(ctx, "4", TYPE_FLOAT);
48         assert(const_4);
49     assert(globals_add(const_4) >= 0);
50     const_4->isconst = true;
51     const_4->constval.vfloat = 4.0;
52
53     /* defining a function */
54     vmain = ast_value_new(ctx, "main", TYPE_FUNCTION);
55         assert(vmain);
56     assert(globals_add(vmain) >= 0);
57     /* This happens in ast_function_new:
58         vmain->isconst = true;
59         vmain->constval.vfunc = fmain;
60        Creating a function node connects the global to the function.
61        You still have to delete *BOTH*, deleting one will NOT delete the other,
62        it will only unlink them.
63      */
64
65     /* creating a function body */
66     fmain = ast_function_new(ctx, "main", vmain);
67         assert(fmain);
68     assert(functions_add(fmain) >= 0);
69
70     /* { block */
71     ba = ast_block_new(ctx);
72         assert(ba);
73     assert(ast_function_blocks_add(fmain, ba));
74
75     /* local variable i */
76     li = ast_value_new(ctx, "i", TYPE_FLOAT);
77         assert(li);
78     assert(ast_block_locals_add(ba, li));
79
80     /* I realize having to provide the opcode isn't the best way to go, but,
81      * urgh... */
82     /* foo = 3; */
83     exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, gfoo, (ast_expression*)const_3);
84         assert(exp);
85     assert(ast_block_exprs_add(ba, exp));
86     /* bar = 4; */
87     exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, gbar, (ast_expression*)const_4);
88         assert(exp);
89     assert(ast_block_exprs_add(ba, exp));
90
91     /* i = foo * bar */
92     exp = (ast_expression*)ast_binary_new(ctx,  INSTR_MUL_F, (ast_expression*)gbar, (ast_expression*)gfoo);
93         assert(exp);
94     exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, li, exp);
95         assert(exp);
96     assert(ast_block_exprs_add(ba, exp));
97
98     /* } block */
99
100     /* Next up: Having the AST generate IR */
101
102     /* cleanup */
103     /* Functions must be deleted FIRST since their expressions
104      * reference global variables.
105      */
106     for (i = 0; i < functions_elements; ++i) {
107         ast_function_delete(functions_data[i]);
108     }
109     if (functions_data)
110         mem_d(functions_data);
111
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.
114      */
115     for (i = 0; i < globals_elements; ++i) {
116         ast_value_delete(globals_data[i]);
117     }
118     if (globals_data)
119         mem_d(globals_data);
120     return 0;
121 }