]> git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-macros.h
creating and generating builtin functions, ast-macros for builtins, todo: params
[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 BUILTIN(name, outtype, number)                              \
60 do {                                                                \
61     ast_function *func_##name;                                      \
62     ast_function *thisfunc;                                         \
63     DEFVAR(return_##name);                                          \
64     VARnamed(TYPE_FUNCTION, name, name);                            \
65     VARnamed(outtype, return_##name, "#returntype");                \
66     name->expression.next = (ast_expression*)return_##name;         \
67     MKGLOBAL(name);                                                 \
68     func_##name = ast_function_new(ctx, #name, name);               \
69     thisfunc = func_##name;                                         \
70     (void)thisfunc;                                                 \
71     assert(functions_add(func_##name) >= 0);                        \
72     func_##name->builtin = number;
73
74 #define ENDBUILTIN() } while(0)
75
76 #define PARAM(ptype, name)                           \
77 do {                                                 \
78     DEFVAR(parm);                                    \
79     VARnamed(ptype, parm, name);                     \
80     assert(ast_function_params_add(thisfunc, parm)); \
81 } while(0)
82
83 #define FUNCTION(name, outtype)                                   \
84 do {                                                              \
85     ast_function *thisfunc;                                       \
86     ast_function *func_##name;                                    \
87     ast_block    *my_funcblock;                                   \
88     DEFVAR(var_##name);                                           \
89     DEFVAR(return_##name);                                        \
90     VARnamed(TYPE_FUNCTION, var_##name, name);                    \
91     VARnamed(outtype, return_##name, "#returntype");              \
92     var_##name->expression.next = (ast_expression*)return_##name; \
93     MKGLOBAL(var_##name);                                         \
94     func_##name = ast_function_new(ctx, #name, var_##name);       \
95     thisfunc = func_##name;                                       \
96     (void)thisfunc;                                               \
97     assert(functions_add(func_##name) >= 0);                      \
98     my_funcblock = ast_block_new(ctx);                            \
99     assert(my_funcblock);                                         \
100     assert(ast_function_blocks_add(func_##name, my_funcblock));   \
101     curblock = my_funcblock;
102
103 #define MKLOCAL(var) \
104     assert(ast_block_locals_add(curblock, var))
105
106 #define ENDFUNCTION(name) \
107 } while(0)
108
109 #endif