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 && \
58 token != ERROR_COMPILER && \
59 token != ERROR_INTERNAL && \
60 token != ERROR_PARSE && \
61 token != ERROR_PREPRO && file->length >= 0) {
67 token = lex_token(file);
68 token = lex_token(file); f = util_strdup(file->lastok);
69 token = lex_token(file);
70 token = lex_token(file); t = util_strdup(file->lastok);
76 token = lex_token(file);
78 token = lex_token(file);
81 error(ERROR_PARSE, "%s:%d Expected a `;` at end of typedef statement\n", file->name, file->line);
83 token = lex_token(file);
87 case TOKEN_VOID: goto fall;
88 case TOKEN_STRING: goto fall;
89 case TOKEN_VECTOR: goto fall;
90 case TOKEN_ENTITY: goto fall;
91 case TOKEN_FLOAT: goto fall;
95 int type = token; /* story copy */
98 token = lex_token(file);
100 token = lex_token(file);
103 name = util_strdup(file->lastok);
106 token = lex_token(file);
108 token = lex_token(file);
112 * Definitions go to the defs table, they don't have
113 * any sort of data with them yet.
115 } else if (token == '=') {
116 token = lex_token(file);
118 token = lex_token(file);
120 /* strings are in file->lastok */
123 return error(ERROR_PARSE, "%s:%d Cannot assign value to type void\n", file->name, file->line);
125 /* TODO: Validate (end quote), strip quotes for constant add, name constant */
127 if (*file->lastok != '"')
128 error(ERROR_PARSE, "%s:%d Expected a '\"' (quote) for string constant\n", file->name, file->line);
129 /* add the compile-time constant */
130 compile_constants_add((constant){
131 .name = util_strdup(name),
134 .string = util_strdup(file->lastok)
137 /* TODO: name constant, old qc vec literals, whitespace fixes, name constant */
139 float compile_calc_x = 0;
140 float compile_calc_y = 0;
141 float compile_calc_z = 0;
142 int compile_calc_d = 0; /* dot? */
143 int compile_calc_s = 0; /* sign (-, +) */
145 char compile_data[1024];
146 char *compile_eval = compile_data;
149 error(ERROR_PARSE, "%s:%d Expected initializer list `{`,`}` for vector constant\n", file->name, file->line);
152 * This parses a single vector element: x,y & z. This will handle all the
153 * complicated mechanics of a vector, and can be extended as well. This
154 * is a rather large macro, and is #undef'd after it's use below.
156 #define PARSE_VEC_ELEMENT(NAME, BIT) \
157 token = lex_token(file); \
159 token = lex_token(file); \
161 compile_calc_d = 1; \
162 if (!isdigit(token) && !compile_calc_d && token != '+' && token != '-') \
163 error(ERROR_PARSE,"%s:%d Invalid constant initializer element %c for vector, must be numeric\n", file->name, file->line, NAME); \
165 compile_calc_s = '+'; \
166 if (token == '-' && !compile_calc_s) \
167 compile_calc_s = '-'; \
168 while (isdigit(token) || token == '.' || token == '+' || token == '-') { \
169 *compile_eval++ = token; \
170 token = lex_token(file); \
171 if (token == '.' && compile_calc_d) { \
172 error(ERROR_PARSE, "%s:%d Invalid constant initializer element %c for vector, must be numeric.\n", file->name, file->line, NAME); \
173 token = lex_token(file); \
175 if ((token == '-' || token == '+') && compile_calc_s) { \
176 error(ERROR_PARSE, "%s:%d Invalid constant initializer sign for vector element %c\n", file->name, file->line, NAME); \
177 token = lex_token(file); \
179 else if (token == '.' && !compile_calc_d) \
180 compile_calc_d = 1; \
181 else if (token == '-' && !compile_calc_s) \
182 compile_calc_s = '-'; \
183 else if (token == '+' && !compile_calc_s) \
184 compile_calc_s = '+'; \
187 token = lex_token(file); \
189 if (token != ',' && token != ' ') \
190 error(ERROR_PARSE, "%s:%d invalid constant initializer element %c for vector (missing spaces, or comma delimited list?)\n", file->name, file->line, NAME); \
191 } else if (token != '}') { \
192 error(ERROR_PARSE, "%s:%d Expected `}` on end of constant initialization for vector\n", file->name, file->line); \
194 compile_calc_##BIT = atof(compile_data); \
195 compile_calc_d = 0; \
196 compile_calc_s = 0; \
197 compile_eval = &compile_data[0]; \
198 memset(compile_data, 0, sizeof(compile_data))
201 * Parse all elements using the macro above.
202 * We must undef the macro afterwards.
204 PARSE_VEC_ELEMENT('x', x);
205 PARSE_VEC_ELEMENT('y', y);
206 PARSE_VEC_ELEMENT('z', z);
207 #undef PARSE_VEC_ELEMENT
209 /* Check for the semi-colon... */
210 token = lex_token(file);
212 token = lex_token(file);
214 error(ERROR_PARSE, "%s:%d Expected `;` on end of constant initialization for vector\n", file->name, file->line);
216 /* add the compile-time constant */
217 compile_constants_add((constant){
218 .name = util_strdup(name),
221 [0] = compile_calc_x,
222 [1] = compile_calc_y,
231 case TOKEN_FLOAT: /*TODO: validate, constant generation, name constant */
233 error(ERROR_PARSE, "%s:%d Expected numeric constant for float constant\n");
234 compile_constants_add((constant){
235 .name = util_strdup(name),
242 } else if (token == '(') {
243 printf("FUNCTION ??\n");
249 * From here down is all language punctuation: There is no
250 * need to actual create tokens from these because they're already
251 * tokenized as these individual tokens (which are in a special area
252 * of the ascii table which doesn't conflict with our other tokens
253 * which are higer than the ascii table.)
256 token = lex_token(file); /* skip '#' */
258 token = lex_token(file);
260 * If we make it here we found a directive, the supported
261 * directives so far are #include.
263 if (strncmp(file->lastok, "include", sizeof("include")) == 0) {
265 * We only suport include " ", not <> like in C (why?)
266 * because the latter is silly.
268 while (*file->lastok != '"' && token != '\n')
269 token = lex_token(file);
271 return error(ERROR_PARSE, "%d: Invalid use of include preprocessor directive: wanted #include \"file.h\"\n", file->line-1);
273 char *copy = util_strdup(file->lastok);
274 struct lex_file *next = lex_include(file, copy);
277 error(ERROR_INTERNAL, "Include subsystem failure\n");
280 compile_constants_add((constant) {
290 /* skip all tokens to end of directive */
291 while (token != '\n')
292 token = lex_token(file);
296 token = lex_token(file);
300 compile_constant_debug();