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