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