]> git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-macros.h
ast macros to use the return value of a call, trying to spawn an entity, setting...
[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 STATE(a)                                 \
50 do {                                             \
51     ast_expression *exp = (ast_expression*)(a);  \
52     assert(ast_block_exprs_add(curblock, exp)); \
53 } while(0)
54
55 #define ASSIGN(op, a, b) \
56 (ast_expression*)ast_store_new(ctx, INSTR_##op, (ast_expression*)(a), (ast_expression*)(b))
57
58 #define BIN(op, a, b) \
59 (ast_expression*)ast_binary_new(ctx, INSTR_##op, (ast_expression*)(a), (ast_expression*)(b))
60
61 #define ENTFIELD(a, b) \
62 (ast_expression*)ast_entfield_new(ctx, (ast_expression*)(a), (ast_expression*)(b))
63
64 #define CALL(what)                                             \
65 do {                                                           \
66     ast_call *call = ast_call_new(ctx, (ast_expression*)what); \
67
68 #define CALLPARAM(x)                                       \
69     assert(ast_call_params_add(call, (ast_expression*)x));
70
71 #define ENDCALL()                                \
72     STATE(call);                                 \
73 } while(0)
74
75 #define ENDCALLWITH(as, where)                      \
76     {                                               \
77         ast_expression *as = (ast_expression*)call; \
78         where;                                      \
79     }                                               \
80 } while(0)
81
82 #define WHILE(cond)                                    \
83 do {                                                   \
84     ast_expression *wh_cond = (ast_expression*)(cond); \
85     ast_block *wh_body = ast_block_new(ctx);           \
86     ast_block *oldcur = curblock;                      \
87     ast_loop  *loop;                                   \
88     curblock = wh_body;
89
90 #define ENDWHILE()                                             \
91     curblock = oldcur;                                         \
92     loop = ast_loop_new(ctx, NULL, (ast_expression*)wh_cond,   \
93                         NULL, NULL, (ast_expression*)wh_body); \
94     assert(loop);                                              \
95     STATE(loop);                                               \
96 } while(0)
97
98 #define BUILTIN(name, outtype, number)                              \
99 do {                                                                \
100     ast_function *func_##name;                                      \
101     ast_value    *thisfuncval;                                      \
102     ast_function *thisfunc;                                         \
103     DEFVAR(return_##name);                                          \
104     VARnamed(TYPE_FUNCTION, name, name);                            \
105     VARnamed(outtype, return_##name, "#returntype");                \
106     name->expression.next = (ast_expression*)return_##name;         \
107     MKGLOBAL(name);                                                 \
108     func_##name = ast_function_new(ctx, #name, name);               \
109     thisfunc = func_##name;                                         \
110     (void)thisfunc;                                                 \
111     thisfuncval = name;                                             \
112     (void)thisfuncval;                                              \
113     assert(functions_add(func_##name) >= 0);                        \
114     func_##name->builtin = number;
115
116 #define ENDBUILTIN() } while(0)
117
118 #define PARAM(ptype, name)                           \
119 do {                                                 \
120     DEFVAR(parm);                                    \
121     VARnamed(ptype, parm, name);                     \
122     assert(ast_value_params_add(thisfuncval, parm)); \
123 } while(0)
124
125 #define FUNCTION(name, outtype)                                   \
126 do {                                                              \
127     ast_function *thisfunc;                                       \
128     ast_function *func_##name;                                    \
129     ast_block    *my_funcblock;                                   \
130     DEFVAR(var_##name);                                           \
131     DEFVAR(return_##name);                                        \
132     VARnamed(TYPE_FUNCTION, var_##name, name);                    \
133     VARnamed(outtype, return_##name, "#returntype");              \
134     var_##name->expression.next = (ast_expression*)return_##name; \
135     MKGLOBAL(var_##name);                                         \
136     func_##name = ast_function_new(ctx, #name, var_##name);       \
137     thisfunc = func_##name;                                       \
138     (void)thisfunc;                                               \
139     assert(functions_add(func_##name) >= 0);                      \
140     my_funcblock = ast_block_new(ctx);                            \
141     assert(my_funcblock);                                         \
142     assert(ast_function_blocks_add(func_##name, my_funcblock));   \
143     curblock = my_funcblock;
144
145 #define MKLOCAL(var) \
146     assert(ast_block_locals_add(curblock, var))
147
148 #define ENDFUNCTION(name) \
149 } while(0)
150
151 #endif