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);
38 void compile_constant_debug() {
40 for(; iter < compile_constants_elements; iter++) {
41 constant *c = &compile_constants_data[iter];
43 case TYPE_FLOAT: printf("constant: %s FLOAT %f\n", c->name, c->value[0]); break;
44 case TYPE_VECTOR: printf("constant: %s VECTOR {%f,%f,%f}\n",c->name, c->value[0], c->value[1], c->value[2]); break;
45 case TYPE_STRING: printf("constant: %s STRING %s\n", c->name, c->string); break;
46 case TYPE_VOID: printf("constant: %s VOID %s\n", c->name, c->string); break;
52 * Generates a parse tree out of the lexees generated by the lexer. This
53 * is where the tree is built. This is where valid check is performed.
55 int parse_gen(struct lex_file *file) {
57 while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
63 token = lex_token(file);
64 token = lex_token(file); f = util_strdup(file->lastok);
65 token = lex_token(file);
66 token = lex_token(file); t = util_strdup(file->lastok);
68 typedef_add(file, f, t);
72 token = lex_token(file);
74 token = lex_token(file);
77 error(file, ERROR_PARSE, "Expected a `;` at end of typedef statement");
79 token = lex_token(file);
83 case TOKEN_VOID: goto fall;
84 case TOKEN_STRING: goto fall;
85 case TOKEN_VECTOR: goto fall;
86 case TOKEN_ENTITY: goto fall;
87 case TOKEN_FLOAT: goto fall;
91 int type = token; /* story copy */
94 token = lex_token(file);
96 token = lex_token(file);
99 name = util_strdup(file->lastok);
102 token = lex_token(file);
104 token = lex_token(file);
108 * Definitions go to the defs table, they don't have
109 * any sort of data with them yet.
111 } else if (token == '=') {
112 token = lex_token(file);
114 token = lex_token(file);
116 /* strings are in file->lastok */
119 error(file, ERROR_PARSE, "Cannot assign value to type void\n");
121 /* TODO: Validate (end quote), strip quotes for constant add, name constant */
123 if (*file->lastok != '"')
124 error(file, ERROR_PARSE, "Expected a '\"' (quote) for string constant\n");
125 /* add the compile-time constant */
126 compile_constants_add((constant){
127 .name = util_strdup(name),
130 .string = util_strdup(file->lastok)
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 */
213 compile_constants_add((constant){
214 .name = util_strdup(name),
217 [0] = compile_calc_x,
218 [1] = compile_calc_y,
227 case TOKEN_FLOAT: /*TODO: validate, constant generation, name constant */
229 error(file, ERROR_PARSE, "Expected numeric constant for float constant\n");
230 compile_constants_add((constant){
231 .name = util_strdup(name),
238 } else if (token == '(') {
239 printf("FUNCTION ??\n");
245 * From here down is all language punctuation: There is no
246 * need to actual create tokens from these because they're already
247 * tokenized as these individual tokens (which are in a special area
248 * of the ascii table which doesn't conflict with our other tokens
249 * which are higer than the ascii table.)
252 token = lex_token(file); /* skip '#' */
254 token = lex_token(file);
256 * If we make it here we found a directive, the supported
257 * directives so far are #include.
259 if (strncmp(file->lastok, "include", sizeof("include")) == 0) {
261 * We only suport include " ", not <> like in C (why?)
262 * because the latter is silly.
264 while (*file->lastok != '"' && token != '\n')
265 token = lex_token(file);
267 return error(file, ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
269 char *copy = util_strdup(file->lastok);
270 struct lex_file *next = lex_include(file, copy);
273 error(file, ERROR_INTERNAL, "Include subsystem failure\n");
276 compile_constants_add((constant) {
286 /* skip all tokens to end of directive */
287 while (token != '\n')
288 token = lex_token(file);
292 token = lex_token(file);
296 compile_constant_debug();