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