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