]> git.xonotic.org Git - xonotic/gmqcc.git/blob - astir.h
Introduce an ast_store rather than splitting ast_binary
[xonotic/gmqcc.git] / astir.h
1 /*
2  * Copyright (C) 2012 
3  *     Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #ifndef ASTIR_COMMON_H__
24 #define ASTIR_COMMON_H__
25
26 #define MEM_VECTOR_PROTO(Towner, Tmem, mem)        \
27     void Towner##_##mem##_add(Towner*, Tmem);      \
28     void Towner##_##mem##_remove(Towner*, size_t);
29
30 #define MEM_VECTOR_PROTO_ALL(Towner, Tmem, mem)          \
31     MEM_VECTOR_PROTO(Towner, Tmem, mem)          \
32     bool Towner##_##mem##_find(Towner*, Tmem, size_t*); \
33     void Towner##_##mem##_clear(Towner*);
34
35 #define MEM_VECTOR_MAKE(Twhat, name) \
36     Twhat  *name;                    \
37     size_t name##_count;             \
38     size_t name##_alloc
39
40 #define _MEM_VEC_FUN_ADD(Tself, Twhat, mem)       \
41 void Tself##_##mem##_add(Tself *self, Twhat f)    \
42 {                                                 \
43     if (self->mem##_count == self->mem##_alloc) { \
44         if (!self->mem##_alloc)                   \
45             self->mem##_alloc = 16;               \
46         else                                      \
47             self->mem##_alloc *= 2;               \
48         self->mem = (Twhat*)realloc(self->mem,    \
49             sizeof(Twhat) * self->mem##_alloc);   \
50     }                                             \
51     self->mem[self->mem##_count++] = f;           \
52 }
53
54 #define _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem)       \
55 void Tself##_##mem##_remove(Tself *self, size_t idx) \
56 {                                                    \
57     size_t i;                                        \
58     if (idx >= self->mem##_count)                    \
59         return;                                      \
60     for (i = idx; i < self->mem##_count-1; ++i)      \
61         self->mem[i] = self->mem[i+1];               \
62     self->mem##_count--;                             \
63     if (self->mem##_count < self->mem##_count/2)     \
64     {                                                \
65         self->mem##_alloc /= 2;                      \
66         self->mem = (Twhat*)realloc(self->mem,       \
67             self->mem##_alloc * sizeof(Twhat));      \
68     }                                                \
69 }
70
71 #define _MEM_VEC_FUN_FIND(Tself, Twhat, mem)                    \
72 bool Tself##_##mem##_find(Tself *self, Twhat obj, size_t *idx) \
73 {                                                               \
74     size_t i;                                                   \
75     for (i = 0; i < self->mem##_count; ++i) {                   \
76         if (self->mem[i] == obj) {                              \
77             if (idx)                                            \
78                 *idx = i;                                       \
79             return true;                                        \
80         }                                                       \
81     }                                                           \
82     return false;                                               \
83 }
84
85 #define _MEM_VEC_FUN_CLEAR(Tself, mem)  \
86 void Tself##_##mem##_clear(Tself *self) \
87 {                                       \
88     if (!self->mem)                     \
89         return;                         \
90     free((void*) self->mem);            \
91     self->mem = NULL;                   \
92     self->mem##_count = 0;              \
93     self->mem##_alloc = 0;              \
94 }
95
96 #define MEM_VECTOR_CLEAR(owner, mem) \
97     if ((owner)->mem)                \
98         free((void*)((owner)->mem)); \
99     (owner)->mem = NULL;             \
100     (owner)->mem##_count = 0;        \
101     (owner)->mem##_alloc = 0
102
103 #define MEM_VECTOR_INIT(owner, mem) \
104 {                                   \
105     (owner)->mem = NULL;            \
106     (owner)->mem##_count = 0;       \
107     (owner)->mem##_alloc = 0;       \
108 }
109
110 #define MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
111 _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem)       \
112 _MEM_VEC_FUN_ADD(Tself, Twhat, mem)
113
114 #define MEM_VEC_FUNCTIONS_ALL(Tself, Twhat, mem) \
115 MEM_VEC_FUNCTIONS(Tself, Twhat, mem)             \
116 _MEM_VEC_FUN_CLEAR(Tself, mem)                   \
117 _MEM_VEC_FUN_FIND(Tself, Twhat, mem)
118
119 enum qc_types {
120     /* Main QC types */
121     qc_void,
122     qc_float,
123     qc_vector,
124     qc_entity,
125     qc_string,
126
127     qc_int,
128
129     /* "virtual" and internal types */
130     qc_pointer,
131     qc_variant, /* eg. OFS_RETURN/PARAM... */
132     qc_function,
133 };
134
135 enum store_types {
136     store_global,
137     store_local,  /* local, assignable for now, should get promoted later */
138     store_value,  /* unassignable */
139 };
140
141 typedef struct {
142     float x, y, z;
143 } vector_t;
144
145 /* A shallow copy of a lex_file to remember where which ast node
146  * came from.
147  */
148 typedef struct lex_ctx
149 {
150     const char *file;
151     size_t     line;
152 } lex_ctx_t;
153
154 #endif