Commit | Line | Data |
---|---|---|
9a3f3b7b ML |
1 | /* Copyright (c) 1982 Regents of the University of California */ |
2 | ||
e1f4dbca | 3 | static char sccsid[] = "@(#)keywords.c 1.4 (Berkeley) %G%"; |
9a3f3b7b ML |
4 | |
5 | /* | |
6 | * Keyword management. | |
7 | */ | |
8 | ||
9 | #include "defs.h" | |
10 | #include "keywords.h" | |
11 | #include "scanner.h" | |
12 | #include "names.h" | |
13 | #include "symbols.h" | |
14 | #include "tree.h" | |
15 | #include "y.tab.h" | |
16 | ||
17 | #ifndef public | |
18 | #include "scanner.h" | |
19 | #endif | |
20 | ||
21 | private String reserved[] ={ | |
22 | "alias", "and", "assign", "at", "call", "catch", "cont", | |
2fd0f574 | 23 | "debug", "delete", "div", "down", "dump", "edit", "file", "func", |
9a3f3b7b ML |
24 | "gripe", "help", "if", "ignore", "in", |
25 | "list", "mod", "next", "nexti", "nil", "not", "or", | |
2fd0f574 | 26 | "print", "psym", "quit", "rerun", "return", "run", |
9a3f3b7b | 27 | "sh", "skip", "source", "status", "step", "stepi", |
2fd0f574 | 28 | "stop", "stopi", "trace", "tracei", "up", |
9a3f3b7b ML |
29 | "use", "whatis", "when", "where", "whereis", "which", |
30 | "INT", "REAL", "NAME", "STRING", | |
31 | "LFORMER", "RFORMER", "#^", "->" | |
32 | }; | |
33 | ||
34 | /* | |
35 | * The keyword table is a traditional hash table with collisions | |
36 | * resolved by chaining. | |
37 | */ | |
38 | ||
39 | #define HASHTABLESIZE 503 | |
40 | ||
41 | typedef struct Keyword { | |
42 | Name name; | |
43 | Token toknum : 16; | |
44 | Boolean isalias : 16; | |
45 | struct Keyword *chain; | |
46 | } *Keyword; | |
47 | ||
48 | typedef unsigned int Hashvalue; | |
49 | ||
50 | private Keyword hashtab[HASHTABLESIZE]; | |
51 | ||
52 | #define hash(n) ((((unsigned) n) >> 2) mod HASHTABLESIZE) | |
53 | ||
54 | /* | |
55 | * Enter all the reserved words into the keyword table. | |
56 | */ | |
57 | ||
58 | public enterkeywords() | |
59 | { | |
60 | register Integer i; | |
61 | ||
62 | for (i = ALIAS; i <= WHICH; i++) { | |
63 | keyword(reserved[ord(i) - ord(ALIAS)], i, false); | |
64 | } | |
65 | keyword("set", ASSIGN, false); | |
2fd0f574 SL |
66 | keyword("c", CONT, true); |
67 | keyword("d", DELETE, true); | |
68 | keyword("h", HELP, true); | |
69 | keyword("e", EDIT, true); | |
70 | keyword("l", LIST, true); | |
71 | keyword("n", NEXT, true); | |
72 | keyword("p", PRINT, true); | |
73 | keyword("q", QUIT, true); | |
74 | keyword("r", RUN, true); | |
75 | keyword("s", STEP, true); | |
76 | keyword("st", STOP, true); | |
77 | keyword("j", STATUS, true); | |
78 | keyword("t", WHERE, true); | |
9a3f3b7b ML |
79 | } |
80 | ||
81 | /* | |
82 | * Deallocate the keyword table. | |
83 | */ | |
84 | ||
85 | public keywords_free() | |
86 | { | |
87 | register Integer i; | |
88 | register Keyword k, nextk; | |
89 | ||
90 | for (i = 0; i < HASHTABLESIZE; i++) { | |
91 | k = hashtab[i]; | |
92 | while (k != nil) { | |
93 | nextk = k->chain; | |
94 | dispose(k); | |
95 | k = nextk; | |
96 | } | |
97 | hashtab[i] = nil; | |
98 | } | |
99 | } | |
100 | ||
101 | /* | |
102 | * Enter a keyword into the name table. It is assumed to not be there already. | |
103 | * The string is assumed to be statically allocated. | |
104 | */ | |
105 | ||
106 | private keyword(s, t, isalias) | |
107 | String s; | |
108 | Token t; | |
109 | Boolean isalias; | |
110 | { | |
111 | register Hashvalue h; | |
112 | register Keyword k; | |
113 | Name n; | |
114 | ||
115 | n = identname(s, true); | |
116 | h = hash(n); | |
117 | k = new(Keyword); | |
118 | k->name = n; | |
119 | k->toknum = t; | |
120 | k->isalias = isalias; | |
121 | k->chain = hashtab[h]; | |
122 | hashtab[h] = k; | |
123 | } | |
124 | ||
125 | /* | |
126 | * Return the string associated with a token corresponding to a keyword. | |
127 | */ | |
128 | ||
129 | public String keywdstring(t) | |
130 | Token t; | |
131 | { | |
132 | return reserved[ord(t) - ord(ALIAS)]; | |
133 | } | |
134 | ||
135 | /* | |
2fd0f574 | 136 | * Find the keyword associated with the given string. |
9a3f3b7b ML |
137 | */ |
138 | ||
2fd0f574 | 139 | private Keyword kwlookup (n) |
9a3f3b7b ML |
140 | Name n; |
141 | { | |
2fd0f574 | 142 | Hashvalue h; |
9a3f3b7b | 143 | register Keyword k; |
9a3f3b7b ML |
144 | |
145 | h = hash(n); | |
146 | k = hashtab[h]; | |
147 | while (k != nil and k->name != n) { | |
148 | k = k->chain; | |
149 | } | |
2fd0f574 SL |
150 | return k; |
151 | } | |
152 | ||
153 | /* | |
154 | * Return the token associated with a given keyword string. | |
155 | * We assume that tokens cannot legitimately be nil (0). | |
156 | */ | |
157 | ||
158 | public Token findkeyword(n) | |
159 | Name n; | |
160 | { | |
161 | Keyword k; | |
162 | Token t; | |
163 | ||
164 | k = kwlookup(n); | |
9a3f3b7b ML |
165 | if (k == nil) { |
166 | t = nil; | |
167 | } else { | |
168 | t = k->toknum; | |
169 | } | |
170 | return t; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Create an alias. | |
175 | */ | |
176 | ||
177 | public enter_alias(newcmd, oldcmd) | |
178 | Name newcmd; | |
179 | Name oldcmd; | |
180 | { | |
181 | Token t; | |
2fd0f574 | 182 | Keyword k; |
9a3f3b7b ML |
183 | |
184 | t = findkeyword(oldcmd); | |
185 | if (t == nil) { | |
186 | error("\"%s\" is not a command", ident(oldcmd)); | |
187 | } else { | |
2fd0f574 SL |
188 | k = kwlookup(newcmd); |
189 | if (k == nil) { | |
190 | keyword(ident(newcmd), t, true); | |
191 | } else { | |
192 | k->toknum = t; | |
193 | } | |
9a3f3b7b ML |
194 | } |
195 | } | |
196 | ||
197 | /* | |
198 | * Print out an alias. | |
199 | */ | |
200 | ||
201 | public print_alias(cmd) | |
202 | Name cmd; | |
203 | { | |
204 | register Keyword k; | |
205 | register Integer i; | |
206 | Token t; | |
207 | ||
208 | if (cmd == nil) { | |
209 | for (i = 0; i < HASHTABLESIZE; i++) { | |
210 | for (k = hashtab[i]; k != nil; k = k->chain) { | |
211 | if (k->isalias) { | |
212 | if (isredirected()) { | |
213 | printf("alias "); | |
214 | } | |
215 | printf("%s\t%s\n", ident(k->name), keywdstring(k->toknum)); | |
216 | } | |
217 | } | |
218 | } | |
219 | } else { | |
220 | t = findkeyword(cmd); | |
221 | if (t == nil) { | |
222 | printf("\n"); | |
223 | } else { | |
224 | printf("%s\n", keywdstring(t)); | |
225 | } | |
226 | } | |
227 | } |