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 |
4192
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
27 #pragma interface |
|
28 #endif |
|
29 |
3011
|
30 #include <cassert> |
|
31 |
1755
|
32 #include <string> |
4214
|
33 #include <stack> |
220
|
34 |
4238
|
35 #include "lo-sstream.h" |
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 |
3325
|
56 // the width parameter in the declaration for symbol_type below... |
3258
|
57 |
195
|
58 enum TYPE |
|
59 { |
|
60 UNKNOWN = 0, |
|
61 USER_FUNCTION = 1, |
|
62 USER_VARIABLE = 2, |
3325
|
63 DLD_FUNCTION = 4, |
|
64 BUILTIN_FUNCTION = 8, |
4208
|
65 COMMAND = 16, |
3325
|
66 MAPPER_FUNCTION = 32, |
|
67 BUILTIN_VARIABLE = 64, |
|
68 BUILTIN_CONSTANT = 128 |
195
|
69 }; |
|
70 |
8
|
71 private: |
|
72 |
3011
|
73 // Variables or functions. |
|
74 |
|
75 class symbol_def |
|
76 { |
|
77 public: |
|
78 |
|
79 symbol_def (const octave_value& val = octave_value (), |
|
80 unsigned int sym_type = 0) |
|
81 : symbol_type (sym_type), eternal (0), read_only (0), help_string (), |
4009
|
82 definition (val), count (1) { } |
3011
|
83 |
|
84 ~symbol_def (void) { } |
|
85 |
3258
|
86 bool is_constant (void) const |
|
87 { return (symbol_type & symbol_record::BUILTIN_CONSTANT); } |
|
88 |
3011
|
89 bool is_variable (void) const |
|
90 { |
|
91 return (symbol_type & symbol_record::USER_VARIABLE |
|
92 || symbol_type & symbol_record::BUILTIN_VARIABLE); |
|
93 } |
|
94 |
4208
|
95 // It's not necessary to check for COMMAND and MAPPER_FUNCTION |
3968
|
96 // here. Those tags are just used as additional qualifiers for |
|
97 // the other types of functions. |
|
98 |
3011
|
99 bool is_function (void) const |
|
100 { |
|
101 return (symbol_type & symbol_record::USER_FUNCTION |
3325
|
102 || symbol_type & symbol_record::DLD_FUNCTION |
3011
|
103 || symbol_type & symbol_record::BUILTIN_FUNCTION); |
|
104 } |
|
105 |
|
106 bool is_user_variable (void) const |
|
107 { return (symbol_type & symbol_record::USER_VARIABLE); } |
195
|
108 |
4208
|
109 void mark_as_command (void) |
|
110 { symbol_type |= symbol_record::COMMAND; } |
4207
|
111 |
4208
|
112 void unmark_command (void) |
|
113 { symbol_type &= ~symbol_record::COMMAND; } |
4207
|
114 |
4208
|
115 bool is_command (void) const |
|
116 { return (symbol_type & symbol_record::COMMAND); } |
3011
|
117 |
|
118 bool is_mapper_function (void) const |
|
119 { return (symbol_type & symbol_record::MAPPER_FUNCTION); } |
|
120 |
|
121 bool is_user_function (void) const |
|
122 { return (symbol_type & symbol_record::USER_FUNCTION); } |
|
123 |
3258
|
124 bool is_builtin_constant (void) const |
|
125 { return (symbol_type & symbol_record::BUILTIN_CONSTANT); } |
|
126 |
3011
|
127 bool is_builtin_variable (void) const |
|
128 { return (symbol_type & symbol_record::BUILTIN_VARIABLE); } |
195
|
129 |
3011
|
130 bool is_builtin_function (void) const |
|
131 { return (symbol_type & symbol_record::BUILTIN_FUNCTION); } |
|
132 |
3325
|
133 bool is_dld_function (void) const |
|
134 { return (symbol_type & symbol_record::DLD_FUNCTION); } |
|
135 |
3011
|
136 // XXX FIXME XXX |
3523
|
137 bool is_map_element (const std::string& /* elts */) const |
3011
|
138 { return false; } |
|
139 |
|
140 bool is_defined (void) const |
|
141 { return definition.is_defined (); } |
|
142 |
|
143 bool is_read_only (void) const |
|
144 { return read_only; } |
|
145 |
|
146 bool is_eternal (void) const |
|
147 { return eternal; } |
8
|
148 |
3013
|
149 int rows (void) const { return definition.rows (); } |
|
150 int columns (void) const { return definition.columns (); } |
|
151 |
3523
|
152 std::string type_name (void) const { return definition.type_name (); } |
3013
|
153 |
3523
|
154 std::string type_as_string (void) const; |
3355
|
155 |
3523
|
156 void type (std::ostream& os, const std::string& name, bool pr_type_info, |
3356
|
157 bool quiet, bool pr_orig_txt); |
|
158 |
3523
|
159 std::string which (const std::string& name); |
3355
|
160 |
3523
|
161 void which (std::ostream& os, const std::string& name); |
3355
|
162 |
3011
|
163 void define (const octave_value& val, unsigned int sym_type) |
|
164 { |
|
165 definition = val; |
|
166 symbol_type = sym_type; |
|
167 } |
|
168 |
|
169 void protect (void) { read_only = 1; } |
|
170 |
|
171 void unprotect (void) { read_only = 0; } |
|
172 |
|
173 void make_eternal (void) { eternal = 1; } |
|
174 |
|
175 octave_value& def (void) { return definition; } |
|
176 |
3523
|
177 std::string help (void) const { return help_string; } |
3011
|
178 |
3523
|
179 void document (const std::string& h) { help_string = h; } |
3011
|
180 |
|
181 unsigned int type (void) { return symbol_type; } |
|
182 |
|
183 void *operator new (size_t size) |
|
184 { return allocator.alloc (size); } |
|
185 |
|
186 void operator delete (void *p, size_t size) |
|
187 { allocator.free (p, size); } |
|
188 |
|
189 static octave_allocator allocator; |
8
|
190 |
3011
|
191 // The type of this symbol (see the enum above). |
3325
|
192 unsigned int symbol_type : 8; |
3011
|
193 |
|
194 // Nonzero means this variable cannot be cleared. |
|
195 unsigned int eternal : 1; |
|
196 |
|
197 // Nonzero means this variable cannot be given a new value. |
|
198 unsigned int read_only : 1; |
|
199 |
|
200 // The doc string associated with this variable. |
3523
|
201 std::string help_string; |
3011
|
202 |
|
203 // The value of this definition. See ov.h and related files. |
|
204 octave_value definition; |
767
|
205 |
3011
|
206 // Reference count. |
|
207 int count; |
|
208 |
3933
|
209 void print_info (std::ostream& os, |
|
210 const std::string& prefix = std::string ()) const; |
3239
|
211 |
3011
|
212 // No copying! |
|
213 |
|
214 symbol_def (const symbol_def& sd); |
|
215 |
|
216 symbol_def& operator = (const symbol_def& sd); |
|
217 }; |
8
|
218 |
|
219 public: |
2953
|
220 |
3011
|
221 typedef int (*change_function) (void); |
2953
|
222 |
3011
|
223 symbol_record (void) |
|
224 : formal_param (0), linked_to_global (0), tagged_static (0), |
4234
|
225 can_hide_function (1), nm (), chg_fcn (0), |
|
226 definition (new symbol_def ()), next_elem (0) { } |
|
227 |
|
228 // XXX FIXME XXX -- kluge alert! We obviously need a better way of |
|
229 // handling allow_shadow! |
3011
|
230 |
3523
|
231 symbol_record (const std::string& n, symbol_record *nxt) |
3011
|
232 : formal_param (0), linked_to_global (0), tagged_static (0), |
4234
|
233 can_hide_function (n != "__end__"), nm (n), chg_fcn (0), |
|
234 definition (new symbol_def ()), next_elem (nxt) { } |
8
|
235 |
1755
|
236 ~symbol_record (void) { } |
8
|
237 |
3523
|
238 std::string name (void) const { return nm; } |
2975
|
239 |
3523
|
240 std::string help (void) const { return definition->help (); } |
3011
|
241 |
|
242 octave_value& def (void) { return definition->def (); } |
8
|
243 |
3523
|
244 void rename (const std::string& new_name); |
195
|
245 |
3011
|
246 bool is_function (void) const |
|
247 { return definition->is_function (); } |
|
248 |
4208
|
249 void mark_as_command (void) |
|
250 { definition->mark_as_command (); } |
4207
|
251 |
4208
|
252 void unmark_command (void) |
|
253 { definition->unmark_command (); } |
4207
|
254 |
4208
|
255 bool is_command (void) const |
|
256 { return definition->is_command (); } |
3011
|
257 |
|
258 bool is_mapper_function (void) const |
|
259 { return definition->is_mapper_function (); } |
|
260 |
|
261 bool is_user_function (void) const |
|
262 { return definition->is_user_function (); } |
195
|
263 |
3011
|
264 bool is_builtin_function (void) const |
|
265 { return definition->is_builtin_function (); } |
|
266 |
3325
|
267 bool is_dld_function (void) const |
|
268 { return definition->is_dld_function (); } |
|
269 |
3258
|
270 bool is_constant (void) const |
|
271 { return definition->is_constant (); } |
|
272 |
|
273 bool is_builtin_constant (void) const |
|
274 { return definition->is_builtin_constant (); } |
|
275 |
3011
|
276 bool is_variable (void) const |
|
277 { return definition->is_variable (); } |
8
|
278 |
3011
|
279 bool is_user_variable (void) const |
|
280 { return definition->is_user_variable (); } |
|
281 |
|
282 bool is_builtin_variable (void) const |
|
283 { return definition->is_builtin_variable (); } |
|
284 |
3523
|
285 bool is_map_element (const std::string& elts) const |
3011
|
286 { return definition->is_map_element (elts); } |
8
|
287 |
3011
|
288 unsigned int type (void) const { return definition->type (); } |
|
289 |
|
290 bool is_defined (void) const { return definition->is_defined (); } |
|
291 |
|
292 bool is_read_only (void) const { return definition->is_read_only (); } |
8
|
293 |
3011
|
294 bool is_eternal (void) const { return definition->is_eternal (); } |
|
295 |
|
296 void protect (void) { definition->protect (); } |
195
|
297 |
3011
|
298 void unprotect (void) { definition->unprotect (); } |
|
299 |
|
300 void make_eternal (void) { definition->make_eternal (); } |
2893
|
301 |
3011
|
302 void set_change_function (change_function f) { chg_fcn = f; } |
2893
|
303 |
3011
|
304 void define (const octave_value& v, unsigned int sym_type = USER_VARIABLE); |
|
305 |
|
306 void define_builtin_var (const octave_value& v); |
195
|
307 |
3258
|
308 bool define_builtin_const (const octave_value& v); |
3011
|
309 |
|
310 bool define (octave_function *f, unsigned int sym_type); |
2893
|
311 |
3523
|
312 void document (const std::string& h) { definition->document (h); } |
195
|
313 |
3011
|
314 void clear (void); |
195
|
315 |
4009
|
316 void alias (symbol_record *s); |
8
|
317 |
|
318 void mark_as_formal_parameter (void); |
3011
|
319 bool is_formal_parameter (void) const { return formal_param; } |
8
|
320 |
195
|
321 void mark_as_linked_to_global (void); |
3011
|
322 bool is_linked_to_global (void) const { return linked_to_global; } |
8
|
323 |
2846
|
324 void mark_as_static (void); |
3011
|
325 bool is_static (void) const { return tagged_static; } |
4319
|
326 void unmark_static (void) { tagged_static = false; } |
2846
|
327 |
3013
|
328 int rows (void) const { return definition->rows (); } |
|
329 int columns (void) const { return definition->columns (); } |
|
330 |
3523
|
331 std::string type_name (void) const { return definition->type_name (); } |
3013
|
332 |
3523
|
333 std::string type_as_string (void) const |
3355
|
334 { return definition->type_as_string (); } |
|
335 |
3523
|
336 void type (std::ostream& os, bool pr_type_info, bool quiet, bool pr_orig_txt) |
3356
|
337 { definition->type (os, name (), pr_type_info, quiet, pr_orig_txt); } |
|
338 |
3523
|
339 std::string which (void) { return definition->which (name ()); } |
3355
|
340 |
3523
|
341 void which (std::ostream& os) { definition->which (os, name ()); } |
3355
|
342 |
2975
|
343 octave_value& variable_value (void); |
2979
|
344 octave_lvalue variable_reference (void); |
2390
|
345 |
3011
|
346 symbol_record *next (void) const { return next_elem; } |
8
|
347 |
3011
|
348 void chain (symbol_record *s) { next_elem = s; } |
195
|
349 |
220
|
350 void push_context (void); |
3011
|
351 |
220
|
352 void pop_context (void); |
|
353 |
3933
|
354 void print_symbol_info_line (std::ostream& os) const; |
3013
|
355 |
3933
|
356 void print_info (std::ostream& os, |
|
357 const std::string& prefix = std::string ()) const; |
3239
|
358 |
8
|
359 private: |
|
360 |
2893
|
361 unsigned int formal_param : 1; |
|
362 unsigned int linked_to_global : 1; |
|
363 unsigned int tagged_static : 1; |
4234
|
364 unsigned int can_hide_function : 1; |
195
|
365 |
3523
|
366 std::string nm; |
3011
|
367 change_function chg_fcn; |
195
|
368 symbol_def *definition; |
8
|
369 symbol_record *next_elem; |
395
|
370 |
3011
|
371 // This should maybe be one stack with a structure containing all the |
|
372 // items we need to save for recursive calls... |
4214
|
373 std::stack <symbol_def *> context; |
|
374 std::stack <unsigned int> global_link_context; |
8
|
375 |
3011
|
376 bool read_only_error (const char *action); |
195
|
377 |
3258
|
378 void link_to_builtin_variable (void); |
|
379 |
3011
|
380 // No copying! |
|
381 |
|
382 symbol_record (const symbol_record& s); |
195
|
383 |
8
|
384 symbol_record& operator = (const symbol_record& s); |
|
385 }; |
|
386 |
767
|
387 // A symbol table. |
200
|
388 |
|
389 #define SYMTAB_LOCAL_SCOPE 1 |
|
390 #define SYMTAB_GLOBAL_SCOPE 2 |
|
391 |
|
392 #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE) |
|
393 |
3011
|
394 #define SYMTAB_ALL_TYPES (symbol_record::USER_FUNCTION \ |
|
395 | symbol_record::USER_VARIABLE \ |
3325
|
396 | symbol_record::DLD_FUNCTION \ |
3011
|
397 | symbol_record::BUILTIN_FUNCTION \ |
4208
|
398 | symbol_record::COMMAND \ |
3011
|
399 | symbol_record::MAPPER_FUNCTION \ |
3258
|
400 | symbol_record::BUILTIN_VARIABLE \ |
|
401 | symbol_record::BUILTIN_CONSTANT) |
200
|
402 |
3011
|
403 #define SYMTAB_VARIABLES (symbol_record::USER_VARIABLE \ |
|
404 | symbol_record::BUILTIN_VARIABLE) |
1412
|
405 |
8
|
406 class |
|
407 symbol_table |
|
408 { |
|
409 public: |
|
410 |
4238
|
411 symbol_table (unsigned int tab_size = 128, |
|
412 const std::string& nm = std::string ()) |
|
413 : table_size (tab_size), table (new symbol_record [table_size]), |
|
414 table_name (nm) |
4009
|
415 { |
|
416 assert ((tab_size % 2) == 0); |
4238
|
417 |
|
418 if (table_name.empty ()) |
|
419 { |
|
420 OSSTREAM buf; |
|
421 buf << symtab_count++ << OSSTREAM_ENDS; |
|
422 table_name = OSSTREAM_STR (buf); |
|
423 OSSTREAM_FREEZE (buf); |
|
424 } |
4009
|
425 } |
3011
|
426 |
|
427 ~symbol_table (void) |
4009
|
428 { |
|
429 clear (); |
|
430 delete [] table; |
|
431 } |
8
|
432 |
3523
|
433 symbol_record *lookup (const std::string& nm, bool insert = false, |
2856
|
434 bool warn = false); |
8
|
435 |
3523
|
436 void rename (const std::string& old_name, const std::string& new_name); |
572
|
437 |
4009
|
438 void clear (void); |
|
439 |
|
440 void clear_variables (void); |
|
441 void clear_functions (void); |
|
442 void clear_globals (void); |
|
443 |
|
444 bool clear (const std::string& nm); |
|
445 |
|
446 bool clear_variable (const std::string& nm); |
|
447 bool clear_function (const std::string& nm); |
|
448 bool clear_global (const std::string& nm); |
|
449 |
|
450 bool clear_variable_pattern (const std::string& pat); |
|
451 bool clear_function_pattern (const std::string& pat); |
|
452 bool clear_global_pattern (const std::string& pat); |
8
|
453 |
164
|
454 int size (void) const; |
8
|
455 |
3013
|
456 Array<symbol_record *> |
3355
|
457 symbol_list (const string_vector& pats = string_vector (), |
3013
|
458 unsigned int type = SYMTAB_ALL_TYPES, |
|
459 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
|
460 |
195
|
461 |
1755
|
462 string_vector |
3355
|
463 name_list (const string_vector& pats = string_vector (), |
3013
|
464 bool sort = false, unsigned int type = SYMTAB_ALL_TYPES, |
|
465 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
605
|
466 |
4009
|
467 string_vector |
|
468 user_function_name_list (void) const |
|
469 { |
|
470 return name_list |
|
471 (string_vector (), false, |
|
472 symbol_record::USER_FUNCTION|symbol_record::DLD_FUNCTION, |
|
473 SYMTAB_ALL_SCOPES); |
|
474 } |
|
475 |
|
476 string_vector |
|
477 global_variable_name_list (void) const |
|
478 { |
|
479 return name_list |
|
480 (string_vector (), false, SYMTAB_VARIABLES, SYMTAB_GLOBAL_SCOPE); |
|
481 } |
|
482 |
|
483 string_vector |
|
484 variable_name_list (void) const |
|
485 { |
|
486 return name_list |
|
487 (string_vector (), false, SYMTAB_VARIABLES, SYMTAB_LOCAL_SCOPE); |
|
488 } |
3011
|
489 |
|
490 int maybe_list (const char *header, const string_vector& argv, |
3523
|
491 std::ostream& os, bool show_verbose, |
3011
|
492 unsigned type, unsigned scope); |
|
493 |
3523
|
494 Array<symbol_record *> glob (const std::string& pat = std::string ("*"), |
3355
|
495 unsigned int type = SYMTAB_ALL_TYPES, |
|
496 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
8
|
497 |
220
|
498 void push_context (void); |
3011
|
499 |
220
|
500 void pop_context (void); |
|
501 |
3941
|
502 void print_info (std::ostream& os) const; |
3011
|
503 |
8
|
504 private: |
|
505 |
3011
|
506 unsigned int table_size; |
|
507 |
|
508 symbol_record *table; |
|
509 |
4238
|
510 std::string table_name; |
|
511 |
|
512 static unsigned long int symtab_count; |
|
513 |
3523
|
514 unsigned int hash (const std::string& s); |
8
|
515 |
3011
|
516 // No copying! |
8
|
517 |
3011
|
518 symbol_table (const symbol_table&); |
1962
|
519 |
3011
|
520 symbol_table& operator = (const symbol_table&); |
|
521 }; |
2790
|
522 |
8
|
523 #endif |
|
524 |
|
525 /* |
|
526 ;;; Local Variables: *** |
|
527 ;;; mode: C++ *** |
|
528 ;;; End: *** |
|
529 */ |