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 */
122 compile_constants_add((constant){
123 .name = util_strdup(name),
126 .string = util_strdup(file->lastok)
129 /* TODO: name constant, old qc vec literals, whitespace fixes, name constant */
131 float compile_calc_x = 0;
132 float compile_calc_y = 0;
133 float compile_calc_z = 0;
134 int compile_calc_d = 0; /* dot? */
135 int compile_calc_s = 0; /* sign (-, +) */
137 char compile_data[1024];
138 char *compile_eval = compile_data;
141 error(file, ERROR_PARSE, "Expected initializer list {} for vector constant\n");
144 * This parses a single vector element: x,y & z. This will handle all the
145 * complicated mechanics of a vector, and can be extended as well. This
146 * is a rather large macro, and is #undef'd after it's use below.
148 #define PARSE_VEC_ELEMENT(NAME, BIT) \
149 token = lex_token(file); \
151 token = lex_token(file); \
153 compile_calc_d = 1; \
154 if (!isdigit(token) && !compile_calc_d && token != '+' && token != '-') \
155 error(file, ERROR_PARSE,"Invalid constant initializer element %c for vector, must be numeric\n", NAME); \
157 compile_calc_s = '+'; \
158 if (token == '-' && !compile_calc_s) \
159 compile_calc_s = '-'; \
160 while (isdigit(token) || token == '.' || token == '+' || token == '-') { \
161 *compile_eval++ = token; \
162 token = lex_token(file); \
163 if (token == '.' && compile_calc_d) { \
164 error(file, ERROR_PARSE, "Invalid constant initializer element %c for vector, must be numeric.\n", NAME); \
165 token = lex_token(file); \
167 if ((token == '-' || token == '+') && compile_calc_s) { \
168 error(file, ERROR_PARSE, "Invalid constant initializer sign for vector element %c\n", NAME); \
169 token = lex_token(file); \
171 else if (token == '.' && !compile_calc_d) \
172 compile_calc_d = 1; \
173 else if (token == '-' && !compile_calc_s) \
174 compile_calc_s = '-'; \
175 else if (token == '+' && !compile_calc_s) \
176 compile_calc_s = '+'; \
179 token = lex_token(file); \
181 if (token != ',' && token != ' ') \
182 error(file, ERROR_PARSE, "invalid constant initializer element %c for vector (missing spaces, or comma delimited list?)\n", NAME); \
183 } else if (token != '}') { \
184 error(file, ERROR_PARSE, "Expected `}` on end of constant initialization for vector\n"); \
186 compile_calc_##BIT = atof(compile_data); \
187 compile_calc_d = 0; \
188 compile_calc_s = 0; \
189 compile_eval = &compile_data[0]; \
190 memset(compile_data, 0, sizeof(compile_data))
193 * Parse all elements using the macro above.
194 * We must undef the macro afterwards.
196 PARSE_VEC_ELEMENT('x', x);
197 PARSE_VEC_ELEMENT('y', y);
198 PARSE_VEC_ELEMENT('z', z);
199 #undef PARSE_VEC_ELEMENT
201 /* Check for the semi-colon... */
202 token = lex_token(file);
204 token = lex_token(file);
206 error(file, ERROR_PARSE, "Expected `;` on end of constant initialization for vector\n");
208 /* add the compile-time constant */
209 compile_constants_add((constant){
210 .name = util_strdup(name),
213 [0] = compile_calc_x,
214 [1] = compile_calc_y,
223 case TOKEN_FLOAT: /*TODO: validate, constant generation, name constant */
225 error(file, ERROR_PARSE, "Expected numeric constant for float constant\n");
226 compile_constants_add((constant){
227 .name = util_strdup(name),
234 } else if (token == '(') {
235 printf("FUNCTION ??\n");
241 * From here down is all language punctuation: There is no
242 * need to actual create tokens from these because they're already
243 * tokenized as these individual tokens (which are in a special area
244 * of the ascii table which doesn't conflict with our other tokens
245 * which are higer than the ascii table.)
248 token = lex_token(file); /* skip '#' */
250 token = lex_token(file);
252 * If we make it here we found a directive, the supported
253 * directives so far are #include.
255 if (strncmp(file->lastok, "include", sizeof("include")) == 0) {
257 * We only suport include " ", not <> like in C (why?)
258 * because the latter is silly.
260 while (*file->lastok != '"' && token != '\n')
261 token = lex_token(file);
263 return error(file, ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
265 char *copy = util_strdup(file->lastok);
266 lex_file *next = lex_include(file, copy);
269 error(file, ERROR_INTERNAL, "Include subsystem failure\n");
272 compile_constants_add((constant) {
282 /* skip all tokens to end of directive */
283 while (token != '\n')
284 token = lex_token(file);
288 token = lex_token(file);
292 compile_constant_debug();
297 for (; i < compile_constants_elements; i++) {
298 mem_d(compile_constants_data[i].name);
299 mem_d(compile_constants_data[i].string);
301 mem_d(compile_constants_data);