8
|
1 // Symbol table classes. -*- C++ -*- |
|
2 /* |
|
3 |
276
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
8
|
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 |
383
|
24 #if !defined (octave_symtab_h) |
|
25 #define octave_symtab_h 1 |
8
|
26 |
453
|
27 #if defined (__GNUG__) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
8
|
31 #include <stdlib.h> |
|
32 #include <string.h> |
|
33 #include <iostream.h> |
|
34 #include <assert.h> |
|
35 |
220
|
36 #include "SLStack.h" |
|
37 |
8
|
38 #ifndef SV_FUNCTION_TYPEDEFS |
|
39 #define SV_FUNCTION_TYPEDEFS 1 |
|
40 |
|
41 typedef int (*sv_Function)(void); |
|
42 |
|
43 #endif |
|
44 |
|
45 #define HASH_TABLE_SIZE 1024 /* Must be multiple of 2 */ |
|
46 #define HASH_MASK (HASH_TABLE_SIZE - 1) |
|
47 |
|
48 class tree; |
489
|
49 class tree_fvc; |
8
|
50 class tree_builtin; |
|
51 class tree_constant; |
|
52 class tree_function; |
|
53 |
|
54 class symbol_def; |
|
55 class symbol_record; |
195
|
56 class symbol_record_info; |
8
|
57 class symbol_table; |
|
58 |
|
59 /* |
|
60 * Variables or functions. |
|
61 */ |
|
62 class symbol_def |
|
63 { |
|
64 friend class symbol_record; |
195
|
65 friend class symbol_record_info; |
8
|
66 |
|
67 public: |
|
68 |
|
69 symbol_def (void); |
|
70 symbol_def (tree_constant *t); |
|
71 symbol_def (tree_builtin *t); |
|
72 symbol_def (tree_function *t); |
|
73 |
|
74 ~symbol_def (void); |
|
75 |
195
|
76 int is_variable (void) const; |
|
77 int is_function (void) const; |
|
78 int is_user_variable (void) const; |
|
79 int is_user_function (void) const; |
|
80 int is_builtin_variable (void) const; |
|
81 int is_builtin_function (void) const; |
|
82 |
8
|
83 void define (tree_constant *t); |
|
84 void define (tree_builtin *t); |
|
85 void define (tree_function *t); |
|
86 |
195
|
87 void protect (void); |
|
88 void unprotect (void); |
|
89 void make_eternal (void); |
|
90 |
489
|
91 tree_fvc *def (void) const; |
164
|
92 char *help (void) const; |
|
93 void document (const char *h); |
8
|
94 |
276
|
95 int save (ostream& os, int mark_as_global, int precision); |
8
|
96 |
195
|
97 enum TYPE |
|
98 { |
|
99 UNKNOWN = 0, |
|
100 USER_FUNCTION = 1, |
|
101 USER_VARIABLE = 2, |
|
102 BUILTIN_FUNCTION = 4, |
|
103 BUILTIN_VARIABLE = 8 |
|
104 }; |
|
105 |
|
106 friend maybe_delete (symbol_def *def); |
|
107 |
8
|
108 private: |
|
109 |
195
|
110 unsigned type : 4; |
|
111 unsigned eternal : 1; |
|
112 unsigned read_only : 1; |
|
113 |
8
|
114 char *help_string; |
489
|
115 tree_fvc *definition; |
195
|
116 symbol_def *next_elem; |
8
|
117 int count; |
195
|
118 |
|
119 void init_state (void); |
8
|
120 |
|
121 symbol_def (const symbol_def& sd); |
|
122 symbol_def& operator = (const symbol_def& sd); |
|
123 }; |
|
124 |
|
125 /* |
|
126 * Individual records in a symbol table. |
|
127 */ |
|
128 class |
|
129 symbol_record |
|
130 { |
195
|
131 friend class symbol_record_info; |
8
|
132 |
|
133 public: |
|
134 symbol_record (void); |
195
|
135 symbol_record (const char *n, symbol_record *nxt = (symbol_record *) NULL); |
8
|
136 |
|
137 ~symbol_record (void); |
|
138 |
164
|
139 char *name (void) const; |
|
140 char *help (void) const; |
489
|
141 tree_fvc *def (void) const; |
8
|
142 |
195
|
143 void rename (const char *n); |
|
144 |
164
|
145 int is_function (void) const; |
195
|
146 int is_user_function (void) const; |
|
147 int is_builtin_function (void) const; |
164
|
148 int is_variable (void) const; |
195
|
149 int is_user_variable (void) const; |
|
150 int is_builtin_variable (void) const; |
|
151 |
|
152 unsigned type (void) const; |
8
|
153 |
164
|
154 int is_defined (void) const; |
195
|
155 int is_read_only (void) const; |
|
156 int is_eternal (void) const; |
8
|
157 |
|
158 void protect (void); |
|
159 void unprotect (void); |
|
160 void make_eternal (void); |
|
161 |
195
|
162 void set_sv_function (sv_Function f); |
|
163 |
|
164 int define (tree_constant *t); |
|
165 int define (tree_builtin *t); |
|
166 int define (tree_function *t); |
|
167 int define_as_fcn (tree_constant *t); |
|
168 int define_builtin_var (tree_constant *t); |
|
169 |
|
170 void document (const char *h); |
|
171 |
276
|
172 int save (ostream& os, int mark_as_global = 0, int precision = 17); |
8
|
173 |
195
|
174 int clear (void); |
|
175 |
|
176 void alias (symbol_record *s, int force = 0); |
8
|
177 |
|
178 void mark_as_formal_parameter (void); |
164
|
179 int is_formal_parameter (void) const; |
8
|
180 |
195
|
181 void mark_as_linked_to_global (void); |
|
182 int is_linked_to_global (void) const; |
8
|
183 |
164
|
184 symbol_record *next (void) const; |
8
|
185 |
195
|
186 void chain (symbol_record *s); |
|
187 |
220
|
188 void push_context (void); |
|
189 void pop_context (void); |
|
190 |
8
|
191 private: |
|
192 |
195
|
193 unsigned formal_param : 1; |
|
194 unsigned linked_to_global : 1; |
|
195 |
8
|
196 char *nm; |
|
197 sv_Function sv_fcn; |
195
|
198 symbol_def *definition; |
8
|
199 symbol_record *next_elem; |
395
|
200 |
|
201 // This should maybe be one stack with a structure containing all the |
|
202 // items we need to save for recursive calls... |
220
|
203 SLStack <symbol_def *> context; |
395
|
204 SLStack <unsigned> global_link_context; |
8
|
205 |
195
|
206 void init_state (void); |
|
207 |
|
208 int read_only_error (void); |
|
209 |
|
210 void push_def (symbol_def *sd); |
|
211 symbol_def *pop_def (void); |
|
212 |
8
|
213 symbol_record& operator = (const symbol_record& s); |
|
214 }; |
|
215 |
|
216 /* |
195
|
217 * A structure for handling verbose information about a symbol_record. |
|
218 */ |
|
219 |
|
220 class |
|
221 symbol_record_info |
|
222 { |
|
223 public: |
|
224 |
|
225 symbol_record_info (void); |
|
226 symbol_record_info (const symbol_record& s); |
|
227 |
|
228 symbol_record_info (const symbol_record_info& s); |
|
229 |
|
230 ~symbol_record_info (void); |
|
231 |
|
232 symbol_record_info& operator = (const symbol_record_info& s); |
|
233 |
|
234 int is_defined (void) const; |
|
235 int is_read_only (void) const; |
|
236 int is_eternal (void) const; |
|
237 int hides_fcn (void) const; |
|
238 int hides_builtin (void) const; |
|
239 char *type_as_string (void) const; |
|
240 int is_function (void) const; |
|
241 int rows (void) const; |
|
242 int columns (void) const; |
|
243 char *name (void) const; |
|
244 |
|
245 enum HIDES |
|
246 { |
|
247 SR_INFO_NONE = 0, |
|
248 SR_INFO_USER_FUNCTION = 1, |
|
249 SR_INFO_BUILTIN_FUNCTION = 2 |
|
250 }; |
|
251 |
|
252 enum CONST_TYPE |
|
253 { |
|
254 SR_INFO_UNKNOWN = 0, |
|
255 SR_INFO_SCALAR = 1, |
|
256 SR_INFO_COMPLEX_SCALAR = 2, |
|
257 SR_INFO_MATRIX = 4, |
|
258 SR_INFO_COMPLEX_MATRIX = 8, |
|
259 SR_INFO_RANGE = 16, |
|
260 SR_INFO_STRING = 32 |
|
261 }; |
|
262 |
|
263 private: |
|
264 |
|
265 void init_state (void); |
|
266 |
|
267 unsigned type : 4; |
|
268 unsigned const_type : 6; |
|
269 unsigned hides : 2; |
|
270 unsigned eternal : 1; |
|
271 unsigned read_only : 1; |
|
272 int nr; |
|
273 int nc; |
|
274 char *nm; |
|
275 |
|
276 int initialized; |
|
277 }; |
|
278 |
|
279 /* |
8
|
280 * A symbol table. |
|
281 */ |
200
|
282 |
|
283 #define SYMTAB_LOCAL_SCOPE 1 |
|
284 #define SYMTAB_GLOBAL_SCOPE 2 |
|
285 |
|
286 #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE) |
|
287 |
|
288 #define SYMTAB_ALL_TYPES (symbol_def::USER_FUNCTION \ |
|
289 | symbol_def::USER_VARIABLE \ |
|
290 | symbol_def::BUILTIN_FUNCTION \ |
|
291 | symbol_def::BUILTIN_VARIABLE) |
|
292 |
8
|
293 class |
|
294 symbol_table |
|
295 { |
|
296 public: |
|
297 |
|
298 symbol_table (void); |
|
299 |
164
|
300 symbol_record *lookup (const char *nm, int insert = 0, int warn = 0); |
8
|
301 |
195
|
302 void clear (int clear_user_functions = 1); |
|
303 int clear (const char *nm, int clear_user_functions = 1); |
8
|
304 |
276
|
305 int save (ostream& os, int mark_as_global = 0, int precision = 17); |
|
306 int save (ostream& os, const char *name, int mark_as_global = 0, |
|
307 int precicion = 17); |
8
|
308 |
164
|
309 int size (void) const; |
8
|
310 |
195
|
311 symbol_record_info *long_list (int& count, int sort = 0, |
|
312 unsigned type = SYMTAB_ALL_TYPES, |
|
313 unsigned scope = SYMTAB_ALL_SCOPES) const; |
|
314 |
|
315 char **list (int& count, int sort = 0, |
|
316 unsigned type = SYMTAB_ALL_TYPES, |
|
317 unsigned scope = SYMTAB_ALL_SCOPES) const; |
8
|
318 |
220
|
319 void push_context (void); |
|
320 void pop_context (void); |
|
321 |
8
|
322 private: |
|
323 |
|
324 unsigned int hash (const char *s); |
|
325 |
|
326 symbol_record table[HASH_TABLE_SIZE]; |
|
327 }; |
|
328 |
|
329 #endif |
|
330 |
|
331 /* |
|
332 ;;; Local Variables: *** |
|
333 ;;; mode: C++ *** |
|
334 ;;; page-delimiter: "^/\\*" *** |
|
335 ;;; End: *** |
|
336 */ |