]> git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-macros.h
function in the ast now MUST have an output type in their 'next' ast_expression point...
[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 STATE(a)                                 \
32 do {                                             \
33     ast_expression *exp = (ast_expression*)(a);  \
34     assert(ast_block_exprs_add(curblock, exp)); \
35 } while(0)
36
37 #define ASSIGN(op, a, b) \
38 (ast_expression*)ast_store_new(ctx, INSTR_##op, (a), (ast_expression*)(b))
39
40 #define BIN(op, a, b) \
41 (ast_expression*)ast_binary_new(ctx, INSTR_##op, (ast_expression*)(a), (ast_expression*)(b))
42
43 #define WHILE(cond)                                    \
44 do {                                                   \
45     ast_expression *wh_cond = (ast_expression*)(cond); \
46     ast_block *wh_body = ast_block_new(ctx);           \
47     ast_block *oldcur = curblock;                      \
48     ast_loop  *loop;                                   \
49     curblock = wh_body;
50
51 #define ENDWHILE()                                             \
52     curblock = oldcur;                                         \
53     loop = ast_loop_new(ctx, NULL, (ast_expression*)wh_cond,   \
54                         NULL, NULL, (ast_expression*)wh_body); \
55     assert(loop);                                              \
56     STATE(loop);                                               \
57 } while(0)
58
59 #define FUNCTION(name, outtype)                                   \
60 do {                                                              \
61     ast_function *func_##name;                                    \
62     ast_block    *my_funcblock;                                   \
63     DEFVAR(var_##name);                                           \
64     DEFVAR(return_##name);                                        \
65     VARnamed(TYPE_FUNCTION, var_##name, name);                    \
66     VARnamed(outtype, return_##name, "#returntype");              \
67     var_##name->expression.next = (ast_expression*)return_##name; \
68     MKGLOBAL(var_##name);                                         \
69     func_##name = ast_function_new(ctx, #name, var_##name);       \
70     assert(functions_add(func_##name) >= 0);                      \
71     my_funcblock = ast_block_new(ctx);                            \
72     assert(my_funcblock);                                         \
73     assert(ast_function_blocks_add(func_##name, my_funcblock));   \
74     curblock = my_funcblock;
75
76 #define MKLOCAL(var) \
77     assert(ast_block_locals_add(curblock, var))
78
79 #define ENDFUNCTION(name) \
80 } while(0)
81
82 #endif