nlist.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "cpp.h"
  5. extern int getopt(int, char *const *, const char *);
  6. extern char *optarg;
  7. extern int optind;
  8. extern int verbose;
  9. extern int Cplusplus;
  10. Nlist *kwdefined;
  11. char wd[128];
  12. #define NLSIZE 128
  13. static Nlist *nlist[NLSIZE];
  14. struct kwtab {
  15. char *kw;
  16. int val;
  17. int flag;
  18. } kwtab[] = {
  19. "if", KIF, ISKW,
  20. "ifdef", KIFDEF, ISKW,
  21. "ifndef", KIFNDEF, ISKW,
  22. "elif", KELIF, ISKW,
  23. "else", KELSE, ISKW,
  24. "endif", KENDIF, ISKW,
  25. "include", KINCLUDE, ISKW,
  26. "define", KDEFINE, ISKW,
  27. "undef", KUNDEF, ISKW,
  28. "line", KLINE, ISKW,
  29. "error", KERROR, ISKW,
  30. "pragma", KPRAGMA, ISKW,
  31. "eval", KEVAL, ISKW,
  32. "defined", KDEFINED, ISDEFINED+ISUNCHANGE,
  33. "__LINE__", KLINENO, ISMAC+ISUNCHANGE,
  34. "__FILE__", KFILE, ISMAC+ISUNCHANGE,
  35. "__DATE__", KDATE, ISMAC+ISUNCHANGE,
  36. "__TIME__", KTIME, ISMAC+ISUNCHANGE,
  37. "__STDC__", KSTDC, ISUNCHANGE,
  38. NULL
  39. };
  40. unsigned long namebit[077+1];
  41. Nlist *np;
  42. void
  43. setup_kwtab(void)
  44. {
  45. struct kwtab *kp;
  46. Nlist *np;
  47. Token t;
  48. static Token deftoken[1] = {{ NAME, 0, 0, 0, 7, (uchar*)"defined" }};
  49. static Tokenrow deftr = { deftoken, deftoken, deftoken+1, 1 };
  50. for (kp=kwtab; kp->kw; kp++) {
  51. t.t = (uchar*)kp->kw;
  52. t.len = strlen(kp->kw);
  53. np = lookup(&t, 1);
  54. np->flag = kp->flag;
  55. np->val = kp->val;
  56. if (np->val == KDEFINED) {
  57. kwdefined = np;
  58. np->val = NAME;
  59. np->vp = &deftr;
  60. np->ap = 0;
  61. }
  62. }
  63. }
  64. Nlist *
  65. lookup(Token *tp, int install)
  66. {
  67. unsigned int h;
  68. Nlist *np;
  69. uchar *cp, *cpe;
  70. h = 0;
  71. for (cp=tp->t, cpe=cp+tp->len; cp<cpe; )
  72. h += *cp++;
  73. h %= NLSIZE;
  74. np = nlist[h];
  75. while (np) {
  76. if (*tp->t==*np->name && tp->len==np->len
  77. && strncmp((char*)tp->t, (char*)np->name, tp->len)==0)
  78. return np;
  79. np = np->next;
  80. }
  81. if (install) {
  82. np = new(Nlist);
  83. np->vp = NULL;
  84. np->ap = NULL;
  85. np->flag = 0;
  86. np->val = 0;
  87. np->len = tp->len;
  88. np->name = newstring(tp->t, tp->len, 0);
  89. np->next = nlist[h];
  90. nlist[h] = np;
  91. quickset(tp->t[0], tp->len>1? tp->t[1]:0);
  92. return np;
  93. }
  94. return NULL;
  95. }