]> git.xonotic.org Git - xonotic/gmqcc.git/blob - parse.c
Count file lines
[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 /*
30  * These are not lexical tokens:  These are parse tree types.  Most people
31  * perform tokenizing on language punctuation which is wrong.  That stuff
32  * is technically already tokenized, it just needs to be parsed into a tree
33  */
34 #define PARSE_TYPE_DO       0
35 #define PARSE_TYPE_ELSE     1
36 #define PARSE_TYPE_IF       2
37 #define PARSE_TYPE_WHILE    3
38 #define PARSE_TYPE_BREAK    4
39 #define PARSE_TYPE_CONTINUE 5
40 #define PARSE_TYPE_RETURN   6
41 #define PARSE_TYPE_GOTO     7
42 #define PARSE_TYPE_FOR      8
43 #define PARSE_TYPE_VOID     9
44 #define PARSE_TYPE_STRING   10
45 #define PARSE_TYPE_FLOAT    11
46 #define PARSE_TYPE_VECTOR   12
47 #define PARSE_TYPE_ENTITY   13
48 #define PARSE_TYPE_LAND     14
49 #define PARSE_TYPE_LOR      15
50 #define PARSE_TYPE_LTEQ     16
51 #define PARSE_TYPE_GTEQ     17
52 #define PARSE_TYPE_EQEQ     18
53 #define PARSE_TYPE_LNEQ     19
54 #define PARSE_TYPE_COMMA    20
55 #define PARSE_TYPE_LNOT     21
56 #define PARSE_TYPE_STAR     22
57 #define PARSE_TYPE_DIVIDE   23
58 #define PARSE_TYPE_LPARTH   24
59 #define PARSE_TYPE_RPARTH   25
60 #define PARSE_TYPE_MINUS    26
61 #define PARSE_TYPE_ADD      27
62 #define PARSE_TYPE_EQUAL    28
63 #define PARSE_TYPE_LBS      29
64 #define PARSE_TYPE_RBS      30
65 #define PARSE_TYPE_ELIP     31
66 #define PARSE_TYPE_DOT      32
67 #define PARSE_TYPE_LT       33
68 #define PARSE_TYPE_GT       34
69 #define PARSE_TYPE_BAND     35
70 #define PARSE_TYPE_BOR      36
71 #define PARSE_TYPE_DONE     37
72 #define PARSE_TYPE_IDENT    38
73
74 /*
75  * Adds a parse type to the parse tree, this is where all the hard
76  * work actually begins.
77  */
78 #define PARSE_TREE_ADD(X)                                        \
79         do {                                                         \
80                 parsetree->next       = mem_a(sizeof(struct parsenode)); \
81                 parsetree->next->next = NULL;                            \
82                 parsetree->next->type = (X);                             \
83                 parsetree             = parsetree->next;                 \
84         } while (0)
85
86 /*
87  * This is all the punctuation handled in the parser, these don't
88  * need tokens, they're already tokens.
89  */
90 #if 0
91         "&&", "||", "<=", ">=", "==", "!=", ";", ",", "!", "*",
92         "/" , "(" , ")" , "-" , "+" , "=" , "[" , "]", "{", "}", "...",
93         "." , "<" , ">" , "&" , "|" , 
94 #endif
95
96 #define STORE(X) {  \
97         printf(X);      \
98         break;          \
99 }
100
101 void parse_debug(struct parsenode *tree) {
102         while (tree) {  
103                 switch (tree->type) {
104                         case PARSE_TYPE_ADD:       STORE("OPERATOR:  ADD    \n");
105                         case PARSE_TYPE_BAND:      STORE("OPERATOR:  BITAND \n");
106                         case PARSE_TYPE_BOR:       STORE("OPERATOR:  BITOR  \n");
107                         case PARSE_TYPE_COMMA:     STORE("OPERATOR:  SEPERATOR\n");
108                         case PARSE_TYPE_DOT:       STORE("OPERATOR:  DOT\n");
109                         case PARSE_TYPE_DIVIDE:    STORE("OPERATOR:  DIVIDE\n");
110                         case PARSE_TYPE_EQUAL:     STORE("OPERATOR:  ASSIGNMENT\n");
111                         
112                         case PARSE_TYPE_BREAK:     STORE("STATEMENT: BREAK  \n");
113                         case PARSE_TYPE_CONTINUE:  STORE("STATEMENT: CONTINUE\n");
114                         case PARSE_TYPE_GOTO:      STORE("STATEMENT: GOTO\n");
115                         case PARSE_TYPE_RETURN:    STORE("STATEMENT: RETURN\n");
116                         case PARSE_TYPE_DONE:      STORE("STATEMENT: DONE\n");
117
118                         case PARSE_TYPE_VOID:      STORE("DECLTYPE:  VOID\n");
119                         case PARSE_TYPE_STRING:    STORE("DECLTYPE:  STRING\n");
120                         case PARSE_TYPE_ELIP:      STORE("DECLTYPE:  VALIST\n");
121                         case PARSE_TYPE_ENTITY:    STORE("DECLTYPE:  ENTITY\n");
122                         case PARSE_TYPE_FLOAT:     STORE("DECLTYPE:  FLOAT\n");
123                         case PARSE_TYPE_VECTOR:    STORE("DECLTYPE:  VECTOR\n");
124                         
125                         case PARSE_TYPE_GT:        STORE("TEST:      GREATER THAN\n");
126                         case PARSE_TYPE_LT:        STORE("TEST:      LESS THAN\n");
127                         case PARSE_TYPE_GTEQ:      STORE("TEST:      GREATER THAN OR EQUAL\n");
128                         case PARSE_TYPE_LTEQ:      STORE("TEST:      LESS THAN OR EQUAL\n");
129                         case PARSE_TYPE_LNEQ:      STORE("TEST:      NOT EQUAL\n");
130                         case PARSE_TYPE_EQEQ:      STORE("TEST:      EQUAL-EQUAL\n");
131                         
132                         case PARSE_TYPE_LBS:       STORE("BLOCK:     BEG\n");
133                         case PARSE_TYPE_RBS:       STORE("BLOCK:     END\n");
134                         case PARSE_TYPE_ELSE:      STORE("BLOCK:     ELSE\n");
135                         case PARSE_TYPE_IF:        STORE("BLOCK:     IF\n");
136                         
137                         case PARSE_TYPE_LAND:      STORE("LOGICAL:   AND\n");
138                         case PARSE_TYPE_LNOT:      STORE("LOGICAL:   NOT\n");
139                         case PARSE_TYPE_LOR:       STORE("LOGICAL:   OR\n");
140                         
141                         case PARSE_TYPE_LPARTH:    STORE("PARTH:     BEG\n");
142                         case PARSE_TYPE_RPARTH:    STORE("PARTH:     END\n");
143                         
144                         case PARSE_TYPE_WHILE:     STORE("LOOP:      WHILE\n");
145                         case PARSE_TYPE_FOR:       STORE("LOOP:      FOR\n");
146                         case PARSE_TYPE_DO:        STORE("LOOP:      DO\n");
147                         
148                         //case PARSE_TYPE_IDENT:     STORE("IDENT:     ???\n");
149                 }
150                 tree = tree->next;
151         }
152 }
153
154 /*
155  * Performs a parse operation:  This is a macro to prevent bugs, if the
156  * calls to lex_token are'nt exactly enough to feed to the end of the
157  * actual lexees for the current thing that is being parsed, the state 
158  * of the next iteration in the creation of the parse tree will be wrong
159  * and everything will fail.
160  */
161 #define PARSE_PERFORM(X,C) {     \
162     token = lex_token(file);     \
163     { C }                        \
164     while (token != '\n') {      \
165             token = lex_token(file); \
166     }                            \
167     PARSE_TREE_ADD(X);           \
168     break;                       \
169 }
170
171 void parse_clear(struct parsenode *tree) {
172         if (!tree) return;
173         struct parsenode *temp = NULL;
174         while (tree != NULL) {
175                 temp = tree;
176                 tree = tree->next;
177                 mem_d (temp);
178         }
179         
180         /* free any potential typedefs */
181         typedef_clear();
182 }
183
184 /*
185  * Generates a parse tree out of the lexees generated by the lexer.  This
186  * is where the tree is built.  This is where valid check is performed.
187  */
188 int parse_tree(struct lex_file *file) {
189         struct parsenode *parsetree = NULL;
190         struct parsenode *parseroot = NULL;
191         
192         /*
193          * Allocate memory for our parse tree:
194          * the parse tree is just a singly linked list which will contain
195          * all the data for code generation.
196          */
197         if (!parseroot) {
198                 parseroot = mem_a(sizeof(struct parsenode));
199                 if (!parseroot)
200                         return error(ERROR_INTERNAL, "Ran out of memory", " ");
201                 parsetree       = parseroot;
202                 parsetree->type = -1; /* not a valid type -- root element */
203         }
204         
205         int     token = 0;
206         while ((token = lex_token(file)) != ERROR_LEX      && \
207                     token                    != ERROR_COMPILER && \
208                     token                    != ERROR_INTERNAL && \
209                     token                    != ERROR_PARSE    && \
210                     token                    != ERROR_PREPRO   && file->length >= 0) {
211                 switch (token) {
212                         case TOKEN_IF:
213                                 token = lex_token(file);
214                                 if (token != '(')
215                                         error(ERROR_PARSE, "Expected `(` on if statement:\n");
216                                 PARSE_TREE_ADD(PARSE_TYPE_IF);
217                                 PARSE_TREE_ADD(PARSE_TYPE_LPARTH);
218                                 break;
219                         case TOKEN_ELSE:
220                                 token = lex_token(file);
221                                 PARSE_TREE_ADD(PARSE_TYPE_ELSE);
222                                 break;
223                         case TOKEN_FOR:
224                                 //token = lex_token(file);
225                                 while ((token == ' ' || token == '\n') && file->length >= 0)
226                                         token = lex_token(file);
227                                 PARSE_TREE_ADD(PARSE_TYPE_FOR);
228                                 break;
229                         
230                         /*
231                          * This is a quick and easy way to do typedefs at parse time
232                          * all power is in typedef_add(), in typedef.c.  We handle 
233                          * the tokens accordingly here.
234                          */
235                         case TOKEN_TYPEDEF: {
236                                 char *f,*t;
237                                 
238                                 token = lex_token(file); 
239                                 token = lex_token(file); f = util_strdup(file->lastok);
240                                 token = lex_token(file); 
241                                 token = lex_token(file); t = util_strdup(file->lastok);
242                                 
243                                 typedef_add(f, t);
244                                 
245                                 mem_d(f);
246                                 mem_d(t);
247                                 
248                                 while (token != '\n')
249                                         token = lex_token(file);
250                                 break;
251                         }
252                         
253                         /*
254                          * Returns are addable as-is, statement checking is during
255                          * the actual parse tree check.
256                          */
257                         case TOKEN_RETURN:
258                                 token = lex_token(file);
259                                 PARSE_TREE_ADD(PARSE_TYPE_RETURN);
260                                 break;
261                         case TOKEN_CONTINUE:
262                                 PARSE_TREE_ADD(PARSE_TYPE_CONTINUE);
263                                 break;
264                         
265                         case TOKEN_DO:        PARSE_PERFORM(PARSE_TYPE_DO,      {});
266                         case TOKEN_WHILE:     PARSE_PERFORM(PARSE_TYPE_WHILE,   {});
267                         case TOKEN_BREAK:     PARSE_PERFORM(PARSE_TYPE_BREAK,   {});
268                         case TOKEN_GOTO:      PARSE_PERFORM(PARSE_TYPE_GOTO,    {});
269                         case TOKEN_VOID:      PARSE_PERFORM(PARSE_TYPE_VOID,    {});
270                         case TOKEN_STRING:    PARSE_PERFORM(PARSE_TYPE_STRING,  {});
271                         case TOKEN_FLOAT:     PARSE_PERFORM(PARSE_TYPE_FLOAT,   {});
272                         case TOKEN_VECTOR:    PARSE_PERFORM(PARSE_TYPE_VECTOR,  {});
273                         case TOKEN_ENTITY:    PARSE_PERFORM(PARSE_TYPE_ENTITY,  {});
274                                 
275                         /*
276                          * From here down is all language punctuation:  There is no
277                          * need to actual create tokens from these because they're already
278                          * tokenized as these individual tokens (which are in a special area
279                          * of the ascii table which doesn't conflict with our other tokens
280                          * which are higer than the ascii table.)
281                          */
282                         case '#':
283                                 token = lex_token(file); /* skip '#' */
284                                 while (isspace(token)) {
285                                         if (token == '\n')
286                                                 return error(ERROR_PARSE, "Expected valid preprocessor directive after `#` %s\n");
287                                         token = lex_token(file); /* try again */
288                                 }
289                                 /*
290                                  * If we make it here we found a directive, the supported
291                                  * directives so far are #include.
292                                  */
293                                 if (strncmp(file->lastok, "include", sizeof("include")) == 0) {
294                                         /*
295                                          * We only suport include " ", not <> like in C (why?)
296                                          * because the latter is silly.
297                                          */
298                                         while (*file->lastok != '"' && token != '\n')
299                                                 token = lex_token(file);
300                                         
301                                         /* we handle lexing at that point now */
302                                         if (token == '\n')
303                                                 return error(ERROR_PARSE, "%d: Invalid use of include preprocessor directive: wanted #include \"file.h\"\n", file->line);
304                                 }
305                         
306                                 /* skip all tokens to end of directive */
307                                 while (token != '\n')
308                                         token = lex_token(file);
309                                 break;
310                                 
311                         case '.':
312                                 PARSE_TREE_ADD(PARSE_TYPE_DOT);
313                                 break;
314                         case '(':
315                                 PARSE_TREE_ADD(PARSE_TYPE_LPARTH);
316                                 break;
317                         case ')':
318                                 PARSE_TREE_ADD(PARSE_TYPE_RPARTH);
319                                 break;
320                                 
321                         case '&':                               /* &  */
322                                 token = lex_token(file);
323                                 if (token == '&') { /* && */
324                                         token = lex_token(file);
325                                         PARSE_TREE_ADD(PARSE_TYPE_LAND);
326                                         break;
327                                 }
328                                 PARSE_TREE_ADD(PARSE_TYPE_BAND);
329                                 break;
330                         case '|':                               /* |  */
331                                 token = lex_token(file);
332                                 if (token == '|') { /* || */
333                                         token = lex_token(file);
334                                         PARSE_TREE_ADD(PARSE_TYPE_LOR);
335                                         break;
336                                 }
337                                 PARSE_TREE_ADD(PARSE_TYPE_BOR);
338                                 break;
339                         case '!':                               /* !  */
340                                 token = lex_token(file);
341                                 if (token == '=') { /* != */
342                                         token = lex_token(file);
343                                         PARSE_TREE_ADD(PARSE_TYPE_LNEQ);
344                                         break;
345                                 }
346                                 PARSE_TREE_ADD(PARSE_TYPE_LNOT);
347                                 break;
348                         case '<':                               /* <  */
349                                 token = lex_token(file);
350                                 if (token == '=') { /* <= */
351                                         token = lex_token(file);
352                                         PARSE_TREE_ADD(PARSE_TYPE_LTEQ);
353                                         break;
354                                 }
355                                 PARSE_TREE_ADD(PARSE_TYPE_LT);
356                                 break;
357                         case '>':                               /* >  */
358                                 token = lex_token(file);
359                                 if (token == '=') { /* >= */
360                                         token = lex_token(file);
361                                         PARSE_TREE_ADD(PARSE_TYPE_GTEQ);
362                                         break;
363                                 }
364                                 PARSE_TREE_ADD(PARSE_TYPE_GT);
365                                 break;
366                         case '=':                               /* =  */
367                                 token = lex_token(file);
368                                 if (token == '=') { /* == */
369                                         token = lex_token(file);
370                                         PARSE_TREE_ADD(PARSE_TYPE_EQEQ);
371                                         break;
372                                 }
373                                 PARSE_TREE_ADD(PARSE_TYPE_EQUAL);
374                                 break;
375                         case ';':
376                                 token = lex_token(file);
377                                 PARSE_TREE_ADD(PARSE_TYPE_DONE);
378                                 break;
379                         case '-':
380                                 token = lex_token(file);
381                                 PARSE_TREE_ADD(PARSE_TYPE_MINUS);
382                                 break;
383                         case '+':
384                                 token = lex_token(file);
385                                 PARSE_TREE_ADD(PARSE_TYPE_ADD);
386                                 break;
387                         case '{':
388                                 token = lex_token(file);
389                                 PARSE_TREE_ADD(PARSE_TYPE_LBS);
390                                 break;
391                         case '}':
392                                 token = lex_token(file);
393                                 PARSE_TREE_ADD(PARSE_TYPE_RBS);
394                                 break;
395                                 
396                         /*
397                          * TODO: Fix lexer to spit out ( ) as tokens, it seems the
398                          * using '(' or ')' in parser doesn't work properly unless
399                          * there are spaces before them to allow the lexer to properly
400                          * seperate identifiers. -- otherwise it eats all of it.
401                          */
402                         case LEX_IDENT:
403                                 token = lex_token(file);
404                                 PARSE_TREE_ADD(PARSE_TYPE_IDENT);
405                                 break;
406                 }
407         }
408         parse_debug(parseroot);
409         lex_reset(file);
410         parse_clear(parseroot);
411         return 1;
412 }