blob: 14903cfb61ae5a59d1274e2d08ef8ebe721b35a5 (
about) (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/*
keyword.cc -- keywords and identifiers
*/
#include <stdlib.h>
#include "glob.hh"
#include "lexer.hh"
//#include "mudobs.hh"
//#include "gram.hh"
/* for the keyword table */
struct Keyword_ent
{
const char *name;
int tokcode;
};
struct Keyword_table
{
Keyword_ent *table;
int maxkey;
Keyword_table(Keyword_ent *);
int lookup(const char *s) const;
};
/* for qsort */
int
tabcmp(const void * p1, const void * p2)
{
return strcmp(((const Keyword_ent *) p1)->name,
((const Keyword_ent *) p2)->name);
}
Keyword_table::Keyword_table(Keyword_ent *tab)
{
table = tab;
/* count keywords */
for (maxkey = 0; table[maxkey].name; maxkey++);
/* sort them */
qsort(table, maxkey, sizeof(Keyword_ent), tabcmp);
}
/*
lookup with binsearch, return tokencode.
*/
int
Keyword_table::lookup(const char *s)const
{
int lo,
hi,
cmp,
result;
lo = 0;
hi = maxkey;
/* binary search */
do
{
cmp = (lo + hi) / 2;
result = strcmp(s, table[cmp].name);
if (result < 0)
hi = cmp;
else
lo = cmp;
}
while (hi - lo > 1);
if (!strcmp(s, table[lo].name))
{
return table[lo].tokcode;
} else
return -1; /* not found */
}
|