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 int offset; /* location in globals */
38 VECTOR_MAKE(globals, assembly_constants);
41 * Assembly text processing: this handles the internal collection
42 * of text to allow parsing and assemblation.
44 static char *const asm_getline(size_t *byte, FILE *fp) {
46 ssize_t read = util_getline(&line, byte, fp);
56 * Entire external interface for main.c - to perform actual assemblation
59 void asm_init(const char *file, FILE **fp) {
60 *fp = fopen(file, "r");
63 void asm_close(FILE *fp) {
69 for (; i < assembly_constants_elements; i++)
70 mem_d(assembly_constants_data[i].name);
71 mem_d(assembly_constants_data);
75 * Parses a type, could be global or not depending on the
76 * assembly state: global scope with assignments are constants.
77 * globals with no assignments are globals. Function body types
80 static inline bool asm_parse_type(const char *skip, size_t line, asm_state *state) {
81 if (strstr(skip, "FLOAT:") == &skip[0]) { return true; }
82 if (strstr(skip, "VECTOR:") == &skip[0]) { return true; }
83 if (strstr(skip, "ENTITY:") == &skip[0]) { return true; }
84 if (strstr(skip, "FIELD:") == &skip[0]) { return true; }
85 if (strstr(skip, "STRING:") == &skip[0]) { return true; }
90 * Parses a function: trivial case, handles occurances of duplicated
91 * names among other things. Ensures valid name as well, and even
92 * internal engine function selection.
94 static inline bool asm_parse_func(const char *skip, size_t line, asm_state *state) {
95 if (*state == ASM_FUNCTION && (strstr(skip, "FUNCTION:") == &skip[0]))
98 if (strstr(skip, "FUNCTION:") == &skip[0]) {
99 char *copy = util_strsws(skip+10);
100 char *name = util_strchp(copy, strchr(copy, '\0'));
102 /* TODO: failure system, missing name */
104 printf("expected name on function\n");
109 /* TODO: failure system, invalid name */
110 if (!isalpha(*name) || util_strupper(name)) {
111 printf("invalid identifer for function name\n");
118 * Function could be internal function, look for $
121 if (strchr(name, ',')) {
122 char *find = strchr(name, ',') + 1;
124 /* skip whitespace */
125 while (*find == ' ' || *find == '\t')
129 printf("expected $ for internal function selection, got %s instead\n", find);
135 if (!isdigit(*find)) {
136 printf("invalid internal identifier, expected valid number\n");
143 name = util_strchp(name, strchr(name, ','));
146 * Now add the following items to the code system:
148 * definition (optional)
152 code_functions_add((prog_section_function){
153 -atoi(find), /* needs to be negated */
155 .name = code_chars_elements,
158 code_defs_add((prog_section_def){
159 .type = TYPE_FUNCTION,
160 .offset = code_globals_elements,
161 .name = code_chars_elements
163 code_globals_add(code_chars_elements);
165 code_chars_put(name, strlen(name));
166 code_chars_add('\0');
168 /* TODO: sanatize `find` to ensure all numerical digits */
170 printf("found internal function %s, -%d\n", name, atoi(find));
181 void asm_parse(FILE *fp) {
184 long line = 1; /* current line */
185 size_t size = 0; /* size of line */
186 asm_state state = ASM_NULL;
193 util_debug("ASM", x); \
194 } while (0); continue
196 while ((data = asm_getline (&size, fp)) != NULL) {
197 char *copy = util_strsws(data); /* skip whitespace */
198 skip = util_strrnl(copy); /* delete newline */
201 if(asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); }
203 if(asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); }
205 /* statement closure */
206 if (state == ASM_FUNCTION && (
207 (strstr(skip, "DONE") == &skip[0])||
208 (strstr(skip, "RETURN") == &skip[0]))) state = ASM_NULL;
210 /* TODO: everything */
212 asm_end("asm_parse_end\n");