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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
29 /* compile-time constant for type constants */
34 char *string; /* string value if constant is string literal */
36 VECTOR_MAKE(constant, compile_constants);
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.
42 int parse_gen(struct lex_file *file) {
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) {
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);
63 token = lex_token(file);
65 token = lex_token(file);
68 error(ERROR_PARSE, "%s:%d Expected a `;` at end of typedef statement\n", file->name, file->line);
70 token = lex_token(file);
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;
82 int type = token; /* story copy */
85 token = lex_token(file);
87 token = lex_token(file);
90 name = util_strdup(file->lastok);
93 token = lex_token(file);
95 token = lex_token(file);
99 * Definitions go to the defs table, they don't have
100 * any sort of data with them yet.
102 } else if (token == '=') {
103 token = lex_token(file);
105 token = lex_token(file);
107 /* strings are in file->lastok */
110 return error(ERROR_PARSE, "%s:%d Cannot assign value to type void\n", file->name, file->line);
112 /* TODO: Validate (end quote), strip quotes for constant add, name constant */
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),
121 .string = util_strdup(file->lastok)
124 /* TODO: name constant, old qc vec literals, whitespace fixes, name constant */
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 (-, +) */
132 char compile_data[1024];
133 char *compile_eval = compile_data;
136 error(ERROR_PARSE, "%s:%d Expected initializer list `{`,`}` for vector constant\n", file->name, file->line);
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.
143 #define PARSE_VEC_ELEMENT(NAME, BIT) \
144 token = lex_token(file); \
146 token = lex_token(file); \
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); \
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); \
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); \
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 = '+'; \
174 token = lex_token(file); \
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); \
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))
188 * Parse all elements using the macro above.
189 * We must undef the macro afterwards.
191 PARSE_VEC_ELEMENT('x', x);
192 PARSE_VEC_ELEMENT('y', y);
193 PARSE_VEC_ELEMENT('z', z);
194 #undef PARSE_VEC_ELEMENT
196 /* Check for the semi-colon... */
197 token = lex_token(file);
199 token = lex_token(file);
201 error(ERROR_PARSE, "%s:%d Expected `;` on end of constant initialization for vector\n", file->name, file->line);
203 /* add the compile-time constant */
204 compile_constants_add((constant){
205 .name = util_strdup(name),
208 [0] = compile_calc_x,
209 [1] = compile_calc_y,
218 case TOKEN_FLOAT: /*TODO: validate, constant generation, name constant */
220 error(ERROR_PARSE, "%s:%d Expected numeric constant for float constant\n");
221 compile_constants_add((constant){
222 .name = util_strdup(name),
229 } else if (token == '(') {
230 printf("FUNCTION ??\n");
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.)
243 token = lex_token(file); /* skip '#' */
245 token = lex_token(file);
247 * If we make it here we found a directive, the supported
248 * directives so far are #include.
250 if (strncmp(file->lastok, "include", sizeof("include")) == 0) {
252 * We only suport include " ", not <> like in C (why?)
253 * because the latter is silly.
255 while (*file->lastok != '"' && token != '\n')
256 token = lex_token(file);
258 return error(ERROR_PARSE, "%d: Invalid use of include preprocessor directive: wanted #include \"file.h\"\n", file->line-1);
260 char *copy = util_strdup(file->lastok);
261 struct lex_file *next = lex_include(file, copy);
264 error(ERROR_INTERNAL, "Include subsystem failure\n");
271 /* skip all tokens to end of directive */
272 while (token != '\n')
273 token = lex_token(file);
277 token = lex_token(file);