Updating to reflect the latest work
[zilutils.git] / zilasm / directives.c
1 /*
2  * directives.c -- part of ZilUtils/ZilAsm
3  *
4  * Copyright (C) 2016 Jason Self <j@jxself.org>
5  *
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.
10  *
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.
15  *
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/>
18  *
19  * SPDX-License-Identifier: AGPL-3.0-or-later
20  */
21
22 #include <stdlib.h>             /* bsearch */
23 #include <string.h>             /* strcmp */
24
25 #include "directives.h"
26
27 #define ARRAY_SIZE(x)  ((sizeof(x)) / (sizeof(x[0])))
28
29 static int
30 byte_handler (const char *args)
31 {
32   /* !!! TODO !!! */
33   return 0;
34 }
35
36 static int
37 end_handler (const char *args)
38 {
39   /* !!! TODO !!! */
40   return 0;
41 }
42
43 static int
44 endi_handler (const char *args)
45 {
46   /* !!! TODO !!! */
47   return 0;
48 }
49
50 static int
51 endt_handler (const char *args)
52 {
53   /* !!! TODO !!! */
54   return 0;
55 }
56
57 static int
58 fstr_handler (const char *args)
59 {
60   /* !!! TODO !!! */
61   return 0;
62 }
63
64 static int
65 funct_handler (const char *args)
66 {
67   /* !!! TODO !!! */
68   return 0;
69 }
70
71 static int
72 gstr_handler (const char *args)
73 {
74   /* !!! TODO !!! */
75   return 0;
76 }
77
78 static int
79 gvar_handler (const char *args)
80 {
81   /* !!! TODO !!! */
82   return 0;
83 }
84
85 static int
86 insert_handler (const char *args)
87 {
88   /* !!! TODO !!! */
89   return 0;
90 }
91
92 static int
93 len_handler (const char *args)
94 {
95   /* !!! TODO !!! */
96   return 0;
97 }
98
99 static int
100 new_handler (const char *args)
101 {
102   /* !!! TODO !!! */
103   return 0;
104 }
105
106 static int
107 object_handler (const char *args)
108 {
109   /* !!! TODO !!! */
110   return 0;
111 }
112
113 static int
114 prop_handler (const char *args)
115 {
116   /* !!! TODO !!! */
117   return 0;
118 }
119
120 static int
121 str_handler (const char *args)
122 {
123   /* !!! TODO !!! */
124   return 0;
125 }
126
127 static int
128 strl_handler (const char *args)
129 {
130   /* !!! TODO !!! */
131   return 0;
132 }
133
134 static int
135 table_handler (const char *args)
136 {
137   /* !!! TODO !!! */
138   return 0;
139 }
140
141 static int
142 vocbeg_handler (const char *args)
143 {
144   /* !!! TODO !!! */
145   return 0;
146 }
147
148 static int
149 vocend_handler (const char *args)
150 {
151   /* !!! TODO !!! */
152   return 0;
153 }
154
155 static int
156 word_handler (const char *args)
157 {
158   /* !!! TODO !!! */
159   return 0;
160 }
161
162 static int
163 zword_handler (const char *args)
164 {
165   /* !!! TODO !!! */
166   return 0;
167 }
168
169 // Sorted array
170 static Directive Directives[] = {
171   "BYTE", byte_handler,
172   "END", end_handler,
173   "ENDI", endi_handler,
174   "ENDT", endt_handler,
175   "FSTR", fstr_handler,
176   "FUNCT", funct_handler,
177   "GSTR", gstr_handler,
178   "GVAR", gvar_handler,
179   "INSERT", insert_handler,
180   "LEN", len_handler,
181   "NEW", new_handler,
182   "OBJECT", object_handler,
183   "PROP", prop_handler,
184   "STR", str_handler,
185   "STRL", strl_handler,
186   "TABLE", table_handler,
187   "VOCBEG", vocbeg_handler,
188   "VOCEND", vocend_handler,
189   "WORD", word_handler,
190   "ZWORD", zword_handler
191 };
192
193 typedef struct
194 {
195   const char *contents;
196   unsigned length;
197 } Name;
198
199 static int
200 namecmp (const void *key, const void *elem)
201 {
202   const Name *p = (Name *) key;
203   const Directive *d = (Directive *) elem;
204
205   int len1 = p->length;
206   int len2 = strlen (d->name);
207
208   int rc = memcmp (p->contents, elem, len1 < len2 ? len1 : len2);
209   return rc ? rc : (len1 - len2);
210 }
211
212 Directive_handler
213 directive_lookup (const char *name, unsigned namelen)
214 {
215   Name n = { name, namelen };
216   Directive *p =
217     (Directive *) bsearch (&n, Directives, ARRAY_SIZE (Directives),
218                            sizeof (Directive), namecmp);
219   return p ? p->handler : NULL;
220 }