]> git.xonotic.org Git - xonotic/gmqcc.git/blob - parse.c
939831949df4b6c98107f140ce7c2e747c0050e0
[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 "gmqcc.h"
25
26
27 static const char *const parse_punct[] = {
28         "&&", "||", "<=", ">=", "==", "!=", ";", ",", "!", "*",
29         "/" , "(" , "-" , "+" , "=" , "[" , "]", "{", "}", "...",
30         "." , "<" , ">" , "#" , "&" , "|" , "$", "@", ":", NULL
31         /* 
32          * $,@,: are extensions:
33          * $ is a shorter `self`, so instead of self.frags, $.frags
34          * @ is a constructor
35          * : is compiler builtin functions
36          */
37 };
38
39 int parse(struct lex_file *file) {
40         int     token = 0;
41         while ((token = lex_token(file)) != ERROR_LEX      && \
42                     token                    != ERROR_COMPILER && \
43                     token                    != ERROR_INTERNAL && \
44                     token                    != ERROR_PARSE    && \
45                     token                    != ERROR_PREPRO   && file->length >= 0) {
46                 switch (token) {
47                         case TOKEN_IF:
48                                 token = lex_token(file);
49                                 while ((token == ' ' || token == '\n') && file->length >= 0)
50                                         token = lex_token(file);
51                                         
52                                 if (token != '(')
53                                         error(ERROR_PARSE, "Expected `(` after if\n", "");
54                                 break;
55                         
56                         /* TODO: Preprocessor */
57                         case '#':
58                                 token = lex_token(file);
59                                 token = lex_token(file);
60                                 token = lex_token(file);
61                                 token = lex_token(file);
62                                 token = lex_token(file);
63                                 token = lex_token(file);
64                                 break;
65                                 
66                         /* PUNCTUATION PARSING BEGINS */
67                         case '&':               /* &  */
68                                 token = lex_token(file);
69                                 if (token == '&') { /* && */
70                                         token = lex_token(file);
71                                         printf("--> LOGICAL AND\n");
72                                         goto end;
73                                 }
74                                 printf("--> BITWISE AND\n");
75                                 break;
76                         case '|':               /* |  */
77                                 token = lex_token(file);
78                                 if (token == '|') { /* || */
79                                         token = lex_token(file);
80                                         printf("--> LOGICAL OR\n");
81                                         goto end;
82                                 }
83                                 printf("--> BITWISE OR\n");
84                                 break;
85                         case '!':
86                                 token = lex_token(file);
87                                 if (token == '=') { /* != */
88                                         token = lex_token(file);
89                                         printf("--> LOGICAL NOT EQUAL\n");
90                                         goto end;
91                                 }
92                                 printf("--> LOGICAL NOT\n");
93                                 break;
94                         case '<':               /* <  */
95                                 token = lex_token(file);
96                                 if (token == '=') { /* <= */
97                                         token = lex_token(file);
98                                         printf("--> LESS THAN OR EQUALL\n");
99                                         goto end;
100                                 }
101                                 printf("--> LESS THAN\n");
102                                 break;
103                         case '>':               /* >  */
104                                 token = lex_token(file);
105                                 if (token == '=') { /* >= */
106                                         token = lex_token(file);
107                                         printf("--> GREATER THAN OR EQUAL\n");
108                                         goto end;
109                                 }
110                                 printf("--> GREATER THAN\n");
111                                 break;
112                         case '=':
113                                 token = lex_token(file);
114                                 if (token == '=') { /* == */
115                                         token = lex_token(file);
116                                         printf("--> COMPARISION \n");
117                                         goto end;
118                                 }
119                                 printf("--> ASSIGNMENT\n");
120                                 break;
121                         case ';':
122                                 token = lex_token(file);
123                                 printf("--> FINISHED STATMENT\n");
124                                 break;
125                         case '-':
126                                 token = lex_token(file);
127                                 printf("--> SUBTRACTION EXPRESSION\n");
128                                 break;
129                         case '+':
130                                 token = lex_token(file);
131                                 printf("--> ASSIGNMENT EXPRRESSION\n");
132                                 break;
133                 }
134                 end:;
135         }
136         lex_reset(file);
137         
138         //      "&&", "||", "<=", ">=", "==", "!=", ";", ",", "!", "*",
139         //"/" , "(" , "-" , "+" , "=" , "[" , "]", "{", "}", "...",
140         //"." , "<" , ">" , "#" , "&" , "|" , "$", "@", ":", NULL
141         
142         return 1;
143 }