]> git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-macros.h
Working vector-field test
[xonotic/gmqcc.git] / test / ast-macros.h
1 #ifndef TEST_AST_MACROS_HDR
2 #define TEST_AST_MACROS_HDR
3
4 #define TESTVARS()   \
5 ast_block *curblock; \
6 lex_ctx    ctx
7
8 #define TESTINIT()   \
9 ctx.file = NULL;     \
10 ctx.line = 1;
11
12 #define DEFVAR(name) \
13 ast_value *name
14
15 #define VAR(type, name) \
16 name = ast_value_new(ctx, #name, type)
17
18 #define VARnamed(type, name, varname) \
19 name = ast_value_new(ctx, #varname, type)
20
21 #define MKGLOBAL(name) \
22 assert(globals_add(name) >= 0)
23
24 #define FIELD(type, name) \
25 name = ast_value_new(ctx, #name, TYPE_FIELD);                  \
26 do {                                                           \
27     ast_value *field_##name = ast_value_new(ctx, #name, type); \
28     name->expression.next = (ast_expression*)field_##name;     \
29     MKFIELD(name);                                             \
30 } while (0)
31
32 #define MKFIELD(name) \
33 assert(fields_add(name) >= 0)
34
35 #define MKCONSTFLOAT(name, value)  \
36 do {                               \
37     name->isconst = true;          \
38     name->constval.vfloat = value; \
39     MKGLOBAL(name);                \
40 } while(0)
41
42 #define MKCONSTSTRING(name, value)               \
43 do {                                             \
44     name->isconst = true;                        \
45     name->constval.vstring = util_strdup(value); \
46     MKGLOBAL(name);                              \
47 } while(0)
48
49 #define MKCONSTVECTOR(name, valx, valy, valz) \
50 do {                                          \
51     name->isconst = true;                     \
52     name->constval.vvec.x = (valx);           \
53     name->constval.vvec.y = (valy);           \
54     name->constval.vvec.z = (valz);           \
55     MKGLOBAL(name);                           \
56 } while(0)
57
58 #define STATE(a)                                 \
59 do {                                             \
60     ast_expression *exp = (ast_expression*)(a);  \
61     assert(ast_block_exprs_add(curblock, exp)); \
62 } while(0)
63
64 #define ASSIGN(op, a, b) \
65 (ast_expression*)ast_store_new(ctx, INSTR_##op, (ast_expression*)(a), (ast_expression*)(b))
66
67 #define BIN(op, a, b) \
68 (ast_expression*)ast_binary_new(ctx, INSTR_##op, (ast_expression*)(a), (ast_expression*)(b))
69
70 #define ENTFIELD(a, b) \
71 (ast_expression*)ast_entfield_new(ctx, (ast_expression*)(a), (ast_expression*)(b))
72
73 #define VECMEM(vec, mem) \
74 (ast_expression*)ast_member_new(ctx, (ast_expression*)(vec), (mem))
75
76 #define CALL(what)                                             \
77 do {                                                           \
78     ast_call *call = ast_call_new(ctx, (ast_expression*)what); \
79
80 #define CALLPARAM(x)                                       \
81     assert(ast_call_params_add(call, (ast_expression*)x));
82
83 #define ENDCALL()                                \
84     STATE(call);                                 \
85 } while(0)
86
87 #define ENDCALLWITH(as, where)                      \
88     {                                               \
89         ast_expression *as = (ast_expression*)call; \
90         where;                                      \
91     }                                               \
92 } while(0)
93
94 #define WHILE(cond)                                    \
95 do {                                                   \
96     ast_expression *wh_cond = (ast_expression*)(cond); \
97     ast_block *wh_body = ast_block_new(ctx);           \
98     ast_block *oldcur = curblock;                      \
99     ast_loop  *loop;                                   \
100     curblock = wh_body;
101
102 #define ENDWHILE()                                             \
103     curblock = oldcur;                                         \
104     loop = ast_loop_new(ctx, NULL, (ast_expression*)wh_cond,   \
105                         NULL, NULL, (ast_expression*)wh_body); \
106     assert(loop);                                              \
107     STATE(loop);                                               \
108 } while(0)
109
110 #define BUILTIN(name, outtype, number)                              \
111 do {                                                                \
112     ast_function *func_##name;                                      \
113     ast_value    *thisfuncval;                                      \
114     ast_function *thisfunc;                                         \
115     DEFVAR(return_##name);                                          \
116     VARnamed(TYPE_FUNCTION, name, name);                            \
117     VARnamed(outtype, return_##name, "#returntype");                \
118     name->expression.next = (ast_expression*)return_##name;         \
119     MKGLOBAL(name);                                                 \
120     func_##name = ast_function_new(ctx, #name, name);               \
121     thisfunc = func_##name;                                         \
122     (void)thisfunc;                                                 \
123     thisfuncval = name;                                             \
124     (void)thisfuncval;                                              \
125     assert(functions_add(func_##name) >= 0);                        \
126     func_##name->builtin = number;
127
128 #define ENDBUILTIN() } while(0)
129
130 #define PARAM(ptype, name)                           \
131 do {                                                 \
132     DEFVAR(parm);                                    \
133     VARnamed(ptype, parm, name);                     \
134     assert(ast_value_params_add(thisfuncval, parm)); \
135 } while(0)
136
137 #define FUNCTION(name, outtype)                                   \
138 do {                                                              \
139     ast_function *thisfunc;                                       \
140     ast_function *func_##name;                                    \
141     ast_block    *my_funcblock;                                   \
142     DEFVAR(var_##name);                                           \
143     DEFVAR(return_##name);                                        \
144     VARnamed(TYPE_FUNCTION, var_##name, name);                    \
145     VARnamed(outtype, return_##name, "#returntype");              \
146     var_##name->expression.next = (ast_expression*)return_##name; \
147     MKGLOBAL(var_##name);                                         \
148     func_##name = ast_function_new(ctx, #name, var_##name);       \
149     thisfunc = func_##name;                                       \
150     (void)thisfunc;                                               \
151     assert(functions_add(func_##name) >= 0);                      \
152     my_funcblock = ast_block_new(ctx);                            \
153     assert(my_funcblock);                                         \
154     assert(ast_function_blocks_add(func_##name, my_funcblock));   \
155     curblock = my_funcblock;
156
157 #define MKLOCAL(var) \
158     assert(ast_block_locals_add(curblock, var))
159
160 #define ENDFUNCTION(name) \
161 } while(0)
162
163 #endif