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