]> git.xonotic.org Git - xonotic/gmqcc.git/blob - astir.h
16c14c95f6ff3cbc346ae3defbaead9cf2f23305
[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 GMQCC_ASTIR_HDR
24 #define GMQCC_ASTIR_HDR
25
26 #define MEM_VECTOR_PROTO(Towner, Tmem, mem)                   \
27     bool GMQCC_WARN Towner##_##mem##_add(Towner*, Tmem);      \
28     bool GMQCC_WARN 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 GMQCC_WARN 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 bool GMQCC_WARN Tself##_##mem##_add(Tself *self, Twhat f)            \
42 {                                                                    \
43     Twhat *reall;                                                    \
44     if (self->mem##_count == self->mem##_alloc) {                    \
45         if (!self->mem##_alloc) {                                    \
46             self->mem##_alloc = 16;                                  \
47         } else {                                                     \
48             self->mem##_alloc *= 2;                                  \
49         }                                                            \
50         reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_alloc);    \
51         if (!reall) {                                                \
52             MEM_VECTOR_CLEAR(self, mem);                             \
53             return false;                                            \
54         }                                                            \
55         memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
56         mem_d(self->mem);                                            \
57         self->mem = reall;                                           \
58     }                                                                \
59     self->mem[self->mem##_count++] = f;                              \
60     return true;                                                     \
61 }
62
63 #define _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem)                       \
64 bool GMQCC_WARN Tself##_##mem##_remove(Tself *self, size_t idx)      \
65 {                                                                    \
66     size_t i;                                                        \
67     Twhat *reall;                                                    \
68     if (idx >= self->mem##_count) {                                  \
69         return true; /* huh... */                                    \
70     }                                                                \
71     for (i = idx; i < self->mem##_count-1; ++i) {                    \
72         self->mem[i] = self->mem[i+1];                               \
73     }                                                                \
74     self->mem##_count--;                                             \
75     if (self->mem##_count < self->mem##_count/2)                     \
76     {                                                                \
77         self->mem##_alloc /= 2;                                      \
78         reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_count);    \
79         if (!reall) {                                                \
80             return false;                                            \
81         }                                                            \
82         memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
83         mem_d(self->mem);                                            \
84         self->mem = reall;                                           \
85     }                                                                \
86     return true;                                                     \
87 }
88
89 #define _MEM_VEC_FUN_FIND(Tself, Twhat, mem)                    \
90 bool GMQCC_WARN Tself##_##mem##_find(Tself *self, Twhat obj, size_t *idx) \
91 {                                                               \
92     size_t i;                                                   \
93     for (i = 0; i < self->mem##_count; ++i) {                   \
94         if (self->mem[i] == obj) {                              \
95             if (idx) {                                          \
96                 *idx = i;                                       \
97             }                                                   \
98             return true;                                        \
99         }                                                       \
100     }                                                           \
101     return false;                                               \
102 }
103
104 #define _MEM_VEC_FUN_CLEAR(Tself, mem)  \
105 void Tself##_##mem##_clear(Tself *self) \
106 {                                       \
107     if (!self->mem)                     \
108         return;                         \
109     free((void*) self->mem);            \
110     self->mem = NULL;                   \
111     self->mem##_count = 0;              \
112     self->mem##_alloc = 0;              \
113 }
114
115 #define MEM_VECTOR_CLEAR(owner, mem) \
116     if ((owner)->mem)                \
117         free((void*)((owner)->mem)); \
118     (owner)->mem = NULL;             \
119     (owner)->mem##_count = 0;        \
120     (owner)->mem##_alloc = 0
121
122 #define MEM_VECTOR_INIT(owner, mem) \
123 {                                   \
124     (owner)->mem = NULL;            \
125     (owner)->mem##_count = 0;       \
126     (owner)->mem##_alloc = 0;       \
127 }
128
129 #define MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
130 _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem)       \
131 _MEM_VEC_FUN_ADD(Tself, Twhat, mem)
132
133 #define MEM_VEC_FUNCTIONS_ALL(Tself, Twhat, mem) \
134 MEM_VEC_FUNCTIONS(Tself, Twhat, mem)             \
135 _MEM_VEC_FUN_CLEAR(Tself, mem)                   \
136 _MEM_VEC_FUN_FIND(Tself, Twhat, mem)
137
138 enum qc_types {
139     /* Main QC types */
140     qc_void,
141     qc_float,
142     qc_vector,
143     qc_entity,
144     qc_string,
145
146     qc_int,
147
148     /* "virtual" and internal types */
149     qc_pointer,
150     qc_variant, /* eg. OFS_RETURN/PARAM... */
151     qc_function,
152 };
153
154 enum store_types {
155     store_global,
156     store_local,  /* local, assignable for now, should get promoted later */
157     store_value,  /* unassignable */
158 };
159
160 typedef struct {
161     float x, y, z;
162 } vector_t;
163
164 /* A shallow copy of a lex_file to remember where which ast node
165  * came from.
166  */
167 typedef struct lex_ctx
168 {
169     const char *file;
170     size_t     line;
171 } lex_ctx_t;
172
173 #endif