2 * directives.c -- part of ZilUtils/ZilAsm
4 * Copyright (C) 2016, 2020 Jason Self <j@jxself.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
19 * SPDX-License-Identifier: AGPL-3.0-or-later
22 #include <stdlib.h> /* bsearch */
23 #include <string.h> /* strcmp */
31 #include "directives.h"
33 #define ARRAY_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
35 int g_stopParsing = 0;
36 stack<FILE*> g_fileHandlers;
41 byte_handler (const char *args)
48 end_handler (const char *args)
55 endi_handler (const char *args)
62 endt_handler (const char *args)
69 fstr_handler (const char *args)
76 funct_handler (const char *args)
83 gstr_handler (const char *args)
90 gvar_handler (const char *args)
97 insert_handler (const char *args)
100 char *p = (char*)args;
101 while (*p == ' ') p++;
111 while (*p != '"' && *p != 0 ) ;
119 } while (*p != ' ' && *p != '\n' && *p != '\r' && *p != '\t' && *p != 0);
122 if (file_name.find('.') == string::npos)
128 char delimeter = '\\';
130 char delimeter = '/';
134 if (file_name.rfind(delimeter) == string::npos)
136 pc.current_directory = "";
137 pc.current_file_name = file_name;
141 pc.current_directory = file_name.substr(0, file_name.rfind(delimeter)+1);
142 pc.current_file_name = file_name.substr(file_name.rfind(delimeter)+1);
145 pc.current_directory = g_parsing_contexts.top().current_directory + pc.current_directory;
146 //pc.current_file_name = s;
147 g_parsing_contexts.push(pc);
150 unsigned saveLineNumber = g_currentLineNumber;
151 parse_file(); //s.c_str());
152 g_currentLineNumber = saveLineNumber;
157 len_handler (const char *args)
164 newdirective_handler (const char *args)
171 object_handler (const char *args)
178 prop_handler (const char *args)
185 str_handler (const char *args)
192 strl_handler (const char *args)
199 table_handler (const char *args)
206 vocbeg_handler (const char *args)
213 vocend_handler (const char *args)
220 word_handler (const char *args)
227 zword_handler (const char *args)
234 static Directive Directives[] = {
235 "BYTE", byte_handler,
237 "ENDI", endi_handler,
238 "ENDT", endt_handler,
239 "FSTR", fstr_handler,
240 "FUNCT", funct_handler,
241 "GSTR", gstr_handler,
242 "GVAR", gvar_handler,
243 "INSERT", insert_handler,
245 "NEW", newdirective_handler,
246 "OBJECT", object_handler,
247 "PROP", prop_handler,
249 "STRL", strl_handler,
250 "TABLE", table_handler,
251 "VOCBEG", vocbeg_handler,
252 "VOCEND", vocend_handler,
253 "WORD", word_handler,
254 "ZWORD", zword_handler
259 const char *contents;
264 namecmp (const void *key, const void *elem)
266 const Name *p = (Name *) key;
267 const Directive *d = (Directive *) elem;
269 int len1 = p->length;
270 int len2 = strlen (d->name);
272 int rc = memcmp (p->contents, elem, len1 < len2 ? len1 : len2);
273 return rc ? rc : (len1 - len2);
277 directive_lookup (const char *name, unsigned namelen)
279 Name n = { name, namelen };
281 (Directive *) bsearch (&n, Directives, ARRAY_SIZE (Directives),
282 sizeof (Directive), namecmp);
283 return p ? p->handler : NULL;