-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsymtab.h
242 lines (204 loc) · 9.88 KB
/
symtab.h
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
/****************************************************************/
/* */
/* S Y M B O L T A B L E (Header) */
/* */
/* FILE: symtab.h */
/* */
/* MODULE: symbol table */
/* */
/* Copyright (c) 1991 by Ronald Mak */
/* For instructional purposes only. No warranties. */
/* */
/****************************************************************/
#ifndef symtab_h
#define symtab_h
#include "common.h"
/*--------------------------------------------------------------*/
/* Value structure */
/*--------------------------------------------------------------*/
typedef union {
int integer;
float real;
char character;
char *stringp;
} VALUE;
/*--------------------------------------------------------------*/
/* Definition structure */
/*--------------------------------------------------------------*/
typedef enum {
UNDEFINED,
CONST_DEFN, TYPE_DEFN, VAR_DEFN, FIELD_DEFN,
VALPARM_DEFN, VARPARM_DEFN,
PROG_DEFN, PROC_DEFN, FUNC_DEFN,
} DEFN_KEY;
typedef enum {
DECLARED, FORWARD,
READ, READLN, WRITE, WRITELN,
ABS, ARCTAN, CHR, COS, EOFF, EOLN, EXP, LN, ODD, ORD,
PRED, ROUND, SIN, SQR, SQRT, SUCC, TRUNC,
} ROUTINE_KEY;
typedef struct {
DEFN_KEY key;
union {
struct {
VALUE value;
} constant;
struct {
ROUTINE_KEY key;
int parm_count;
int total_parm_size;
int total_local_size;
struct symtab_node *parms;
struct symtab_node *locals;
struct symtab_node *local_symtab;
char *code_segment;
} routine;
struct {
int offset;
struct symtab_node *record_idp;
} data;
} info;
} DEFN_STRUCT;
/*--------------------------------------------------------------*/
/* Type structure */
/*--------------------------------------------------------------*/
typedef enum {
NO_FORM,
SCALAR_FORM, ENUM_FORM, SUBRANGE_FORM,
ARRAY_FORM, RECORD_FORM,
} TYPE_FORM;
typedef struct type_struct {
TYPE_FORM form;
int size;
struct symtab_node *type_idp;
union {
struct {
struct symtab_node *const_idp;
int max;
} enumeration;
struct {
struct type_struct *range_typep;
int min, max;
} subrange;
struct {
struct type_struct *index_typep, *elmt_typep;
int min_index, max_index;
int elmt_count;
} array;
struct {
struct symtab_node *field_symtab;
} record;
} info;
} TYPE_STRUCT, *TYPE_STRUCT_PTR;
/*--------------------------------------------------------------*/
/* Symbol table node */
/*--------------------------------------------------------------*/
typedef struct symtab_node {
struct symtab_node *left, *right; /* ptrs to subtrees */
struct symtab_node *next; /* for chaining nodes */
char *name; /* name string */
char *info; /* ptr to generic info */
DEFN_STRUCT defn; /* definition struct */
TYPE_STRUCT_PTR typep; /* ptr to type struct */
int level; /* nesting level */
int label_index; /* index for code label */
} SYMTAB_NODE, *SYMTAB_NODE_PTR;
/*--------------------------------------------------------------*/
/* Functions */
/*--------------------------------------------------------------*/
SYMTAB_NODE_PTR search_symtab();
SYMTAB_NODE_PTR search_symtab_display();
SYMTAB_NODE_PTR enter_symtab();
SYMTAB_NODE_PTR exit_scope();
TYPE_STRUCT_PTR make_string_typep();
/****************************************/
/* */
/* Macros to search symbol tables */
/* */
/****************************************/
/*--------------------------------------------------------------*/
/* search_local_symtab Search the local symbol */
/* table for the current id */
/* name. Set a pointer to the */
/* entry if found, else to */
/* NULL. */
/*--------------------------------------------------------------*/
#define search_local_symtab(idp) \
idp = search_symtab(word_string, symtab_display[level])
/*--------------------------------------------------------------*/
/* search_this_symtab Search the given symbol */
/* table for the current id */
/* name. Set a pointer to the */
/* entry if found, else to */
/* NULL. */
/*--------------------------------------------------------------*/
#define search_this_symtab(idp, this_symtab) \
idp = search_symtab(word_string, this_symtab)
/*--------------------------------------------------------------*/
/* search_all_symtab Search the symbol table */
/* display for the current id */
/* name. Set a pointer to the */
/* entry if found, else to */
/* NULL. */
/*--------------------------------------------------------------*/
#define search_all_symtab(idp) \
idp = search_symtab_display(word_string)
/*--------------------------------------------------------------*/
/* enter_local_symtab Enter the current id name */
/* into the local symbol */
/* table, and set a pointer */
/* to the entry. */
/*--------------------------------------------------------------*/
#define enter_local_symtab(idp) \
idp = enter_symtab(word_string, &symtab_display[level])
/*--------------------------------------------------------------*/
/* enter_name_local_symtab Enter the given name into */
/* the local symbol table, and */
/* set a pointer to the entry. */
/*--------------------------------------------------------------*/
#define enter_name_local_symtab(idp, name) \
idp = enter_symtab(name, &symtab_display[level])
/*--------------------------------------------------------------*/
/* search_and_find_all_symtab Search the symbol table */
/* display for the current id */
/* name. If not found, ID */
/* UNDEFINED error, and enter */
/* into the local symbol table.*/
/* Set a pointer to the entry. */
/*--------------------------------------------------------------*/
#define search_and_find_all_symtab(idp) \
if ((idp = search_symtab_display(word_string)) == NULL) { \
error(UNDEFINED_IDENTIFIER); \
idp = enter_symtab(word_string, &symtab_display[level]);\
idp->defn.key = UNDEFINED; \
idp->typep = &dummy_type; \
}
/*--------------------------------------------------------------*/
/* search_and_enter_local_symtab Search the local symbol */
/* table for the current id */
/* name. Enter the name if */
/* it is not already in there, */
/* else ID REDEFINED error. */
/* Set a pointer to the entry. */
/*--------------------------------------------------------------*/
#define search_and_enter_local_symtab(idp) \
if ((idp = search_symtab(word_string, \
symtab_display[level])) == NULL) { \
idp = enter_symtab(word_string, &symtab_display[level]);\
} \
else error(REDEFINED_IDENTIFIER)
/*--------------------------------------------------------------*/
/* search_and_enter_this_symtab Search the given symbol */
/* table for the current id */
/* name. Enter the name if */
/* it is not already in there, */
/* else ID REDEFINED error. */
/* Set a pointer to the entry. */
/*--------------------------------------------------------------*/
#define search_and_enter_this_symtab(idp, this_symtab) \
if ((idp = search_symtab(word_string, \
this_symtab)) == NULL) { \
idp = enter_symtab(word_string, &this_symtab); \
} \
else error(REDEFINED_IDENTIFIER)
#endif