]> git.xonotic.org Git - xonotic/gmqcc.git/blob - parse.c
a218e6337af340beb095a16df62ecc91d2b4a508
[xonotic/gmqcc.git] / parse.c
1 /*
2  * Copyright (C) 2012 
3  *      Dale Weiler
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 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include "gmqcc.h"
28
29 /* compile-time constant for type constants */
30 typedef struct {
31         char *name;
32         int   type;
33         float value[3];
34         char *string; /* string value if constant is string literal */
35 } constant;
36 VECTOR_MAKE(constant, compile_constants);
37
38 void compile_constant_debug() {
39         int iter = 0;
40         for(; iter < compile_constants_elements; iter++) {
41                 constant *c = &compile_constants_data[iter];
42                 switch(c->type) {
43                         case TYPE_FLOAT:  printf("constant: %s FLOAT   %f\n",       c->name, c->value[0]);                           break;
44                         case TYPE_VECTOR: printf("constant: %s VECTOR {%f,%f,%f}\n",c->name, c->value[0], c->value[1], c->value[2]); break;
45                         case TYPE_STRING: printf("constant: %s STRING  %s\n",       c->name, c->string); break;
46                         case TYPE_VOID:   printf("constant: %s VOID    %s\n",       c->name, c->string); break;
47                 }
48         }
49 }
50
51 /*
52  * Generates a parse tree out of the lexees generated by the lexer.  This
53  * is where the tree is built.  This is where valid check is performed.
54  */
55 int parse_gen(struct lex_file *file) {  
56         int     token = 0;
57         while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
58                 switch (token) {
59                         case TOKEN_TYPEDEF: {
60                                 char *f; /* from */
61                                 char *t; /* to   */
62                                 
63                                 token = lex_token(file); 
64                                 token = lex_token(file); f = util_strdup(file->lastok);
65                                 token = lex_token(file); 
66                                 token = lex_token(file); t = util_strdup(file->lastok);
67                                 
68                                 typedef_add(file, f, t);
69                                 mem_d(f);
70                                 mem_d(t);
71                                 
72                                 token = lex_token(file);
73                                 if (token == ' ')
74                                         token = lex_token(file);
75                                         
76                                 if (token != ';')
77                                         error(file, ERROR_PARSE, "Expected a `;` at end of typedef statement");
78                                         
79                                 token = lex_token(file);
80                                 break;
81                         }
82                         
83                         case TOKEN_VOID:   goto fall;
84                         case TOKEN_STRING: goto fall;
85                         case TOKEN_VECTOR: goto fall;
86                         case TOKEN_ENTITY: goto fall;
87                         case TOKEN_FLOAT:  goto fall;
88                         {
89                         fall:;
90                                 char *name = NULL;
91                                 int   type = token; /* story copy */
92                                 
93                                 /* skip over space */
94                                 token = lex_token(file);
95                                 if (token == ' ')
96                                         token = lex_token(file);
97                                 
98                                 /* save name */
99                                 name = util_strdup(file->lastok);
100                                 
101                                 /* skip spaces */
102                                 token = lex_token(file);
103                                 if (token == ' ')
104                                         token = lex_token(file);
105                                         
106                                 if (token == ';') {
107                                         /*
108                                          * Definitions go to the defs table, they don't have
109                                          * any sort of data with them yet.
110                                          */
111                                 } else if (token == '=') {
112                                         token = lex_token(file);
113                                         if (token == ' ')
114                                                 token = lex_token(file);
115                                         
116                                         /* strings are in file->lastok */
117                                         switch (type) {
118                                                 case TOKEN_VOID:
119                                                         error(file, ERROR_PARSE, "Cannot assign value to type void\n");
120                                                         
121                                                 /* TODO: Validate (end quote), strip quotes for constant add, name constant */
122                                                 case TOKEN_STRING:
123                                                         if (*file->lastok != '"')
124                                                                 error(file, ERROR_PARSE, "Expected a '\"' (quote) for string constant\n");
125                                                         /* add the compile-time constant */
126                                                         compile_constants_add((constant){
127                                                                 .name   = util_strdup(name),
128                                                                 .type   = TYPE_STRING,
129                                                                 .value  = {0,0,0},
130                                                                 .string = util_strdup(file->lastok)
131                                                         });
132                                                         break;
133                                                 /* TODO: name constant, old qc vec literals, whitespace fixes, name constant */
134                                                 case TOKEN_VECTOR: {
135                                                         float compile_calc_x = 0;
136                                                         float compile_calc_y = 0;
137                                                         float compile_calc_z = 0;
138                                                         int   compile_calc_d = 0; /* dot?        */
139                                                         int   compile_calc_s = 0; /* sign (-, +) */
140                                                         
141                                                         char  compile_data[1024];
142                                                         char *compile_eval = compile_data;
143                                                         
144                                                         if (token != '{')
145                                                                 error(file, ERROR_PARSE, "Expected initializer list {} for vector constant\n"); 
146                                                         
147                                                         /*
148                                                          * This parses a single vector element: x,y & z.  This will handle all the
149                                                          * complicated mechanics of a vector, and can be extended as well.  This
150                                                          * is a rather large macro, and is #undef'd after it's use below.
151                                                          */
152                                                         #define PARSE_VEC_ELEMENT(NAME, BIT)                                                                                                           \
153                                                             token = lex_token(file);                                                                                                                   \
154                                                             if (token == ' ')                                                                                                                          \
155                                                                 token = lex_token(file);                                                                                                               \
156                                                             if (token == '.')                                                                                                                          \
157                                                                 compile_calc_d = 1;                                                                                                                    \
158                                                             if (!isdigit(token) && !compile_calc_d && token != '+' && token != '-')                                                                    \
159                                                                 error(file, ERROR_PARSE,"Invalid constant initializer element %c for vector, must be numeric\n", NAME);                                \
160                                                             if (token == '+')                                                                                                                          \
161                                                                 compile_calc_s = '+';                                                                                                                  \
162                                                             if (token == '-' && !compile_calc_s)                                                                                                       \
163                                                                 compile_calc_s = '-';                                                                                                                  \
164                                                             while (isdigit(token) || token == '.' || token == '+' || token == '-') {                                                                   \
165                                                                 *compile_eval++ = token;                                                                                                               \
166                                                                 token           = lex_token(file);                                                                                                     \
167                                                                 if (token == '.' && compile_calc_d) {                                                                                                  \
168                                                                     error(file, ERROR_PARSE, "Invalid constant initializer element %c for vector, must be numeric.\n", NAME);                          \
169                                                                     token = lex_token(file);                                                                                                           \
170                                                                 }                                                                                                                                      \
171                                                                 if ((token == '-' || token == '+') && compile_calc_s) {                                                                                \
172                                                                     error(file, ERROR_PARSE, "Invalid constant initializer sign for vector element %c\n", NAME);                                       \
173                                                                     token = lex_token(file);                                                                                                           \
174                                                                 }                                                                                                                                      \
175                                                                 else if (token == '.' && !compile_calc_d)                                                                                              \
176                                                                     compile_calc_d = 1;                                                                                                                \
177                                                                 else if (token == '-' && !compile_calc_s)                                                                                              \
178                                                                     compile_calc_s = '-';                                                                                                              \
179                                                                 else if (token == '+' && !compile_calc_s)                                                                                              \
180                                                                     compile_calc_s = '+';                                                                                                              \
181                                                             }                                                                                                                                          \
182                                                             if (token == ' ')                                                                                                                          \
183                                                                 token = lex_token(file);                                                                                                               \
184                                                             if (NAME != 'z') {                                                                                                                         \
185                                                                 if (token != ',' && token != ' ')                                                                                                      \
186                                                                     error(file, ERROR_PARSE, "invalid constant initializer element %c for vector (missing spaces, or comma delimited list?)\n", NAME); \
187                                                             } else if (token != '}') {                                                                                                                 \
188                                                                 error(file, ERROR_PARSE, "Expected `}` on end of constant initialization for vector\n");                                               \
189                                                             }                                                                                                                                          \
190                                                             compile_calc_##BIT = atof(compile_data);                                                                                                   \
191                                                             compile_calc_d = 0;                                                                                                                        \
192                                                             compile_calc_s = 0;                                                                                                                        \
193                                                             compile_eval   = &compile_data[0];                                                                                                         \
194                                                             memset(compile_data, 0, sizeof(compile_data))
195                                                         
196                                                         /*
197                                                          * Parse all elements using the macro above.
198                                                          * We must undef the macro afterwards.
199                                                          */
200                                                         PARSE_VEC_ELEMENT('x', x);
201                                                         PARSE_VEC_ELEMENT('y', y);
202                                                         PARSE_VEC_ELEMENT('z', z);
203                                                         #undef PARSE_VEC_ELEMENT
204                                                         
205                                                         /* Check for the semi-colon... */
206                                                         token = lex_token(file);
207                                                         if (token == ' ')
208                                                                 token = lex_token(file);
209                                                         if (token != ';')
210                                                                 error(file, ERROR_PARSE, "Expected `;` on end of constant initialization for vector\n");
211                                                                 
212                                                         /* add the compile-time constant */
213                                                         compile_constants_add((constant){
214                                                                 .name   = util_strdup(name),
215                                                                 .type   = TYPE_VECTOR,
216                                                                 .value  = {
217                                                                         [0] = compile_calc_x,
218                                                                         [1] = compile_calc_y,
219                                                                         [2] = compile_calc_z
220                                                                 },
221                                                                 .string = NULL
222                                                         });
223                                                         break;
224                                                 }
225                                                         
226                                                 case TOKEN_ENTITY:
227                                                 case TOKEN_FLOAT: /*TODO: validate, constant generation, name constant */
228                                                         if (!isdigit(token))
229                                                                 error(file, ERROR_PARSE, "Expected numeric constant for float constant\n");
230                                                         compile_constants_add((constant){
231                                                                 .name   = util_strdup(name),
232                                                                 .type   = TOKEN_FLOAT,
233                                                                 .value  = {0,0,0},
234                                                                 .string = NULL
235                                                         });
236                                                         break;
237                                         }
238                                 } else if (token == '(') {
239                                         printf("FUNCTION ??\n");
240                                 }
241                                 mem_d(name);
242                         }
243                                 
244                         /*
245                          * From here down is all language punctuation:  There is no
246                          * need to actual create tokens from these because they're already
247                          * tokenized as these individual tokens (which are in a special area
248                          * of the ascii table which doesn't conflict with our other tokens
249                          * which are higer than the ascii table.)
250                          */
251                         case '#':
252                                 token = lex_token(file); /* skip '#' */
253                                 if (token == ' ')
254                                         token = lex_token(file);
255                                 /*
256                                  * If we make it here we found a directive, the supported
257                                  * directives so far are #include.
258                                  */
259                                 if (strncmp(file->lastok, "include", sizeof("include")) == 0) {
260                                         /*
261                                          * We only suport include " ", not <> like in C (why?)
262                                          * because the latter is silly.
263                                          */
264                                         while (*file->lastok != '"' && token != '\n')
265                                                 token = lex_token(file);
266                                         if (token == '\n')
267                                                 return error(file, ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
268                                                 
269                                         char            *copy = util_strdup(file->lastok);
270                                         struct lex_file *next = lex_include(file,   copy);
271                                         
272                                         if (!next) {
273                                                 error(file, ERROR_INTERNAL, "Include subsystem failure\n");
274                                                 exit (-1);
275                                         }
276                                         compile_constants_add((constant) {
277                                                         .name   = "#include",
278                                                         .type   = TYPE_VOID,
279                                                         .value  = {0,0,0},
280                                                         .string = copy
281                                         });
282                                         parse_gen(next);
283                                         mem_d    (copy);
284                                         lex_close(next);
285                                 }
286                                 /* skip all tokens to end of directive */
287                                 while (token != '\n')
288                                         token = lex_token(file);
289                                 break;
290                                 
291                         case LEX_IDENT:
292                                 token = lex_token(file);
293                                 break;
294                 }
295         }
296         compile_constant_debug();
297         lex_reset(file);
298         return 1;
299 }