]> git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-macros.h
8c6d6c62c3243c1c5408811941f9782dde4dcc25
[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, (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 WHILE(cond)                                    \
51 do {                                                   \
52     ast_expression *wh_cond = (ast_expression*)(cond); \
53     ast_block *wh_body = ast_block_new(ctx);           \
54     ast_block *oldcur = curblock;                      \
55     ast_loop  *loop;                                   \
56     curblock = wh_body;
57
58 #define ENDWHILE()                                             \
59     curblock = oldcur;                                         \
60     loop = ast_loop_new(ctx, NULL, (ast_expression*)wh_cond,   \
61                         NULL, NULL, (ast_expression*)wh_body); \
62     assert(loop);                                              \
63     STATE(loop);                                               \
64 } while(0)
65
66 #define BUILTIN(name, outtype, number)                              \
67 do {                                                                \
68     ast_function *func_##name;                                      \
69     ast_function *thisfunc;                                         \
70     DEFVAR(return_##name);                                          \
71     VARnamed(TYPE_FUNCTION, name, name);                            \
72     VARnamed(outtype, return_##name, "#returntype");                \
73     name->expression.next = (ast_expression*)return_##name;         \
74     MKGLOBAL(name);                                                 \
75     func_##name = ast_function_new(ctx, #name, name);               \
76     thisfunc = func_##name;                                         \
77     (void)thisfunc;                                                 \
78     assert(functions_add(func_##name) >= 0);                        \
79     func_##name->builtin = number;
80
81 #define ENDBUILTIN() } while(0)
82
83 #define PARAM(ptype, name)                           \
84 do {                                                 \
85     DEFVAR(parm);                                    \
86     VARnamed(ptype, parm, name);                     \
87     assert(ast_function_params_add(thisfunc, parm)); \
88 } while(0)
89
90 #define FUNCTION(name, outtype)                                   \
91 do {                                                              \
92     ast_function *thisfunc;                                       \
93     ast_function *func_##name;                                    \
94     ast_block    *my_funcblock;                                   \
95     DEFVAR(var_##name);                                           \
96     DEFVAR(return_##name);                                        \
97     VARnamed(TYPE_FUNCTION, var_##name, name);                    \
98     VARnamed(outtype, return_##name, "#returntype");              \
99     var_##name->expression.next = (ast_expression*)return_##name; \
100     MKGLOBAL(var_##name);                                         \
101     func_##name = ast_function_new(ctx, #name, var_##name);       \
102     thisfunc = func_##name;                                       \
103     (void)thisfunc;                                               \
104     assert(functions_add(func_##name) >= 0);                      \
105     my_funcblock = ast_block_new(ctx);                            \
106     assert(my_funcblock);                                         \
107     assert(ast_function_blocks_add(func_##name, my_funcblock));   \
108     curblock = my_funcblock;
109
110 #define MKLOCAL(var) \
111     assert(ast_block_locals_add(curblock, var))
112
113 #define ENDFUNCTION(name) \
114 } while(0)
115
116 #endif