directives.c 3.7 KB

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