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