8
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
8
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
8
|
20 |
|
21 */ |
|
22 |
383
|
23 #if !defined (octave_symtab_h) |
|
24 #define octave_symtab_h 1 |
8
|
25 |
1297
|
26 #if defined (__GNUG__) |
|
27 #pragma interface |
|
28 #endif |
|
29 |
3011
|
30 #include <cassert> |
|
31 |
1755
|
32 #include <string> |
|
33 |
220
|
34 #include "SLStack.h" |
|
35 |
3011
|
36 #include "oct-alloc.h" |
1755
|
37 #include "str-vec.h" |
|
38 |
2975
|
39 #include "ov.h" |
|
40 |
2979
|
41 class octave_lvalue; |
8
|
42 |
1755
|
43 class string_vector; |
|
44 |
8
|
45 class symbol_record; |
|
46 class symbol_table; |
|
47 |
3011
|
48 // Individual records in a symbol table. |
8
|
49 |
3011
|
50 class |
|
51 symbol_record |
|
52 { |
|
53 public: |
8
|
54 |
3258
|
55 // If you add or delete an entry here, you'll also need to change |
|
56 // the with parameter in the declaration for symbol_type below... |
|
57 |
195
|
58 enum TYPE |
|
59 { |
|
60 UNKNOWN = 0, |
|
61 USER_FUNCTION = 1, |
|
62 USER_VARIABLE = 2, |
|
63 BUILTIN_FUNCTION = 4, |
530
|
64 TEXT_FUNCTION = 8, |
|
65 MAPPER_FUNCTION = 16, |
3258
|
66 BUILTIN_VARIABLE = 32, |
|
67 BUILTIN_CONSTANT = 64 |
195
|
68 }; |
|
69 |
8
|
70 private: |
|
71 |
3011
|
72 // Variables or functions. |
|
73 |
|
74 class symbol_def |
|
75 { |
|
76 public: |
|
77 |
|
78 symbol_def (const octave_value& val = octave_value (), |
|
79 unsigned int sym_type = 0) |
|
80 : symbol_type (sym_type), eternal (0), read_only (0), help_string (), |
|
81 definition (val), next_elem (0), count (1) { } |
|
82 |
|
83 ~symbol_def (void) { } |
|
84 |
3258
|
85 bool is_constant (void) const |
|
86 { return (symbol_type & symbol_record::BUILTIN_CONSTANT); } |
|
87 |
3011
|
88 bool is_variable (void) const |
|
89 { |
|
90 return (symbol_type & symbol_record::USER_VARIABLE |
|
91 || symbol_type & symbol_record::BUILTIN_VARIABLE); |
|
92 } |
|
93 |
|
94 bool is_function (void) const |
|
95 { |
|
96 return (symbol_type & symbol_record::USER_FUNCTION |
|
97 || symbol_type & symbol_record::BUILTIN_FUNCTION); |
|
98 } |
|
99 |
|
100 bool is_user_variable (void) const |
|
101 { return (symbol_type & symbol_record::USER_VARIABLE); } |
195
|
102 |
3011
|
103 bool is_text_function (void) const |
|
104 { return (symbol_type & symbol_record::TEXT_FUNCTION); } |
|
105 |
|
106 bool is_mapper_function (void) const |
|
107 { return (symbol_type & symbol_record::MAPPER_FUNCTION); } |
|
108 |
|
109 bool is_user_function (void) const |
|
110 { return (symbol_type & symbol_record::USER_FUNCTION); } |
|
111 |
3258
|
112 bool is_builtin_constant (void) const |
|
113 { return (symbol_type & symbol_record::BUILTIN_CONSTANT); } |
|
114 |
3011
|
115 bool is_builtin_variable (void) const |
|
116 { return (symbol_type & symbol_record::BUILTIN_VARIABLE); } |
195
|
117 |
3011
|
118 bool is_builtin_function (void) const |
|
119 { return (symbol_type & symbol_record::BUILTIN_FUNCTION); } |
|
120 |
|
121 // XXX FIXME XXX |
|
122 bool is_map_element (const string& /* elts */) const |
|
123 { return false; } |
|
124 |
|
125 bool is_defined (void) const |
|
126 { return definition.is_defined (); } |
|
127 |
|
128 bool is_read_only (void) const |
|
129 { return read_only; } |
|
130 |
|
131 bool is_eternal (void) const |
|
132 { return eternal; } |
8
|
133 |
3013
|
134 int rows (void) const { return definition.rows (); } |
|
135 int columns (void) const { return definition.columns (); } |
|
136 |
|
137 string type_name (void) const { return definition.type_name (); } |
|
138 |
3011
|
139 void define (const octave_value& val, unsigned int sym_type) |
|
140 { |
|
141 definition = val; |
|
142 symbol_type = sym_type; |
|
143 } |
|
144 |
|
145 void protect (void) { read_only = 1; } |
|
146 |
|
147 void unprotect (void) { read_only = 0; } |
|
148 |
|
149 void make_eternal (void) { eternal = 1; } |
|
150 |
|
151 octave_value& def (void) { return definition; } |
|
152 |
|
153 string help (void) const { return help_string; } |
|
154 |
|
155 void document (const string& h) { help_string = h; } |
|
156 |
|
157 unsigned int type (void) { return symbol_type; } |
|
158 |
|
159 void *operator new (size_t size) |
|
160 { return allocator.alloc (size); } |
|
161 |
|
162 void operator delete (void *p, size_t size) |
|
163 { allocator.free (p, size); } |
|
164 |
|
165 static octave_allocator allocator; |
8
|
166 |
3011
|
167 // The type of this symbol (see the enum above). |
3258
|
168 unsigned int symbol_type : 7; |
3011
|
169 |
|
170 // Nonzero means this variable cannot be cleared. |
|
171 unsigned int eternal : 1; |
|
172 |
|
173 // Nonzero means this variable cannot be given a new value. |
|
174 unsigned int read_only : 1; |
|
175 |
|
176 // The doc string associated with this variable. |
|
177 string help_string; |
|
178 |
|
179 // The value of this definition. See ov.h and related files. |
|
180 octave_value definition; |
767
|
181 |
3011
|
182 // Pointer to next definition in chain. This is used so that |
|
183 // variables can hide function definitions, and so that the function |
|
184 // definitions can reappear if the variable is cleared. |
|
185 symbol_def *next_elem; |
|
186 |
|
187 // Reference count. |
|
188 int count; |
|
189 |
3239
|
190 void dump_symbol_info (void); |
|
191 |
3011
|
192 // No copying! |
|
193 |
|
194 symbol_def (const symbol_def& sd); |
|
195 |
|
196 symbol_def& operator = (const symbol_def& sd); |
|
197 }; |
8
|
198 |
|
199 public: |
2953
|
200 |
3011
|
201 typedef int (*change_function) (void); |
2953
|
202 |
3011
|
203 symbol_record (void) |
|
204 : formal_param (0), linked_to_global (0), tagged_static (0), |
|
205 nm (), chg_fcn (0), definition (new symbol_def ()), |
|
206 next_elem (0) { } |
|
207 |
|
208 symbol_record (const string& n, symbol_record *nxt) |
|
209 : formal_param (0), linked_to_global (0), tagged_static (0), |
|
210 nm (n), chg_fcn (0), definition (new symbol_def ()), |
|
211 next_elem (nxt) { } |
8
|
212 |
1755
|
213 ~symbol_record (void) { } |
8
|
214 |
3011
|
215 string name (void) const { return nm; } |
2975
|
216 |
3011
|
217 string help (void) const { return definition->help (); } |
|
218 |
|
219 octave_value& def (void) { return definition->def (); } |
8
|
220 |
1755
|
221 void rename (const string& new_name); |
195
|
222 |
3011
|
223 bool is_function (void) const |
|
224 { return definition->is_function (); } |
|
225 |
|
226 bool is_text_function (void) const |
|
227 { return definition->is_text_function (); } |
|
228 |
|
229 bool is_mapper_function (void) const |
|
230 { return definition->is_mapper_function (); } |
|
231 |
|
232 bool is_user_function (void) const |
|
233 { return definition->is_user_function (); } |
195
|
234 |
3011
|
235 bool is_builtin_function (void) const |
|
236 { return definition->is_builtin_function (); } |
|
237 |
3258
|
238 bool is_constant (void) const |
|
239 { return definition->is_constant (); } |
|
240 |
|
241 bool is_builtin_constant (void) const |
|
242 { return definition->is_builtin_constant (); } |
|
243 |
3011
|
244 bool is_variable (void) const |
|
245 { return definition->is_variable (); } |
8
|
246 |
3011
|
247 bool is_user_variable (void) const |
|
248 { return definition->is_user_variable (); } |
|
249 |
|
250 bool is_builtin_variable (void) const |
|
251 { return definition->is_builtin_variable (); } |
|
252 |
|
253 bool is_map_element (const string& elts) const |
|
254 { return definition->is_map_element (elts); } |
8
|
255 |
3011
|
256 unsigned int type (void) const { return definition->type (); } |
|
257 |
|
258 bool is_defined (void) const { return definition->is_defined (); } |
|
259 |
|
260 bool is_read_only (void) const { return definition->is_read_only (); } |
8
|
261 |
3011
|
262 bool is_eternal (void) const { return definition->is_eternal (); } |
|
263 |
|
264 void protect (void) { definition->protect (); } |
195
|
265 |
3011
|
266 void unprotect (void) { definition->unprotect (); } |
|
267 |
|
268 void make_eternal (void) { definition->make_eternal (); } |
2893
|
269 |
3011
|
270 void set_change_function (change_function f) { chg_fcn = f; } |
2893
|
271 |
3011
|
272 void define (const octave_value& v, unsigned int sym_type = USER_VARIABLE); |
|
273 |
|
274 void define_builtin_var (const octave_value& v); |
195
|
275 |
3258
|
276 bool define_builtin_const (const octave_value& v); |
3011
|
277 |
|
278 bool define (octave_function *f, unsigned int sym_type); |
2893
|
279 |
3011
|
280 void document (const string& h) { definition->document (h); } |
195
|
281 |
3011
|
282 void clear (void); |
195
|
283 |
2856
|
284 void alias (symbol_record *s, bool force = false); |
8
|
285 |
|
286 void mark_as_formal_parameter (void); |
3011
|
287 bool is_formal_parameter (void) const { return formal_param; } |
8
|
288 |
195
|
289 void mark_as_linked_to_global (void); |
3011
|
290 bool is_linked_to_global (void) const { return linked_to_global; } |
8
|
291 |
2846
|
292 void mark_as_static (void); |
3011
|
293 bool is_static (void) const { return tagged_static; } |
2846
|
294 |
3013
|
295 bool hides_fcn (void) const; |
|
296 bool hides_builtin (void) const; |
|
297 |
|
298 int rows (void) const { return definition->rows (); } |
|
299 int columns (void) const { return definition->columns (); } |
|
300 |
|
301 string type_name (void) const { return definition->type_name (); } |
|
302 |
2975
|
303 octave_value& variable_value (void); |
2979
|
304 octave_lvalue variable_reference (void); |
2390
|
305 |
3011
|
306 symbol_record *next (void) const { return next_elem; } |
8
|
307 |
3011
|
308 void chain (symbol_record *s) { next_elem = s; } |
195
|
309 |
220
|
310 void push_context (void); |
3011
|
311 |
220
|
312 void pop_context (void); |
|
313 |
3013
|
314 void print_symbol_info_line (ostream& os); |
|
315 |
3239
|
316 void dump_symbol_info (void); |
|
317 |
8
|
318 private: |
|
319 |
2893
|
320 unsigned int formal_param : 1; |
|
321 unsigned int linked_to_global : 1; |
|
322 unsigned int tagged_static : 1; |
195
|
323 |
1755
|
324 string nm; |
3011
|
325 change_function chg_fcn; |
195
|
326 symbol_def *definition; |
8
|
327 symbol_record *next_elem; |
395
|
328 |
3011
|
329 // This should maybe be one stack with a structure containing all the |
|
330 // items we need to save for recursive calls... |
220
|
331 SLStack <symbol_def *> context; |
2893
|
332 SLStack <unsigned int> global_link_context; |
8
|
333 |
3011
|
334 bool read_only_error (const char *action); |
195
|
335 |
|
336 void push_def (symbol_def *sd); |
3011
|
337 |
|
338 void remove_top_def (void); |
|
339 |
|
340 void replace_all_defs (symbol_def *sd); |
|
341 |
3258
|
342 void link_to_builtin_variable (void); |
|
343 |
3011
|
344 // No copying! |
|
345 |
|
346 symbol_record (const symbol_record& s); |
195
|
347 |
8
|
348 symbol_record& operator = (const symbol_record& s); |
|
349 }; |
|
350 |
767
|
351 // A symbol table. |
200
|
352 |
|
353 #define SYMTAB_LOCAL_SCOPE 1 |
|
354 #define SYMTAB_GLOBAL_SCOPE 2 |
|
355 |
|
356 #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE) |
|
357 |
3011
|
358 #define SYMTAB_ALL_TYPES (symbol_record::USER_FUNCTION \ |
|
359 | symbol_record::USER_VARIABLE \ |
|
360 | symbol_record::BUILTIN_FUNCTION \ |
|
361 | symbol_record::TEXT_FUNCTION \ |
|
362 | symbol_record::MAPPER_FUNCTION \ |
3258
|
363 | symbol_record::BUILTIN_VARIABLE \ |
|
364 | symbol_record::BUILTIN_CONSTANT) |
200
|
365 |
3011
|
366 #define SYMTAB_VARIABLES (symbol_record::USER_VARIABLE \ |
|
367 | symbol_record::BUILTIN_VARIABLE) |
1412
|
368 |
8
|
369 class |
|
370 symbol_table |
|
371 { |
|
372 public: |
|
373 |
3011
|
374 symbol_table (unsigned int tab_size = 128) |
|
375 : table_size (tab_size), table (new symbol_record [table_size]) |
|
376 { |
|
377 assert ((tab_size % 2) == 0); |
|
378 } |
|
379 |
|
380 ~symbol_table (void) |
|
381 { |
|
382 delete [] table; |
|
383 } |
8
|
384 |
2856
|
385 symbol_record *lookup (const string& nm, bool insert = false, |
|
386 bool warn = false); |
8
|
387 |
1755
|
388 void rename (const string& old_name, const string& new_name); |
572
|
389 |
2856
|
390 void clear (bool clear_user_functions = true); |
3011
|
391 bool clear (const string& nm, bool clear_user_functions = true); |
8
|
392 |
164
|
393 int size (void) const; |
8
|
394 |
3013
|
395 Array<symbol_record *> |
|
396 symbol_list (int& count, const string_vector& pats = string_vector (), |
|
397 unsigned int type = SYMTAB_ALL_TYPES, |
|
398 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
|
399 |
195
|
400 |
1755
|
401 string_vector |
3013
|
402 name_list (int& count, const string_vector& pats = string_vector (), |
|
403 bool sort = false, unsigned int type = SYMTAB_ALL_TYPES, |
|
404 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
605
|
405 |
3011
|
406 |
|
407 int maybe_list (const char *header, const string_vector& argv, |
3013
|
408 ostream& os, bool show_verbose, |
3011
|
409 unsigned type, unsigned scope); |
|
410 |
1755
|
411 symbol_record **glob (int& count, const string& pat = string ("*"), |
2893
|
412 unsigned int type = SYMTAB_ALL_TYPES, |
|
413 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
8
|
414 |
220
|
415 void push_context (void); |
3011
|
416 |
220
|
417 void pop_context (void); |
|
418 |
3011
|
419 void print_stats (void); |
|
420 |
8
|
421 private: |
|
422 |
3011
|
423 unsigned int table_size; |
|
424 |
|
425 symbol_record *table; |
|
426 |
1755
|
427 unsigned int hash (const string& s); |
8
|
428 |
3011
|
429 // No copying! |
8
|
430 |
3011
|
431 symbol_table (const symbol_table&); |
1962
|
432 |
3011
|
433 symbol_table& operator = (const symbol_table&); |
|
434 }; |
2790
|
435 |
8
|
436 #endif |
|
437 |
|
438 /* |
|
439 ;;; Local Variables: *** |
|
440 ;;; mode: C++ *** |
|
441 ;;; End: *** |
|
442 */ |