-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsymtab.c
278 lines (224 loc) · 10.2 KB
/
symtab.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/****************************************************************/
/* */
/* S Y M B O L T A B L E */
/* */
/* Symbol table routines. */
/* */
/* FILE: symtab.c */
/* */
/* MODULE: symbol table */
/* */
/* Copyright (c) 1991 by Ronald Mak */
/* For instructional purposes only. No warranties. */
/* */
/****************************************************************/
#include <stdio.h>
#include "common.h"
#include "error.h"
#include "symtab.h"
/*--------------------------------------------------------------*/
/* Externals */
/*--------------------------------------------------------------*/
extern int level;
/*--------------------------------------------------------------*/
/* Globals */
/*--------------------------------------------------------------*/
SYMTAB_NODE_PTR symtab_display[MAX_NESTING_LEVEL];
TYPE_STRUCT_PTR integer_typep, real_typep, /* predefined types */
boolean_typep, char_typep;
TYPE_STRUCT dummy_type = { /* for erroneous type definitions */
NO_FORM, /* form */
0, /* size */
NULL /* type_idp */
};
/*--------------------------------------------------------------*/
/* search_symtab Search for a name in the symbol table. */
/* Return a pointer of the entry if found, */
/* or NULL if not. */
/*--------------------------------------------------------------*/
SYMTAB_NODE_PTR
search_symtab(name, np)
char *name; /* name to search for */
SYMTAB_NODE_PTR np; /* ptr to symtab root */
{
int cmp;
/*
-- Loop to check each node. Return if the node matches,
-- else continue search down the left or right subtree.
*/
while (np != NULL) {
cmp = strcmp(name, np->name);
if (cmp == 0) return(np); /* found */
np = cmp < 0 ? np->left : np->right; /* continue search */
}
return(NULL); /* not found */
}
/*--------------------------------------------------------------*/
/* search_symtab_display Search all the symbol tables in the */
/* symbol table display for a name. */
/* Return a pointer to the entry if */
/* found, or NULL if not. */
/*--------------------------------------------------------------*/
SYMTAB_NODE_PTR
search_symtab_display(name)
char *name; /* name to search for */
{
short i;
SYMTAB_NODE_PTR np; /* ptr to symtab node */
for (i = level; i >= 0; --i) {
np = search_symtab(name, symtab_display[i]);
if (np != NULL) return(np);
}
return(NULL);
}
/*--------------------------------------------------------------*/
/* enter_symtab Enter a name into the symbol table, */
/* and return a pointer to the new entry. */
/*--------------------------------------------------------------*/
SYMTAB_NODE_PTR
enter_symtab(name, npp)
char *name; /* name to enter */
SYMTAB_NODE_PTR *npp; /* ptr to ptr to symtab root */
{
int cmp; /* result of strcmp */
SYMTAB_NODE_PTR new_nodep; /* ptr to new entry */
SYMTAB_NODE_PTR np; /* ptr to node to test */
/*
-- Create the new node for the name.
*/
new_nodep = alloc_struct(SYMTAB_NODE);
new_nodep->name = alloc_bytes(strlen(name) + 1);
strcpy(new_nodep->name, name);
new_nodep->left = new_nodep->right = new_nodep->next = NULL;
new_nodep->info = NULL;
new_nodep->defn.key = UNDEFINED;
new_nodep->typep = NULL;
new_nodep->level = level;
new_nodep->label_index = 0;
/*
-- Loop to search for the insertion point.
*/
while ((np = *npp) != NULL) {
cmp = strcmp(name, np->name);
npp = cmp < 0 ? &(np->left) : &(np->right);
}
*npp = new_nodep; /* replace */
return(new_nodep);
}
/*--------------------------------------------------------------*/
/* init_symtab Initialize the symbol table with */
/* predefined identifiers and types, */
/* and routines. */
/*--------------------------------------------------------------*/
init_symtab()
{
SYMTAB_NODE_PTR integer_idp, real_idp, boolean_idp, char_idp,
false_idp, true_idp;
/*
-- Initialize the level-0 symbol table.
*/
symtab_display[0] = NULL;
enter_name_local_symtab(integer_idp, "integer");
enter_name_local_symtab(real_idp, "real");
enter_name_local_symtab(boolean_idp, "boolean");
enter_name_local_symtab(char_idp, "char");
enter_name_local_symtab(false_idp, "false");
enter_name_local_symtab(true_idp, "true");
integer_typep = alloc_struct(TYPE_STRUCT);
real_typep = alloc_struct(TYPE_STRUCT);
boolean_typep = alloc_struct(TYPE_STRUCT);
char_typep = alloc_struct(TYPE_STRUCT);
integer_idp->defn.key = TYPE_DEFN;
integer_idp->typep = integer_typep;
integer_typep->form = SCALAR_FORM;
integer_typep->size = sizeof(int);
integer_typep->type_idp = integer_idp;
real_idp->defn.key = TYPE_DEFN;
real_idp->typep = real_typep;
real_typep->form = SCALAR_FORM;
real_typep->size = sizeof(float);
real_typep->type_idp = real_idp;
boolean_idp->defn.key = TYPE_DEFN;
boolean_idp->typep = boolean_typep;
boolean_typep->form = ENUM_FORM;
boolean_typep->size = sizeof(int);
boolean_typep->type_idp = boolean_idp;
boolean_typep->info.enumeration.max = 1;
boolean_idp->typep->info.enumeration.const_idp = false_idp;
false_idp->defn.key = CONST_DEFN;
false_idp->defn.info.constant.value.integer = 0;
false_idp->typep = boolean_typep;
false_idp->next = true_idp;
true_idp->defn.key = CONST_DEFN;
true_idp->defn.info.constant.value.integer = 1;
true_idp->typep = boolean_typep;
char_idp->defn.key = TYPE_DEFN;
char_idp->typep = char_typep;
char_typep->form = SCALAR_FORM;
char_typep->size = sizeof(char);
char_typep->type_idp = char_idp;
enter_standard_routine("read", READ, PROC_DEFN);
enter_standard_routine("readln", READLN, PROC_DEFN);
enter_standard_routine("write", WRITE, PROC_DEFN);
enter_standard_routine("writeln", WRITELN, PROC_DEFN);
enter_standard_routine("abs", ABS, FUNC_DEFN);
enter_standard_routine("arctan", ARCTAN, FUNC_DEFN);
enter_standard_routine("chr", CHR, FUNC_DEFN);
enter_standard_routine("cos", COS, FUNC_DEFN);
enter_standard_routine("eof", EOFF, FUNC_DEFN);
enter_standard_routine("eoln", EOLN, FUNC_DEFN);
enter_standard_routine("exp", EXP, FUNC_DEFN);
enter_standard_routine("ln", LN, FUNC_DEFN);
enter_standard_routine("odd", ODD, FUNC_DEFN);
enter_standard_routine("ord", ORD, FUNC_DEFN);
enter_standard_routine("pred", PRED, FUNC_DEFN);
enter_standard_routine("round", ROUND, FUNC_DEFN);
enter_standard_routine("sin", SIN, FUNC_DEFN);
enter_standard_routine("sqr", SQR, FUNC_DEFN);
enter_standard_routine("sqrt", SQRT, FUNC_DEFN);
enter_standard_routine("succ", SUCC, FUNC_DEFN);
enter_standard_routine("trunc", TRUNC, FUNC_DEFN);
}
/*--------------------------------------------------------------*/
/* enter_standard_routine Enter a standard procedure or */
/* function identifier into the */
/* symbol table. */
/*--------------------------------------------------------------*/
enter_standard_routine(name, routine_key, defn_key)
char *name; /* name string */
ROUTINE_KEY routine_key;
DEFN_KEY defn_key;
{
SYMTAB_NODE_PTR rtn_idp = enter_name_local_symtab(rtn_idp, name);
rtn_idp->defn.key = defn_key;
rtn_idp->defn.info.routine.key = routine_key;
rtn_idp->defn.info.routine.parms = NULL;
rtn_idp->defn.info.routine.local_symtab = NULL;
rtn_idp->typep = NULL;
}
/*--------------------------------------------------------------*/
/* enter_scope Enter a new nesting level by creating */
/* a new scope. Push the given symbol */
/* table onto the display stack. */
/*--------------------------------------------------------------*/
enter_scope(symtab_root)
SYMTAB_NODE_PTR symtab_root;
{
if (++level >= MAX_NESTING_LEVEL) {
error(NESTING_TOO_DEEP);
exit(-NESTING_TOO_DEEP);
}
symtab_display[level] = symtab_root;
}
/*--------------------------------------------------------------*/
/* exit_scope Exit the current nesting level by */
/* closing the current scope. Pop the */
/* current symbol table off the display */
/* stack and return a pointer to it. */
/*--------------------------------------------------------------*/
SYMTAB_NODE_PTR
exit_scope()
{
SYMTAB_NODE_PTR symtab_root = symtab_display[level--];
return(symtab_root);
}