8
|
1 // Symbol table classes. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (_symtab_h) |
|
25 #define _symtab_h 1 |
|
26 |
|
27 #ifdef __GNUG__ |
|
28 #pragma interface |
|
29 #endif |
|
30 |
|
31 #include <stdlib.h> |
|
32 #include <string.h> |
|
33 #include <iostream.h> |
|
34 #include <assert.h> |
|
35 |
|
36 #ifndef SV_FUNCTION_TYPEDEFS |
|
37 #define SV_FUNCTION_TYPEDEFS 1 |
|
38 |
|
39 typedef int (*sv_Function)(void); |
|
40 |
|
41 #endif |
|
42 |
|
43 #define HASH_TABLE_SIZE 1024 /* Must be multiple of 2 */ |
|
44 #define HASH_MASK (HASH_TABLE_SIZE - 1) |
|
45 |
|
46 class tree; |
|
47 class tree_builtin; |
|
48 class tree_constant; |
|
49 class tree_function; |
|
50 |
|
51 class symbol_def; |
|
52 class symbol_record; |
195
|
53 class symbol_record_info; |
8
|
54 class symbol_table; |
|
55 |
|
56 /* |
|
57 * Variables or functions. |
|
58 */ |
|
59 class symbol_def |
|
60 { |
|
61 friend class symbol_record; |
195
|
62 friend class symbol_record_info; |
8
|
63 |
|
64 public: |
|
65 |
|
66 symbol_def (void); |
|
67 symbol_def (tree_constant *t); |
|
68 symbol_def (tree_builtin *t); |
|
69 symbol_def (tree_function *t); |
|
70 |
|
71 ~symbol_def (void); |
|
72 |
195
|
73 int is_variable (void) const; |
|
74 int is_function (void) const; |
|
75 int is_user_variable (void) const; |
|
76 int is_user_function (void) const; |
|
77 int is_builtin_variable (void) const; |
|
78 int is_builtin_function (void) const; |
|
79 |
8
|
80 void define (tree_constant *t); |
|
81 void define (tree_builtin *t); |
|
82 void define (tree_function *t); |
|
83 |
195
|
84 void protect (void); |
|
85 void unprotect (void); |
|
86 void make_eternal (void); |
|
87 |
164
|
88 tree *def (void) const; |
|
89 char *help (void) const; |
|
90 void document (const char *h); |
8
|
91 |
|
92 int save (ostream& os, int mark_as_global); |
|
93 |
195
|
94 enum TYPE |
|
95 { |
|
96 UNKNOWN = 0, |
|
97 USER_FUNCTION = 1, |
|
98 USER_VARIABLE = 2, |
|
99 BUILTIN_FUNCTION = 4, |
|
100 BUILTIN_VARIABLE = 8 |
|
101 }; |
|
102 |
|
103 friend maybe_delete (symbol_def *def); |
|
104 |
8
|
105 private: |
|
106 |
195
|
107 unsigned type : 4; |
|
108 unsigned eternal : 1; |
|
109 unsigned read_only : 1; |
|
110 |
8
|
111 char *help_string; |
|
112 tree *definition; |
195
|
113 symbol_def *next_elem; |
8
|
114 int count; |
195
|
115 |
|
116 void init_state (void); |
8
|
117 |
|
118 symbol_def (const symbol_def& sd); |
|
119 symbol_def& operator = (const symbol_def& sd); |
|
120 }; |
|
121 |
|
122 /* |
|
123 * Individual records in a symbol table. |
|
124 */ |
|
125 class |
|
126 symbol_record |
|
127 { |
195
|
128 friend class symbol_record_info; |
8
|
129 |
|
130 public: |
|
131 symbol_record (void); |
195
|
132 symbol_record (const char *n, symbol_record *nxt = (symbol_record *) NULL); |
8
|
133 |
|
134 ~symbol_record (void); |
|
135 |
164
|
136 char *name (void) const; |
|
137 char *help (void) const; |
|
138 tree *def (void) const; |
8
|
139 |
195
|
140 void rename (const char *n); |
|
141 |
164
|
142 int is_function (void) const; |
195
|
143 int is_user_function (void) const; |
|
144 int is_builtin_function (void) const; |
164
|
145 int is_variable (void) const; |
195
|
146 int is_user_variable (void) const; |
|
147 int is_builtin_variable (void) const; |
|
148 |
|
149 unsigned type (void) const; |
8
|
150 |
164
|
151 int is_defined (void) const; |
195
|
152 int is_read_only (void) const; |
|
153 int is_eternal (void) const; |
8
|
154 |
|
155 void protect (void); |
|
156 void unprotect (void); |
|
157 void make_eternal (void); |
|
158 |
195
|
159 void set_sv_function (sv_Function f); |
|
160 |
|
161 int define (tree_constant *t); |
|
162 int define (tree_builtin *t); |
|
163 int define (tree_function *t); |
|
164 int define_as_fcn (tree_constant *t); |
|
165 int define_builtin_var (tree_constant *t); |
|
166 |
|
167 void document (const char *h); |
|
168 |
8
|
169 int save (ostream& os, int mark_as_global = 0); |
|
170 |
195
|
171 int clear (void); |
|
172 |
|
173 void alias (symbol_record *s, int force = 0); |
8
|
174 |
|
175 void mark_as_formal_parameter (void); |
164
|
176 int is_formal_parameter (void) const; |
8
|
177 |
195
|
178 void mark_as_linked_to_global (void); |
|
179 int is_linked_to_global (void) const; |
8
|
180 |
164
|
181 symbol_record *next (void) const; |
8
|
182 |
195
|
183 void chain (symbol_record *s); |
|
184 |
8
|
185 private: |
|
186 |
195
|
187 unsigned formal_param : 1; |
|
188 unsigned linked_to_global : 1; |
|
189 |
8
|
190 char *nm; |
|
191 sv_Function sv_fcn; |
195
|
192 symbol_def *definition; |
8
|
193 symbol_record *next_elem; |
|
194 |
195
|
195 void init_state (void); |
|
196 |
|
197 int read_only_error (void); |
|
198 |
|
199 void push_def (symbol_def *sd); |
|
200 symbol_def *pop_def (void); |
|
201 |
8
|
202 symbol_record& operator = (const symbol_record& s); |
|
203 }; |
|
204 |
|
205 /* |
195
|
206 * A structure for handling verbose information about a symbol_record. |
|
207 */ |
|
208 |
|
209 class |
|
210 symbol_record_info |
|
211 { |
|
212 public: |
|
213 |
|
214 symbol_record_info (void); |
|
215 symbol_record_info (const symbol_record& s); |
|
216 |
|
217 symbol_record_info (const symbol_record_info& s); |
|
218 |
|
219 ~symbol_record_info (void); |
|
220 |
|
221 symbol_record_info& operator = (const symbol_record_info& s); |
|
222 |
|
223 int is_defined (void) const; |
|
224 int is_read_only (void) const; |
|
225 int is_eternal (void) const; |
|
226 int hides_fcn (void) const; |
|
227 int hides_builtin (void) const; |
|
228 char *type_as_string (void) const; |
|
229 int is_function (void) const; |
|
230 int rows (void) const; |
|
231 int columns (void) const; |
|
232 char *name (void) const; |
|
233 |
|
234 enum HIDES |
|
235 { |
|
236 SR_INFO_NONE = 0, |
|
237 SR_INFO_USER_FUNCTION = 1, |
|
238 SR_INFO_BUILTIN_FUNCTION = 2 |
|
239 }; |
|
240 |
|
241 enum CONST_TYPE |
|
242 { |
|
243 SR_INFO_UNKNOWN = 0, |
|
244 SR_INFO_SCALAR = 1, |
|
245 SR_INFO_COMPLEX_SCALAR = 2, |
|
246 SR_INFO_MATRIX = 4, |
|
247 SR_INFO_COMPLEX_MATRIX = 8, |
|
248 SR_INFO_RANGE = 16, |
|
249 SR_INFO_STRING = 32 |
|
250 }; |
|
251 |
|
252 private: |
|
253 |
|
254 void init_state (void); |
|
255 |
|
256 unsigned type : 4; |
|
257 unsigned const_type : 6; |
|
258 unsigned hides : 2; |
|
259 unsigned eternal : 1; |
|
260 unsigned read_only : 1; |
|
261 int nr; |
|
262 int nc; |
|
263 char *nm; |
|
264 |
|
265 int initialized; |
|
266 }; |
|
267 |
|
268 /* |
8
|
269 * A symbol table. |
|
270 */ |
|
271 class |
|
272 symbol_table |
|
273 { |
|
274 public: |
|
275 |
|
276 symbol_table (void); |
|
277 |
164
|
278 symbol_record *lookup (const char *nm, int insert = 0, int warn = 0); |
8
|
279 |
195
|
280 void clear (int clear_user_functions = 1); |
|
281 int clear (const char *nm, int clear_user_functions = 1); |
8
|
282 |
|
283 int save (ostream& os, int mark_as_global = 0); |
164
|
284 int save (ostream& os, const char *name, int mark_as_global = 0); |
8
|
285 |
164
|
286 int size (void) const; |
8
|
287 |
195
|
288 #define SYMTAB_LOCAL_SCOPE 1 |
|
289 #define SYMTAB_GLOBAL_SCOPE 2 |
8
|
290 |
195
|
291 #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE) |
8
|
292 |
195
|
293 #define SYMTAB_ALL_TYPES (symbol_def::USER_FUNCTION \ |
|
294 | symbol_def::USER_VARIABLE \ |
|
295 | symbol_def::BUILTIN_FUNCTION \ |
|
296 | symbol_def::BUILTIN_VARIABLE) |
8
|
297 |
195
|
298 symbol_record_info *long_list (int& count, int sort = 0, |
|
299 unsigned type = SYMTAB_ALL_TYPES, |
|
300 unsigned scope = SYMTAB_ALL_SCOPES) const; |
|
301 |
|
302 char **list (int& count, int sort = 0, |
|
303 unsigned type = SYMTAB_ALL_TYPES, |
|
304 unsigned scope = SYMTAB_ALL_SCOPES) const; |
8
|
305 |
|
306 private: |
|
307 |
|
308 unsigned int hash (const char *s); |
|
309 |
|
310 symbol_record table[HASH_TABLE_SIZE]; |
|
311 }; |
|
312 |
|
313 #endif |
|
314 |
|
315 /* |
|
316 ;;; Local Variables: *** |
|
317 ;;; mode: C++ *** |
|
318 ;;; page-delimiter: "^/\\*" *** |
|
319 ;;; End: *** |
|
320 */ |