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 * Following parse states:
26 * ASM_FUNCTION -- in a function accepting input statements
35 char *name; /* name of constant */
36 char type; /* type, float, vector, string */
37 char elem; /* 0=x, 1=y, or 2=Z? */
38 int offset; /* location in globals */
40 VECTOR_MAKE(globals, assembly_constants);
43 * Assembly text processing: this handles the internal collection
44 * of text to allow parsing and assemblation.
46 static char *const asm_getline(size_t *byte, FILE *fp) {
48 size_t read = util_getline(&line, byte, fp);
58 * Entire external interface for main.c - to perform actual assemblation
61 void asm_init(const char *file, FILE **fp) {
62 *fp = fopen(file, "r");
65 void asm_close(FILE *fp) {
71 for (; i < assembly_constants_elements; i++)
72 mem_d(assembly_constants_data[i].name);
73 mem_d(assembly_constants_data);
77 * Dumps all values of all constants and assembly related
78 * information obtained during the assembly procedure.
82 for (; i < assembly_constants_elements; i++) {
83 globals *g = &assembly_constants_data[i];
86 util_debug("ASM", "vector %s %c[%f]\n", g->name,
87 (g->elem == 0) ? 'X' :(
88 (g->elem == 1) ? 'Y' :
89 (g->elem == 2) ? 'Z' :' '),
90 INT2FLT(code_globals_data[g->offset])
99 * Parses a type, could be global or not depending on the
100 * assembly state: global scope with assignments are constants.
101 * globals with no assignments are globals. Function body types
104 static GMQCC_INLINE bool asm_parse_type(const char *skip, size_t line, asm_state *state) {
105 if (!(strstr(skip, "FLOAT:") == &skip[0]) &&
106 (strstr(skip, "VECTOR:") == &skip[0]) &&
107 (strstr(skip, "ENTITY:") == &skip[0]) &&
108 (strstr(skip, "FIELD:") == &skip[0]) &&
109 (strstr(skip, "STRING:") == &skip[0])) return false;
111 /* TODO: determine if constant, global, or local */
113 /* VECTOR */ case 'V': {
119 char *find = (char*)skip + 7;
120 char *name = (char*)skip + 7;
121 while (*find == ' ' || *find == '\t') find++;
124 if (strchr(find, ',')) {
126 *strchr((name = util_strdup(find)), ',')='\0';
128 find += strlen(name) + 1;
129 while (*find == ' ' || *find == '\t') find++;
131 if (util_strupper(name) || isdigit(*name)) {
132 printf("invalid name for vector variable\n");
136 * Parse all three elements of the vector. This will only
137 * pass the first try if we hit a constant, otherwise it's
140 #define PARSE_ELEMENT(X,Y,Z) \
141 if (isdigit(*X) || *X == '-'||*X == '+') { \
142 bool negated = (*X == '-'); \
143 if (negated || *X == '+') { X++; } \
144 Y = (negated)?-atof(X):atof(X); \
145 X = strchr(X, ','); \
149 PARSE_ELEMENT(find, val1, { find ++; while (*find == ' ') { find ++; } });
150 PARSE_ELEMENT(find, val2, { find ++; while (*find == ' ') { find ++; } });
151 PARSE_ELEMENT(find, val3, { find ++; /* no need to do anything here */ });
153 #define BUILD_ELEMENT(X,Y) \
154 global.type = TYPE_VECTOR; \
155 global.name = util_strdup(name); \
157 global.offset = code_globals_elements; \
158 assembly_constants_add(global); \
159 code_globals_add(FLT2INT(Y))
160 BUILD_ELEMENT(0, val1);
161 BUILD_ELEMENT(1, val2);
162 BUILD_ELEMENT(2, val3);
165 /* TODO global not constant */
169 /* ENTITY */ case 'E': {
170 const char *find = skip + 7;
171 while (*find == ' ' || *find == '\t') find++;
172 printf("found ENTITY %s\n", find);
175 /* STRING */ case 'S': {
176 const char *find = skip + 7;
177 while (*find == ' ' || *find == '\t') find++;
178 printf("found STRING %s\n", find);
187 * Parses a function: trivial case, handles occurances of duplicated
188 * names among other things. Ensures valid name as well, and even
189 * internal engine function selection.
191 static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state *state) {
192 if (*state == ASM_FUNCTION && (strstr(skip, "FUNCTION:") == &skip[0]))
195 if (strstr(skip, "FUNCTION:") == &skip[0]) {
196 char *copy = util_strsws(skip+10);
197 char *name = util_strchp(copy, strchr(copy, '\0'));
199 /* TODO: failure system, missing name */
201 printf("expected name on function\n");
206 /* TODO: failure system, invalid name */
207 if (!isalpha(*name) || util_strupper(name)) {
208 printf("invalid identifer for function name\n");
215 * Function could be internal function, look for $
218 if (strchr(name, ',')) {
219 prog_section_function function;
220 prog_section_def def;
222 char *find = strchr(name, ',') + 1;
224 /* skip whitespace */
225 while (*find == ' ' || *find == '\t')
229 printf("expected $ for internal function selection, got %s instead\n", find);
235 if (!isdigit(*find)) {
236 printf("invalid internal identifier, expected valid number\n");
241 *strchr(name, ',')='\0';
244 * Now add the following items to the code system:
246 * definition (optional)
250 function.entry = -atoi(find);
251 function.firstlocal = 0;
252 function.profile = 0;
253 function.name = code_chars_elements;
256 def.type = TYPE_FUNCTION;
257 def.offset = code_globals_elements;
258 def.name = code_chars_elements;
259 code_functions_add(function);
261 code_globals_add (code_chars_elements);
262 code_chars_put (name, strlen(name));
263 code_chars_add ('\0');
266 * Sanatize the numerical constant used to select the
267 * internal function. Must ensure it's all numeric, since
268 * atoi can silently drop characters from a string and still
269 * produce a valid constant that would lead to runtime problems.
271 if (util_strdigit(find))
272 printf("found internal function %s, -%d\n", name, atoi(find));
274 printf("invalid internal function identifier, must be all numeric\n");
277 printf("Found function %s\n", name);
287 void asm_parse(FILE *fp) {
290 long line = 1; /* current line */
291 size_t size = 0; /* size of line */
292 asm_state state = ASM_NULL;
299 util_debug("ASM", x); \
300 } while (0); continue
302 while ((data = asm_getline (&size, fp)) != NULL) {
303 char *copy = util_strsws(data); /* skip whitespace */
304 skip = util_strrnl(copy); /* delete newline */
307 if(asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); }
309 if(asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); }
311 /* statement closure */
312 if (state == ASM_FUNCTION && (
313 (strstr(skip, "DONE") == &skip[0])||
314 (strstr(skip, "RETURN") == &skip[0]))) state = ASM_NULL;
316 /* TODO: everything */
318 asm_end("asm_parse_end\n");