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