]> git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-macros.h
16130efcc2df6a98b2740f2b2cf758e722c9954c
[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 CALL(what)                                             \
74 do {                                                           \
75     ast_call *call = ast_call_new(ctx, (ast_expression*)what); \
76
77 #define CALLPARAM(x)                                       \
78     assert(ast_call_params_add(call, (ast_expression*)x));
79
80 #define ENDCALL()                                \
81     STATE(call);                                 \
82 } while(0)
83
84 #define ENDCALLWITH(as, where)                      \
85     {                                               \
86         ast_expression *as = (ast_expression*)call; \
87         where;                                      \
88     }                                               \
89 } while(0)
90
91 #define WHILE(cond)                                    \
92 do {                                                   \
93     ast_expression *wh_cond = (ast_expression*)(cond); \
94     ast_block *wh_body = ast_block_new(ctx);           \
95     ast_block *oldcur = curblock;                      \
96     ast_loop  *loop;                                   \
97     curblock = wh_body;
98
99 #define ENDWHILE()                                             \
100     curblock = oldcur;                                         \
101     loop = ast_loop_new(ctx, NULL, (ast_expression*)wh_cond,   \
102                         NULL, NULL, (ast_expression*)wh_body); \
103     assert(loop);                                              \
104     STATE(loop);                                               \
105 } while(0)
106
107 #define BUILTIN(name, outtype, number)                              \
108 do {                                                                \
109     ast_function *func_##name;                                      \
110     ast_value    *thisfuncval;                                      \
111     ast_function *thisfunc;                                         \
112     DEFVAR(return_##name);                                          \
113     VARnamed(TYPE_FUNCTION, name, name);                            \
114     VARnamed(outtype, return_##name, "#returntype");                \
115     name->expression.next = (ast_expression*)return_##name;         \
116     MKGLOBAL(name);                                                 \
117     func_##name = ast_function_new(ctx, #name, name);               \
118     thisfunc = func_##name;                                         \
119     (void)thisfunc;                                                 \
120     thisfuncval = name;                                             \
121     (void)thisfuncval;                                              \
122     assert(functions_add(func_##name) >= 0);                        \
123     func_##name->builtin = number;
124
125 #define ENDBUILTIN() } while(0)
126
127 #define PARAM(ptype, name)                           \
128 do {                                                 \
129     DEFVAR(parm);                                    \
130     VARnamed(ptype, parm, name);                     \
131     assert(ast_value_params_add(thisfuncval, parm)); \
132 } while(0)
133
134 #define FUNCTION(name, outtype)                                   \
135 do {                                                              \
136     ast_function *thisfunc;                                       \
137     ast_function *func_##name;                                    \
138     ast_block    *my_funcblock;                                   \
139     DEFVAR(var_##name);                                           \
140     DEFVAR(return_##name);                                        \
141     VARnamed(TYPE_FUNCTION, var_##name, name);                    \
142     VARnamed(outtype, return_##name, "#returntype");              \
143     var_##name->expression.next = (ast_expression*)return_##name; \
144     MKGLOBAL(var_##name);                                         \
145     func_##name = ast_function_new(ctx, #name, var_##name);       \
146     thisfunc = func_##name;                                       \
147     (void)thisfunc;                                               \
148     assert(functions_add(func_##name) >= 0);                      \
149     my_funcblock = ast_block_new(ctx);                            \
150     assert(my_funcblock);                                         \
151     assert(ast_function_blocks_add(func_##name, my_funcblock));   \
152     curblock = my_funcblock;
153
154 #define MKLOCAL(var) \
155     assert(ast_block_locals_add(curblock, var))
156
157 #define ENDFUNCTION(name) \
158 } while(0)
159
160 #endif