]> git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-macros.h
ast_store to take ast_expression on its left, rather than ast_value. Assigning to...
[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 MKCONSTFLOAT(name, value)  \
25 do {                               \
26     name->isconst = true;          \
27     name->constval.vfloat = value; \
28     MKGLOBAL(name);                \
29 } while(0)
30
31 #define MKCONSTSTRING(name, value)               \
32 do {                                             \
33     name->isconst = true;                        \
34     name->constval.vstring = util_strdup(value); \
35     MKGLOBAL(name);                              \
36 } while(0)
37
38 #define STATE(a)                                 \
39 do {                                             \
40     ast_expression *exp = (ast_expression*)(a);  \
41     assert(ast_block_exprs_add(curblock, exp)); \
42 } while(0)
43
44 #define ASSIGN(op, a, b) \
45 (ast_expression*)ast_store_new(ctx, INSTR_##op, (ast_expression*)(a), (ast_expression*)(b))
46
47 #define BIN(op, a, b) \
48 (ast_expression*)ast_binary_new(ctx, INSTR_##op, (ast_expression*)(a), (ast_expression*)(b))
49
50 #define CALL(what)                                             \
51 do {                                                           \
52     ast_call *call = ast_call_new(ctx, (ast_expression*)what); \
53
54 #define CALLPARAM(x)                                       \
55     assert(ast_call_params_add(call, (ast_expression*)x));
56
57 #define ENDCALL()                                \
58     STATE(call);                                 \
59 } while(0)
60
61 #define WHILE(cond)                                    \
62 do {                                                   \
63     ast_expression *wh_cond = (ast_expression*)(cond); \
64     ast_block *wh_body = ast_block_new(ctx);           \
65     ast_block *oldcur = curblock;                      \
66     ast_loop  *loop;                                   \
67     curblock = wh_body;
68
69 #define ENDWHILE()                                             \
70     curblock = oldcur;                                         \
71     loop = ast_loop_new(ctx, NULL, (ast_expression*)wh_cond,   \
72                         NULL, NULL, (ast_expression*)wh_body); \
73     assert(loop);                                              \
74     STATE(loop);                                               \
75 } while(0)
76
77 #define BUILTIN(name, outtype, number)                              \
78 do {                                                                \
79     ast_function *func_##name;                                      \
80     ast_value    *thisfuncval;                                      \
81     ast_function *thisfunc;                                         \
82     DEFVAR(return_##name);                                          \
83     VARnamed(TYPE_FUNCTION, name, name);                            \
84     VARnamed(outtype, return_##name, "#returntype");                \
85     name->expression.next = (ast_expression*)return_##name;         \
86     MKGLOBAL(name);                                                 \
87     func_##name = ast_function_new(ctx, #name, name);               \
88     thisfunc = func_##name;                                         \
89     (void)thisfunc;                                                 \
90     thisfuncval = name;                                             \
91     (void)thisfuncval;                                              \
92     assert(functions_add(func_##name) >= 0);                        \
93     func_##name->builtin = number;
94
95 #define ENDBUILTIN() } while(0)
96
97 #define PARAM(ptype, name)                           \
98 do {                                                 \
99     DEFVAR(parm);                                    \
100     VARnamed(ptype, parm, name);                     \
101     assert(ast_value_params_add(thisfuncval, parm)); \
102 } while(0)
103
104 #define FUNCTION(name, outtype)                                   \
105 do {                                                              \
106     ast_function *thisfunc;                                       \
107     ast_function *func_##name;                                    \
108     ast_block    *my_funcblock;                                   \
109     DEFVAR(var_##name);                                           \
110     DEFVAR(return_##name);                                        \
111     VARnamed(TYPE_FUNCTION, var_##name, name);                    \
112     VARnamed(outtype, return_##name, "#returntype");              \
113     var_##name->expression.next = (ast_expression*)return_##name; \
114     MKGLOBAL(var_##name);                                         \
115     func_##name = ast_function_new(ctx, #name, var_##name);       \
116     thisfunc = func_##name;                                       \
117     (void)thisfunc;                                               \
118     assert(functions_add(func_##name) >= 0);                      \
119     my_funcblock = ast_block_new(ctx);                            \
120     assert(my_funcblock);                                         \
121     assert(ast_function_blocks_add(func_##name, my_funcblock));   \
122     curblock = my_funcblock;
123
124 #define MKLOCAL(var) \
125     assert(ast_block_locals_add(curblock, var))
126
127 #define ENDFUNCTION(name) \
128 } while(0)
129
130 #endif