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
25 /* compile-time constant for type constants */
30 char *string; /* string value if constant is string literal */
32 VECTOR_MAKE(constant, compile_constants);
34 void compile_constant_debug() {
36 for(; iter < compile_constants_elements; iter++) {
37 constant *c = &compile_constants_data[iter];
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;
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.
51 int parse_gen(lex_file *file) {
53 while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
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);
64 typedef_add(file, f, t);
68 token = lex_token(file);
70 token = lex_token(file);
73 error(file, ERROR_PARSE, "Expected a `;` at end of typedef statement");
75 token = lex_token(file);
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;
87 int type = token; /* story copy */
90 token = lex_token(file);
92 token = lex_token(file);
95 name = util_strdup(file->lastok);
98 token = lex_token(file);
100 token = lex_token(file);
104 * Definitions go to the defs table, they don't have
105 * any sort of data with them yet.
107 } else if (token == '=') {
108 token = lex_token(file);
110 token = lex_token(file);
112 /* strings are in file->lastok */
115 error(file, ERROR_PARSE, "Cannot assign value to type void\n");
117 /* TODO: Validate (end quote), strip quotes for constant add, name constant */
119 if (*file->lastok != '"')
120 error(file, ERROR_PARSE, "Expected a '\"' (quote) for string constant\n");
121 /* add the compile-time constant */
124 c.name = util_strdup(name),
125 c.type = TYPE_STRING,
129 c.string = util_strdup(file->lastok);
130 compile_constants_add(c);
133 /* TODO: name constant, old qc vec literals, whitespace fixes, name constant */
135 float compile_calc_x = 0;
136 float compile_calc_y = 0;
137 float compile_calc_z = 0;
138 int compile_calc_d = 0; /* dot? */
139 int compile_calc_s = 0; /* sign (-, +) */
141 char compile_data[1024];
142 char *compile_eval = compile_data;
145 error(file, ERROR_PARSE, "Expected initializer list {} for vector constant\n");
148 * This parses a single vector element: x,y & z. This will handle all the
149 * complicated mechanics of a vector, and can be extended as well. This
150 * is a rather large macro, and is #undef'd after it's use below.
152 #define PARSE_VEC_ELEMENT(NAME, BIT) \
153 token = lex_token(file); \
155 token = lex_token(file); \
157 compile_calc_d = 1; \
158 if (!isdigit(token) && !compile_calc_d && token != '+' && token != '-') \
159 error(file, ERROR_PARSE,"Invalid constant initializer element %c for vector, must be numeric\n", NAME); \
161 compile_calc_s = '+'; \
162 if (token == '-' && !compile_calc_s) \
163 compile_calc_s = '-'; \
164 while (isdigit(token) || token == '.' || token == '+' || token == '-') { \
165 *compile_eval++ = token; \
166 token = lex_token(file); \
167 if (token == '.' && compile_calc_d) { \
168 error(file, ERROR_PARSE, "Invalid constant initializer element %c for vector, must be numeric.\n", NAME); \
169 token = lex_token(file); \
171 if ((token == '-' || token == '+') && compile_calc_s) { \
172 error(file, ERROR_PARSE, "Invalid constant initializer sign for vector element %c\n", NAME); \
173 token = lex_token(file); \
175 else if (token == '.' && !compile_calc_d) \
176 compile_calc_d = 1; \
177 else if (token == '-' && !compile_calc_s) \
178 compile_calc_s = '-'; \
179 else if (token == '+' && !compile_calc_s) \
180 compile_calc_s = '+'; \
183 token = lex_token(file); \
185 if (token != ',' && token != ' ') \
186 error(file, ERROR_PARSE, "invalid constant initializer element %c for vector (missing spaces, or comma delimited list?)\n", NAME); \
187 } else if (token != '}') { \
188 error(file, ERROR_PARSE, "Expected `}` on end of constant initialization for vector\n"); \
190 compile_calc_##BIT = atof(compile_data); \
191 compile_calc_d = 0; \
192 compile_calc_s = 0; \
193 compile_eval = &compile_data[0]; \
194 memset(compile_data, 0, sizeof(compile_data))
197 * Parse all elements using the macro above.
198 * We must undef the macro afterwards.
200 PARSE_VEC_ELEMENT('x', x);
201 PARSE_VEC_ELEMENT('y', y);
202 PARSE_VEC_ELEMENT('z', z);
203 #undef PARSE_VEC_ELEMENT
205 /* Check for the semi-colon... */
206 token = lex_token(file);
208 token = lex_token(file);
210 error(file, ERROR_PARSE, "Expected `;` on end of constant initialization for vector\n");
212 /* add the compile-time constant */
216 c.name = util_strdup(name),
217 c.type = TYPE_VECTOR,
218 c.value[0] = compile_calc_x;
219 c.value[1] = compile_calc_y;
220 c.value[2] = compile_calc_z;
222 compile_constants_add(c);
228 case TOKEN_FLOAT: /*TODO: validate, constant generation, name constant */
230 error(file, ERROR_PARSE, "Expected numeric constant for float constant\n");
234 c.name = util_strdup(name),
235 c.type = TOKEN_FLOAT,
240 compile_constants_add(c);
244 } else if (token == '(') {
245 printf("FUNCTION ??\n");
251 * From here down is all language punctuation: There is no
252 * need to actual create tokens from these because they're already
253 * tokenized as these individual tokens (which are in a special area
254 * of the ascii table which doesn't conflict with our other tokens
255 * which are higer than the ascii table.)
258 token = lex_token(file); /* skip '#' */
260 token = lex_token(file);
262 * If we make it here we found a directive, the supported
263 * directives so far are #include.
265 if (strncmp(file->lastok, "include", sizeof("include")) == 0) {
267 lex_file *next = NULL;
269 * We only suport include " ", not <> like in C (why?)
270 * because the latter is silly.
272 while (*file->lastok != '"' && token != '\n')
273 token = lex_token(file);
275 return error(file, ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
277 copy = util_strdup(file->lastok);
278 next = lex_include(file, copy);
281 error(file, ERROR_INTERNAL, "Include subsystem failure\n");
293 compile_constants_add(c);
299 /* skip all tokens to end of directive */
300 while (token != '\n')
301 token = lex_token(file);
305 token = lex_token(file);
309 compile_constant_debug();
314 for (; i < compile_constants_elements; i++) {
315 mem_d(compile_constants_data[i].name);
316 mem_d(compile_constants_data[i].string);
318 mem_d(compile_constants_data);