searchkw.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * GTools C compiler
  3. * =================
  4. * source file :
  5. * keyword searching
  6. *
  7. * Copyright 2001-2004 Paul Froissart.
  8. * Credits to Christoph van Wuellen and Matthew Brandt.
  9. * All commercial rights reserved.
  10. *
  11. * This compiler may be redistributed as long there is no
  12. * commercial interest. The compiler must not be redistributed
  13. * without its full sources. This notice must stay intact.
  14. */
  15. #include "define.h"
  16. _FILE(__FILE__)
  17. #include "c.h"
  18. #include "expr.h"
  19. #include "gen.h"
  20. #include "cglbdec.h"
  21. xstatic readonly struct kwblk {
  22. char *word;
  23. enum(e_sym) stype;
  24. #ifndef PC
  25. int pad;
  26. #endif
  27. } keywords[] = {
  28. {
  29. "__asm__", kw_asm
  30. }, {
  31. "__attribute__", kw_attr
  32. }, {
  33. "__builtin_constant_p", kwb_constant_p
  34. }, {
  35. "__c__", kw_c
  36. }, {
  37. "__count__", kw_count
  38. }, {
  39. "__eval__", kw_eval
  40. }, {
  41. "__loop__", kw_loop
  42. }, {
  43. "__softcast__", kw_softcast
  44. }, {
  45. "__until__", kw_until
  46. }, {
  47. "alloca", kw_alloca
  48. }, {
  49. "asm", kw_asm
  50. }, {
  51. "auto", kw_auto
  52. }, {
  53. "break", kw_break
  54. }, {
  55. "case", kw_case
  56. }, {
  57. "char", kw_char
  58. }, {
  59. "const", kw_const
  60. }, {
  61. "continue", kw_continue
  62. }, {
  63. "default", kw_default
  64. }, {
  65. "defined", kw_defined
  66. }, {
  67. "do", kw_do
  68. }, {
  69. "double", kw_double
  70. }, {
  71. "else", kw_else
  72. }, {
  73. "enum", kw_enum
  74. }, {
  75. "extern", kw_extern
  76. }, {
  77. "float", kw_float
  78. }, {
  79. "for", kw_for
  80. }, {
  81. "goto", kw_goto
  82. }, {
  83. "if", kw_if
  84. }, {
  85. "incbin", kw_incbin
  86. }, {
  87. "int", kw_int
  88. }, {
  89. "long", kw_long
  90. }, {
  91. "loop", kw_loop
  92. }, {
  93. "register", kw_register
  94. }, {
  95. "return", kw_return
  96. }, {
  97. "short", kw_short
  98. }, {
  99. "signed", kw_signed
  100. }, {
  101. "sizeof", kw_sizeof
  102. }, {
  103. "static", kw_static
  104. }, {
  105. "struct", kw_struct
  106. }, {
  107. "switch", kw_switch
  108. }, {
  109. "typedef", kw_typedef
  110. }, {
  111. "typeof", kw_typeof
  112. }, {
  113. "union", kw_union
  114. }, {
  115. "unsigned", kw_unsigned
  116. }, {
  117. "until", kw_until
  118. }, {
  119. "void", kw_void
  120. }, {
  121. "volatile", kw_volatile
  122. }, {
  123. "while", kw_while
  124. /* }, {
  125. 0, 0*/
  126. }
  127. };
  128. #define kw_N 48
  129. /*
  130. * Dichotomic search allows a max of 6 comparisons instead of 50...
  131. */
  132. void searchkw(void) {
  133. char *s1,*s2,c;
  134. const struct kwblk *kwbp;
  135. int a=0,b=kw_N-1,m;
  136. #ifdef PC
  137. if (sizeof(keywords)/sizeof(struct kwblk)!=kw_N)
  138. fatal("BUILD ERROR: INVALID KEYWORDS #");
  139. #endif
  140. while (a<=b) {
  141. m=(a+b)>>1;
  142. kwbp=&keywords[m];
  143. s1=lastid;
  144. s2=kwbp->word;
  145. do {
  146. if (!(c=*s2++)) {
  147. if (!*s1)
  148. lastst = kwbp->stype;
  149. if (is_lang_ext(lastst) && lastid[0]!='_' && !(flags&X_LANG_EXT))
  150. lastst = id;
  151. if (*s1!='u') // the only case when a kw is the beginning of another one
  152. return; // is 'do' vs 'double'
  153. }
  154. } while (*s1++==c);
  155. if (s1[-1]<c) // il faut aller plus haut
  156. b=m-1;
  157. else a=m+1;
  158. }
  159. }
  160. /*searchkw() // non-dichotomic version
  161. {
  162. char *s1,*s2,c;
  163. struct kwblk *kwbp;
  164. kwbp = keywords;
  165. while (kwbp->word != 0) {
  166. s1 = lastid;
  167. s2 = kwbp->word;
  168. kwbp++;
  169. do {
  170. if (!(c=*s2++)) {
  171. if (!*s1++)
  172. lastst = (--kwbp)->stype;
  173. return;
  174. }
  175. } while (*s1++==c);
  176. }
  177. }*/
  178. // vim:ts=4:sw=4