1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
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. |
1
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
3013
|
31 #include <cassert> |
1962
|
32 #include <cctype> |
3013
|
33 #include <climits> |
|
34 |
3503
|
35 #include <iomanip> |
|
36 #include <fstream> |
1962
|
37 |
2926
|
38 #include "glob-match.h" |
1755
|
39 #include "str-vec.h" |
|
40 |
3308
|
41 #include "defun.h" |
1352
|
42 #include "error.h" |
2979
|
43 #include "oct-lvalue.h" |
2975
|
44 #include "ov.h" |
3356
|
45 #include "pt-pr-code.h" |
1755
|
46 #include "symtab.h" |
1
|
47 #include "utils.h" |
1352
|
48 #include "variables.h" |
1
|
49 |
4238
|
50 unsigned long int symbol_table::symtab_count = 0; |
|
51 |
3308
|
52 // Should variables be allowed to hide functions of the same name? A |
|
53 // positive value means yes. A negative value means yes, but print a |
|
54 // warning message. Zero means it should be considered an error. |
|
55 static int Vvariables_can_hide_functions; |
|
56 |
4238
|
57 // Nonzero means we print debugging info about symbol table lookups. |
|
58 static int Vdebug_symtab_lookups; |
|
59 |
3013
|
60 octave_allocator |
|
61 symbol_record::symbol_def::allocator (sizeof (symbol_record::symbol_def)); |
195
|
62 |
3355
|
63 #define SYMBOL_DEF symbol_record::symbol_def |
|
64 |
3536
|
65 std::string |
3355
|
66 SYMBOL_DEF::type_as_string (void) const |
|
67 { |
3523
|
68 std::string retval = "<unknown type>"; |
3355
|
69 |
|
70 if (is_user_variable ()) |
|
71 retval = "user-defined variable"; |
4208
|
72 else if (is_command ()) |
|
73 retval = "built-in command"; |
3355
|
74 else if (is_mapper_function ()) |
|
75 retval = "built-in mapper function"; |
|
76 else if (is_user_function ()) |
|
77 retval = "user-defined function"; |
|
78 else if (is_builtin_constant ()) |
|
79 retval = "built-in constant"; |
|
80 else if (is_builtin_variable ()) |
|
81 retval = "built-in variable"; |
|
82 else if (is_builtin_function ()) |
|
83 retval = "built-in function"; |
|
84 else if (is_dld_function ()) |
|
85 retval = "dynamically-linked function"; |
|
86 |
|
87 return retval; |
|
88 } |
|
89 |
3356
|
90 void |
3523
|
91 SYMBOL_DEF::type (std::ostream& os, const std::string& name, bool pr_type_info, |
3356
|
92 bool quiet, bool pr_orig_txt) |
|
93 { |
|
94 if (is_user_function ()) |
|
95 { |
|
96 octave_function *defn = definition.function_value (); |
|
97 |
3523
|
98 std::string fn = defn ? defn->fcn_file_name () : std::string (); |
3356
|
99 |
|
100 if (pr_orig_txt && ! fn.empty ()) |
|
101 { |
3544
|
102 std::ifstream fs (fn.c_str (), std::ios::in); |
3356
|
103 |
|
104 if (fs) |
|
105 { |
|
106 if (pr_type_info && ! quiet) |
|
107 os << name << " is the " << type_as_string () |
|
108 << " defined from: " << fn << "\n\n"; |
|
109 |
|
110 char ch; |
|
111 |
|
112 while (fs.get (ch)) |
|
113 os << ch; |
|
114 } |
|
115 else |
|
116 os << "unable to open `" << fn << "' for reading!\n"; |
|
117 } |
|
118 else |
|
119 { |
|
120 if (pr_type_info && ! quiet) |
|
121 os << name << " is a " << type_as_string () << ":\n\n"; |
|
122 |
|
123 tree_print_code tpc (os, "", pr_orig_txt); |
|
124 |
|
125 defn->accept (tpc); |
|
126 } |
|
127 } |
|
128 else if (is_user_variable () |
|
129 || is_builtin_variable () |
|
130 || is_builtin_constant ()) |
|
131 { |
|
132 if (pr_type_info && ! quiet) |
|
133 os << name << " is a " << type_as_string () << "\n"; |
|
134 |
|
135 definition.print_raw (os, true); |
|
136 |
|
137 if (pr_type_info) |
|
138 os << "\n"; |
|
139 } |
|
140 else |
|
141 os << name << " is a " << type_as_string () << "\n"; |
|
142 } |
|
143 |
3536
|
144 std::string |
3523
|
145 SYMBOL_DEF::which (const std::string& name) |
3355
|
146 { |
3523
|
147 std::string retval; |
3355
|
148 |
|
149 if (is_user_function () || is_dld_function ()) |
|
150 { |
|
151 octave_function *defn = definition.function_value (); |
|
152 |
|
153 if (defn) |
|
154 retval = defn->fcn_file_name (); |
|
155 } |
|
156 else |
|
157 retval = name + " is a " + type_as_string (); |
|
158 |
|
159 return retval; |
|
160 } |
|
161 |
3239
|
162 void |
3523
|
163 SYMBOL_DEF::which (std::ostream& os, const std::string& name) |
3355
|
164 { |
|
165 os << name; |
|
166 |
|
167 if (is_user_function () || is_dld_function ()) |
|
168 { |
|
169 octave_function *defn = definition.function_value (); |
|
170 |
3523
|
171 std::string fn = defn ? defn->fcn_file_name () : std::string (); |
3355
|
172 |
|
173 if (! fn.empty ()) |
|
174 { |
|
175 os << " is the " << type_as_string () << " from the file\n" |
|
176 << fn << "\n"; |
|
177 |
|
178 return; |
|
179 } |
|
180 } |
|
181 |
|
182 os << " is a " << type_as_string () << "\n"; |
|
183 } |
|
184 |
|
185 void |
3944
|
186 SYMBOL_DEF::print_info (std::ostream& os, const std::string& prefix) const |
3239
|
187 { |
3933
|
188 os << prefix << "symbol_def::count: " << count << "\n"; |
|
189 |
|
190 definition.print_info (os, prefix + " "); |
3239
|
191 } |
|
192 |
767
|
193 // Individual records in a symbol table. |
|
194 |
3013
|
195 // XXX FIXME XXX -- there are lots of places below where we should |
|
196 // probably be temporarily ignoring interrupts. |
1
|
197 |
572
|
198 void |
3523
|
199 symbol_record::rename (const std::string& new_name) |
572
|
200 { |
2791
|
201 if (! read_only_error ("rename")) |
|
202 nm = new_name; |
572
|
203 } |
|
204 |
3013
|
205 void |
|
206 symbol_record::define (const octave_value& v, unsigned int sym_type) |
195
|
207 { |
3013
|
208 if (! (is_variable () && read_only_error ("redefine"))) |
|
209 { |
|
210 if (definition->type () == symbol_record::BUILTIN_VARIABLE) |
|
211 sym_type = symbol_record::BUILTIN_VARIABLE; |
1
|
212 |
3013
|
213 definition->define (v, sym_type); |
195
|
214 } |
|
215 } |
|
216 |
|
217 void |
3013
|
218 symbol_record::define_builtin_var (const octave_value& v) |
195
|
219 { |
3013
|
220 define (v, symbol_record::BUILTIN_VARIABLE); |
195
|
221 |
3013
|
222 if (chg_fcn) |
|
223 chg_fcn (); |
1
|
224 } |
|
225 |
3013
|
226 bool |
3258
|
227 symbol_record::define_builtin_const (const octave_value& v) |
1
|
228 { |
3013
|
229 bool retval = false; |
1
|
230 |
3258
|
231 if (! read_only_error ("redefine")) |
195
|
232 { |
3811
|
233 definition->define (v, symbol_record::BUILTIN_CONSTANT); |
2893
|
234 |
3013
|
235 retval = true; |
1
|
236 } |
|
237 |
2893
|
238 return retval; |
2390
|
239 } |
|
240 |
3013
|
241 bool |
|
242 symbol_record::define (octave_function *f, unsigned int sym_type) |
1
|
243 { |
3013
|
244 bool retval = false; |
2949
|
245 |
3013
|
246 if (! read_only_error ("redefine")) |
|
247 { |
|
248 octave_value tmp (f); |
|
249 |
4009
|
250 delete definition; |
|
251 |
|
252 definition = new symbol_def (tmp, sym_type); |
3013
|
253 |
|
254 retval = true; |
|
255 } |
2949
|
256 |
|
257 return retval; |
1
|
258 } |
|
259 |
|
260 void |
195
|
261 symbol_record::clear (void) |
1
|
262 { |
4009
|
263 if (! tagged_static) |
1
|
264 { |
3013
|
265 if (--definition->count <= 0) |
|
266 delete definition; |
|
267 |
|
268 definition = new symbol_def (); |
4009
|
269 } |
3013
|
270 |
4009
|
271 if (linked_to_global) |
|
272 linked_to_global = 0; |
1
|
273 } |
|
274 |
|
275 void |
4009
|
276 symbol_record::alias (symbol_record *s) |
1
|
277 { |
3013
|
278 chg_fcn = s->chg_fcn; |
195
|
279 |
4009
|
280 if (--definition->count <= 0) |
|
281 delete definition; |
|
282 |
|
283 definition = (s->definition); |
3013
|
284 |
|
285 definition->count++; |
1
|
286 } |
|
287 |
|
288 void |
|
289 symbol_record::mark_as_formal_parameter (void) |
|
290 { |
3013
|
291 if (is_linked_to_global ()) |
|
292 error ("can't mark global variable `%s' as function parameter", |
|
293 nm.c_str ()); |
|
294 else if (is_static ()) |
|
295 error ("can't mark static variable `%s' as function paraemter", |
|
296 nm.c_str ()); |
|
297 else |
|
298 formal_param = 1; |
1
|
299 } |
|
300 |
|
301 void |
195
|
302 symbol_record::mark_as_linked_to_global (void) |
122
|
303 { |
2975
|
304 if (is_formal_parameter ()) |
|
305 error ("can't make function parameter `%s' global", nm.c_str ()); |
|
306 else if (is_static ()) |
|
307 error ("can't make static variable `%s' global", nm.c_str ()); |
|
308 else |
|
309 linked_to_global = 1; |
122
|
310 } |
|
311 |
2846
|
312 void |
|
313 symbol_record::mark_as_static (void) |
|
314 { |
|
315 if (is_linked_to_global ()) |
2975
|
316 error ("can't make global variable `%s' static", nm.c_str ()); |
2846
|
317 else if (is_formal_parameter ()) |
2975
|
318 error ("can't make formal parameter `%s' static", nm.c_str ()); |
2846
|
319 else |
|
320 tagged_static = 1; |
|
321 } |
|
322 |
2975
|
323 octave_value& |
|
324 symbol_record::variable_value (void) |
2390
|
325 { |
2975
|
326 static octave_value foo; |
2390
|
327 |
2975
|
328 return is_variable () ? def () : foo; |
2390
|
329 } |
|
330 |
3258
|
331 inline void |
|
332 symbol_record::link_to_builtin_variable (void) |
|
333 { |
4009
|
334 symbol_record *tmp_sym = fbi_sym_tab->lookup (name ()); |
3258
|
335 |
|
336 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
337 alias (tmp_sym); |
|
338 } |
|
339 |
2979
|
340 octave_lvalue |
2390
|
341 symbol_record::variable_reference (void) |
|
342 { |
4234
|
343 if ((Vvariables_can_hide_functions <= 0 || ! can_hide_function) |
3308
|
344 && (is_function () |
|
345 || (! is_defined () && is_valid_function (nm)))) |
|
346 { |
4234
|
347 if (Vvariables_can_hide_functions < 0 && can_hide_function) |
3308
|
348 warning ("variable `%s' hides function", nm.c_str ()); |
|
349 else |
|
350 { |
|
351 error ("variable `%s' hides function", nm.c_str ()); |
|
352 return octave_lvalue (); |
|
353 } |
|
354 } |
|
355 |
3258
|
356 if (is_function () || is_constant ()) |
2390
|
357 clear (); |
|
358 |
|
359 if (! is_defined ()) |
|
360 { |
|
361 if (! (is_formal_parameter () || is_linked_to_global ())) |
3258
|
362 link_to_builtin_variable (); |
2390
|
363 |
|
364 if (! is_defined ()) |
2975
|
365 { |
|
366 octave_value tmp; |
|
367 define (tmp); |
|
368 } |
2390
|
369 } |
|
370 |
3013
|
371 return octave_lvalue (&(def ()), chg_fcn); |
195
|
372 } |
|
373 |
220
|
374 void |
|
375 symbol_record::push_context (void) |
|
376 { |
2846
|
377 if (! is_static ()) |
|
378 { |
|
379 context.push (definition); |
3013
|
380 |
|
381 definition = new symbol_def (); |
395
|
382 |
2893
|
383 global_link_context.push (static_cast<unsigned int> (linked_to_global)); |
3013
|
384 |
2846
|
385 linked_to_global = 0; |
|
386 } |
220
|
387 } |
|
388 |
|
389 void |
|
390 symbol_record::pop_context (void) |
|
391 { |
1653
|
392 // It is possible for context to be empty if new symbols have been |
|
393 // inserted in the symbol table during recursive calls. This can |
|
394 // happen as a result of calls to eval() and feval(). |
220
|
395 |
1653
|
396 if (! context.empty ()) |
220
|
397 { |
4009
|
398 if (--definition->count <= 0) |
|
399 delete definition; |
|
400 |
4214
|
401 definition = context.top (); |
|
402 context.pop (); |
1653
|
403 |
4214
|
404 linked_to_global = global_link_context.top (); |
|
405 global_link_context.pop (); |
220
|
406 } |
|
407 } |
|
408 |
3013
|
409 void |
3933
|
410 symbol_record::print_symbol_info_line (std::ostream& os) const |
3013
|
411 { |
|
412 os << (is_read_only () ? " r-" : " rw") |
|
413 << (is_eternal () ? "-" : "d") |
|
414 << " " |
3548
|
415 << std::setiosflags (std::ios::left) << std::setw (24) |
|
416 << type_name () . c_str (); |
3013
|
417 |
3548
|
418 os << std::resetiosflags (std::ios::left); |
3013
|
419 |
|
420 int nr = rows (); |
|
421 int nc = columns (); |
|
422 |
|
423 if (nr < 0) |
|
424 os << " -"; |
|
425 else |
3548
|
426 os << std::setiosflags (std::ios::right) << std::setw (7) << nr; |
3013
|
427 |
|
428 if (nc < 0) |
|
429 os << " -"; |
|
430 else |
3548
|
431 os << std::setiosflags (std::ios::right) << std::setw (7) << nc; |
3013
|
432 |
3548
|
433 os << std::resetiosflags (std::ios::right); |
3013
|
434 |
|
435 os << " " << name () << "\n"; |
|
436 } |
|
437 |
3239
|
438 void |
3944
|
439 symbol_record::print_info (std::ostream& os, const std::string& prefix) const |
3239
|
440 { |
|
441 if (definition) |
3933
|
442 definition->print_info (os, prefix); |
3239
|
443 else |
3933
|
444 os << prefix << "symbol " << name () << " is undefined\n"; |
3239
|
445 } |
|
446 |
3013
|
447 bool |
2791
|
448 symbol_record::read_only_error (const char *action) |
195
|
449 { |
|
450 if (is_read_only ()) |
|
451 { |
3258
|
452 if (is_variable () || is_constant ()) |
3013
|
453 ::error ("can't %s read-only constant `%s'", action, nm.c_str ()); |
195
|
454 else if (is_function ()) |
3013
|
455 ::error ("can't %s read-only function `%s'", action, nm.c_str ()); |
1045
|
456 else |
3013
|
457 ::error ("can't %s read-only symbol `%s'", action, nm.c_str ()); |
195
|
458 |
3013
|
459 return true; |
195
|
460 } |
|
461 else |
3013
|
462 return false; |
195
|
463 } |
|
464 |
767
|
465 // A symbol table. |
1
|
466 |
|
467 symbol_record * |
3523
|
468 symbol_table::lookup (const std::string& nm, bool insert, bool warn) |
1
|
469 { |
4238
|
470 if (Vdebug_symtab_lookups) |
|
471 { |
|
472 std::cerr << (table_name.empty () ? "???" : table_name) |
|
473 << " symtab::lookup [" |
|
474 << (insert ? "I" : "-") |
|
475 << (warn ? "W" : "-") |
|
476 << "] \"" << nm << "\"\n"; |
|
477 } |
|
478 |
3013
|
479 unsigned int index = hash (nm); |
1
|
480 |
|
481 symbol_record *ptr = table[index].next (); |
|
482 |
530
|
483 while (ptr) |
1
|
484 { |
1755
|
485 if (ptr->name () == nm) |
1
|
486 return ptr; |
3013
|
487 |
1
|
488 ptr = ptr->next (); |
|
489 } |
|
490 |
|
491 if (insert) |
|
492 { |
3013
|
493 symbol_record *sr = new symbol_record (nm, table[index].next ()); |
|
494 |
|
495 table[index].chain (sr); |
|
496 |
|
497 return sr; |
1
|
498 } |
|
499 else if (warn) |
3356
|
500 warning ("lookup: symbol `%s' not found", nm.c_str ()); |
1
|
501 |
530
|
502 return 0; |
1
|
503 } |
|
504 |
|
505 void |
3523
|
506 symbol_table::rename (const std::string& old_name, const std::string& new_name) |
572
|
507 { |
4238
|
508 if (Vdebug_symtab_lookups) |
|
509 { |
|
510 std::cerr << (table_name.empty () ? "???" : table_name) |
|
511 << " symtab::rename " |
|
512 << "\"" << old_name << "\"" |
|
513 << " to " |
|
514 << "\"" << new_name << "\"\n"; |
|
515 } |
|
516 |
3013
|
517 unsigned int index = hash (old_name); |
572
|
518 |
|
519 symbol_record *prev = &table[index]; |
|
520 symbol_record *ptr = prev->next (); |
|
521 |
|
522 while (ptr) |
|
523 { |
1755
|
524 if (ptr->name () == old_name) |
572
|
525 { |
|
526 ptr->rename (new_name); |
|
527 |
2791
|
528 if (! error_state) |
|
529 { |
|
530 prev->chain (ptr->next ()); |
|
531 |
3013
|
532 index = hash (new_name); |
3125
|
533 ptr->chain (table[index].next ()); |
2791
|
534 table[index].chain (ptr); |
|
535 |
|
536 return; |
|
537 } |
|
538 |
|
539 break; |
572
|
540 } |
|
541 |
|
542 prev = ptr; |
|
543 ptr = ptr->next (); |
|
544 } |
|
545 |
1755
|
546 error ("unable to rename `%s' to `%s'", old_name.c_str (), |
|
547 new_name.c_str ()); |
572
|
548 } |
|
549 |
4009
|
550 // XXX FIXME XXX -- it would be nice to eliminate a lot of the |
|
551 // following duplicate code. |
|
552 |
572
|
553 void |
4009
|
554 symbol_table::clear (void) |
|
555 { |
|
556 for (unsigned int i = 0; i < table_size; i++) |
|
557 { |
|
558 symbol_record *ptr = table[i].next (); |
|
559 |
|
560 while (ptr) |
|
561 { |
|
562 ptr->clear (); |
|
563 |
|
564 ptr = ptr->next (); |
|
565 } |
|
566 } |
|
567 } |
|
568 |
|
569 void |
|
570 symbol_table::clear_variables (void) |
|
571 { |
|
572 for (unsigned int i = 0; i < table_size; i++) |
|
573 { |
|
574 symbol_record *ptr = table[i].next (); |
|
575 |
|
576 while (ptr) |
|
577 { |
|
578 if (ptr->is_user_variable ()) |
|
579 ptr->clear (); |
|
580 |
|
581 ptr = ptr->next (); |
|
582 } |
|
583 } |
|
584 } |
|
585 |
|
586 // Really only clear functions that can be reloaded. |
|
587 |
|
588 void |
|
589 symbol_table::clear_functions (void) |
1
|
590 { |
3013
|
591 for (unsigned int i = 0; i < table_size; i++) |
1
|
592 { |
169
|
593 symbol_record *ptr = table[i].next (); |
1
|
594 |
530
|
595 while (ptr) |
169
|
596 { |
4009
|
597 if (ptr->is_user_function () || ptr->is_dld_function ()) |
|
598 ptr->clear (); |
|
599 |
|
600 ptr = ptr->next (); |
|
601 } |
|
602 } |
|
603 } |
|
604 |
|
605 void |
|
606 symbol_table::clear_globals (void) |
|
607 { |
|
608 for (unsigned int i = 0; i < table_size; i++) |
|
609 { |
|
610 symbol_record *ptr = table[i].next (); |
|
611 |
|
612 while (ptr) |
|
613 { |
|
614 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
615 ptr->clear (); |
195
|
616 |
169
|
617 ptr = ptr->next (); |
1
|
618 } |
|
619 } |
|
620 } |
|
621 |
3013
|
622 bool |
4009
|
623 symbol_table::clear (const std::string& nm) |
|
624 { |
|
625 unsigned int index = hash (nm); |
|
626 |
|
627 symbol_record *ptr = table[index].next (); |
|
628 |
|
629 while (ptr) |
|
630 { |
|
631 if (ptr->name () == nm) |
|
632 { |
|
633 ptr->clear (); |
|
634 return true; |
|
635 } |
|
636 ptr = ptr->next (); |
|
637 } |
|
638 |
|
639 return false; |
|
640 } |
|
641 |
|
642 bool |
|
643 symbol_table::clear_variable (const std::string& nm) |
|
644 { |
|
645 unsigned int index = hash (nm); |
|
646 |
|
647 symbol_record *ptr = table[index].next (); |
|
648 |
|
649 while (ptr) |
|
650 { |
|
651 if (ptr->name () == nm && ptr->is_user_variable ()) |
|
652 { |
|
653 ptr->clear (); |
|
654 return true; |
|
655 } |
|
656 ptr = ptr->next (); |
|
657 } |
|
658 |
|
659 return false; |
|
660 } |
|
661 |
|
662 bool |
|
663 symbol_table::clear_global (const std::string& nm) |
1
|
664 { |
3013
|
665 unsigned int index = hash (nm); |
1
|
666 |
195
|
667 symbol_record *ptr = table[index].next (); |
1
|
668 |
530
|
669 while (ptr) |
1
|
670 { |
1755
|
671 if (ptr->name () == nm |
4009
|
672 && ptr->is_user_variable () |
|
673 && ptr->is_linked_to_global ()) |
|
674 { |
|
675 ptr->clear (); |
|
676 return true; |
|
677 } |
|
678 ptr = ptr->next (); |
|
679 } |
|
680 |
|
681 return false; |
|
682 } |
|
683 |
|
684 // Really only clear functions that can be reloaded. |
|
685 |
|
686 bool |
|
687 symbol_table::clear_function (const std::string& nm) |
|
688 { |
|
689 unsigned int index = hash (nm); |
|
690 |
|
691 symbol_record *ptr = table[index].next (); |
|
692 |
|
693 while (ptr) |
|
694 { |
|
695 if (ptr->name () == nm |
|
696 && (ptr->is_user_function () || ptr->is_dld_function ())) |
1
|
697 { |
195
|
698 ptr->clear (); |
3013
|
699 return true; |
1
|
700 } |
195
|
701 ptr = ptr->next (); |
1
|
702 } |
|
703 |
3013
|
704 return false; |
1
|
705 } |
|
706 |
4009
|
707 bool |
|
708 symbol_table::clear_variable_pattern (const std::string& pat) |
|
709 { |
|
710 bool retval = false; |
|
711 |
|
712 for (unsigned int i = 0; i < table_size; i++) |
|
713 { |
|
714 symbol_record *ptr = table[i].next (); |
|
715 |
|
716 while (ptr) |
|
717 { |
|
718 if (ptr->is_user_variable ()) |
|
719 { |
|
720 glob_match pattern (pat); |
|
721 |
|
722 if (pattern.match (ptr->name ())) |
|
723 { |
|
724 ptr->clear (); |
|
725 |
|
726 retval = true; |
|
727 } |
|
728 } |
|
729 |
|
730 ptr = ptr->next (); |
|
731 } |
|
732 } |
|
733 |
|
734 return retval; |
|
735 } |
|
736 |
|
737 bool |
|
738 symbol_table::clear_global_pattern (const std::string& pat) |
|
739 { |
|
740 bool retval = false; |
|
741 |
|
742 for (unsigned int i = 0; i < table_size; i++) |
|
743 { |
|
744 symbol_record *ptr = table[i].next (); |
|
745 |
|
746 while (ptr) |
|
747 { |
|
748 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
749 { |
|
750 glob_match pattern (pat); |
|
751 |
|
752 if (pattern.match (ptr->name ())) |
|
753 { |
|
754 ptr->clear (); |
|
755 |
|
756 retval = true; |
|
757 } |
|
758 } |
|
759 |
|
760 ptr = ptr->next (); |
|
761 } |
|
762 } |
|
763 |
|
764 return retval; |
|
765 } |
|
766 |
|
767 // Really only clear functions that can be reloaded. |
|
768 |
|
769 bool |
|
770 symbol_table::clear_function_pattern (const std::string& pat) |
|
771 { |
|
772 bool retval = false; |
|
773 |
|
774 for (unsigned int i = 0; i < table_size; i++) |
|
775 { |
|
776 symbol_record *ptr = table[i].next (); |
|
777 |
|
778 while (ptr) |
|
779 { |
|
780 if (ptr->is_user_function () || ptr->is_dld_function ()) |
|
781 { |
|
782 glob_match pattern (pat); |
|
783 |
|
784 if (pattern.match (ptr->name ())) |
|
785 { |
|
786 ptr->clear (); |
|
787 |
|
788 retval = true; |
|
789 } |
|
790 } |
|
791 |
|
792 ptr = ptr->next (); |
|
793 } |
|
794 } |
|
795 |
|
796 return retval; |
|
797 } |
|
798 |
1
|
799 int |
164
|
800 symbol_table::size (void) const |
1
|
801 { |
|
802 int count = 0; |
3013
|
803 |
|
804 for (unsigned int i = 0; i < table_size; i++) |
1
|
805 { |
|
806 symbol_record *ptr = table[i].next (); |
3013
|
807 |
530
|
808 while (ptr) |
1
|
809 { |
|
810 count++; |
|
811 ptr = ptr->next (); |
|
812 } |
|
813 } |
3013
|
814 |
1
|
815 return count; |
|
816 } |
|
817 |
3013
|
818 static bool |
3523
|
819 matches_patterns (const std::string& name, const string_vector& pats) |
1
|
820 { |
3013
|
821 int npats = pats.length (); |
1
|
822 |
3013
|
823 if (npats == 0) |
|
824 return true; |
|
825 |
|
826 glob_match pattern (pats); |
|
827 |
|
828 return pattern.match (name); |
1
|
829 } |
|
830 |
3013
|
831 Array<symbol_record *> |
3355
|
832 symbol_table::symbol_list (const string_vector& pats, |
3013
|
833 unsigned int type, unsigned int scope) const |
1
|
834 { |
3355
|
835 int count = 0; |
3013
|
836 |
1
|
837 int n = size (); |
3013
|
838 |
3355
|
839 Array<symbol_record *> symbols (n); |
|
840 |
1
|
841 if (n == 0) |
3355
|
842 return symbols; |
3013
|
843 |
|
844 for (unsigned int i = 0; i < table_size; i++) |
1
|
845 { |
|
846 symbol_record *ptr = table[i].next (); |
3013
|
847 |
530
|
848 while (ptr) |
1
|
849 { |
|
850 assert (count < n); |
867
|
851 |
2893
|
852 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
853 |
2893
|
854 unsigned int my_type = ptr->type (); |
867
|
855 |
3523
|
856 std::string my_name = ptr->name (); |
867
|
857 |
|
858 if ((type & my_type) && (scope & my_scope) |
3013
|
859 && matches_patterns (my_name, pats)) |
|
860 symbols(count++) = ptr; |
605
|
861 |
1
|
862 ptr = ptr->next (); |
|
863 } |
|
864 } |
1755
|
865 |
|
866 symbols.resize (count); |
1
|
867 |
3013
|
868 return symbols; |
|
869 } |
|
870 |
|
871 string_vector |
3355
|
872 symbol_table::name_list (const string_vector& pats, bool sort, |
3013
|
873 unsigned int type, unsigned int scope) const |
|
874 { |
3355
|
875 Array<symbol_record *> symbols = symbol_list (pats, type, scope); |
3013
|
876 |
|
877 string_vector names; |
|
878 |
|
879 int n = symbols.length (); |
|
880 |
|
881 if (n > 0) |
|
882 { |
|
883 names.resize (n); |
|
884 |
|
885 for (int i = 0; i < n; i++) |
|
886 names[i] = symbols(i)->name (); |
|
887 } |
|
888 |
|
889 if (sort) |
|
890 names.qsort (); |
|
891 |
|
892 return names; |
|
893 } |
|
894 |
|
895 static int |
3145
|
896 maybe_list_cmp_fcn (const void *a_arg, const void *b_arg) |
3013
|
897 { |
3145
|
898 const symbol_record *a = *(X_CAST (const symbol_record **, a_arg)); |
|
899 const symbol_record *b = *(X_CAST (const symbol_record **, b_arg)); |
3013
|
900 |
3523
|
901 std::string a_nm = a->name (); |
|
902 std::string b_nm = b->name (); |
3145
|
903 |
|
904 return a_nm.compare (b_nm); |
3013
|
905 } |
1
|
906 |
3013
|
907 int |
|
908 symbol_table::maybe_list (const char *header, const string_vector& argv, |
3523
|
909 std::ostream& os, bool show_verbose, |
3013
|
910 unsigned type, unsigned scope) |
|
911 { |
|
912 int status = 0; |
|
913 |
|
914 if (show_verbose) |
|
915 { |
3355
|
916 Array<symbol_record *> symbols = symbol_list (argv, type, scope); |
3013
|
917 |
|
918 int len = symbols.length (); |
|
919 |
3355
|
920 if (len > 0) |
3013
|
921 { |
|
922 os << "\n" << header << "\n\n" |
|
923 << "prot type rows cols name\n" |
|
924 << "==== ==== ==== ==== ====\n"; |
|
925 |
|
926 symbols.qsort (maybe_list_cmp_fcn); |
|
927 |
|
928 for (int i = 0; i < len; i++) |
|
929 symbols(i)->print_symbol_info_line (os); |
|
930 |
|
931 status = 1; |
|
932 } |
|
933 } |
|
934 else |
|
935 { |
3355
|
936 string_vector symbols = name_list (argv, 1, type, scope); |
3013
|
937 |
3355
|
938 if (! symbols.empty ()) |
3013
|
939 { |
|
940 os << "\n" << header << "\n\n"; |
|
941 |
|
942 symbols.list_in_columns (os); |
|
943 |
|
944 status = 1; |
|
945 } |
|
946 } |
|
947 |
|
948 return status; |
1
|
949 } |
|
950 |
3355
|
951 Array<symbol_record *> |
3523
|
952 symbol_table::glob (const std::string& pat, unsigned int type, |
2893
|
953 unsigned int scope) const |
605
|
954 { |
3355
|
955 int count = 0; |
|
956 |
605
|
957 int n = size (); |
3355
|
958 |
|
959 Array<symbol_record *> symbols (n); |
|
960 |
605
|
961 if (n == 0) |
3355
|
962 return symbols; |
3013
|
963 |
|
964 for (unsigned int i = 0; i < table_size; i++) |
605
|
965 { |
|
966 symbol_record *ptr = table[i].next (); |
3013
|
967 |
605
|
968 while (ptr) |
|
969 { |
|
970 assert (count < n); |
|
971 |
2893
|
972 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
973 |
2893
|
974 unsigned int my_type = ptr->type (); |
605
|
975 |
1792
|
976 glob_match pattern (pat); |
1755
|
977 |
605
|
978 if ((type & my_type) && (scope & my_scope) |
1792
|
979 && pattern.match (ptr->name ())) |
605
|
980 { |
3355
|
981 symbols(count++) = ptr; |
605
|
982 } |
|
983 |
|
984 ptr = ptr->next (); |
|
985 } |
|
986 } |
3355
|
987 |
|
988 symbols.resize (count); |
605
|
989 |
|
990 return symbols; |
|
991 } |
|
992 |
220
|
993 void |
|
994 symbol_table::push_context (void) |
|
995 { |
3013
|
996 for (unsigned int i = 0; i < table_size; i++) |
220
|
997 { |
|
998 symbol_record *ptr = table[i].next (); |
|
999 |
530
|
1000 while (ptr) |
220
|
1001 { |
|
1002 ptr->push_context (); |
|
1003 ptr = ptr->next (); |
|
1004 } |
|
1005 } |
|
1006 } |
|
1007 |
|
1008 void |
|
1009 symbol_table::pop_context (void) |
|
1010 { |
3013
|
1011 for (unsigned int i = 0; i < table_size; i++) |
220
|
1012 { |
|
1013 symbol_record *ptr = table[i].next (); |
|
1014 |
530
|
1015 while (ptr) |
220
|
1016 { |
|
1017 ptr->pop_context (); |
|
1018 ptr = ptr->next (); |
|
1019 } |
|
1020 } |
|
1021 } |
|
1022 |
3013
|
1023 void |
3944
|
1024 symbol_table::print_info (std::ostream& os) const |
3013
|
1025 { |
|
1026 int count = 0; |
|
1027 int empty_chains = 0; |
|
1028 int max_chain_length = 0; |
|
1029 int min_chain_length = INT_MAX; |
|
1030 |
|
1031 for (unsigned int i = 0; i < table_size; i++) |
|
1032 { |
|
1033 int num_this_chain = 0; |
|
1034 |
|
1035 symbol_record *ptr = table[i].next (); |
|
1036 |
|
1037 if (ptr) |
3933
|
1038 os << "chain number " << i << ":\n"; |
3013
|
1039 else |
|
1040 { |
|
1041 empty_chains++; |
|
1042 min_chain_length = 0; |
|
1043 } |
|
1044 |
|
1045 while (ptr) |
|
1046 { |
|
1047 num_this_chain++; |
|
1048 |
3933
|
1049 os << " " << ptr->name () << "\n"; |
|
1050 |
|
1051 ptr->print_info (os, " "); |
3013
|
1052 |
|
1053 ptr = ptr->next (); |
|
1054 } |
|
1055 |
|
1056 count += num_this_chain; |
|
1057 |
|
1058 if (num_this_chain > max_chain_length) |
|
1059 max_chain_length = num_this_chain; |
|
1060 |
|
1061 if (num_this_chain < min_chain_length) |
|
1062 min_chain_length = num_this_chain; |
|
1063 |
|
1064 if (num_this_chain > 0) |
3933
|
1065 os << "\n"; |
3013
|
1066 } |
|
1067 |
3933
|
1068 os << "max chain length: " << max_chain_length << "\n"; |
|
1069 os << "min chain length: " << min_chain_length << "\n"; |
|
1070 os << "empty chains: " << empty_chains << "\n"; |
|
1071 os << "total chains: " << table_size << "\n"; |
|
1072 os << "total symbols: " << count << "\n"; |
3013
|
1073 } |
|
1074 |
1
|
1075 // Chris Torek's fave hash function. |
|
1076 |
|
1077 unsigned int |
3523
|
1078 symbol_table::hash (const std::string& str) |
1
|
1079 { |
2893
|
1080 unsigned int h = 0; |
3013
|
1081 |
2893
|
1082 for (unsigned int i = 0; i < str.length (); i++) |
1755
|
1083 h = h * 33 + str[i]; |
1
|
1084 |
3013
|
1085 return h & (table_size - 1); |
2790
|
1086 } |
|
1087 |
3308
|
1088 |
|
1089 static int |
|
1090 variables_can_hide_functions (void) |
|
1091 { |
|
1092 Vvariables_can_hide_functions |
|
1093 = check_preference ("variables_can_hide_functions"); |
|
1094 |
|
1095 return 0; |
|
1096 } |
|
1097 |
4238
|
1098 static int |
|
1099 debug_symtab_lookups (void) |
|
1100 { |
|
1101 Vdebug_symtab_lookups = check_preference ("debug_symtab_lookups"); |
|
1102 |
|
1103 return 0; |
|
1104 } |
|
1105 |
3308
|
1106 void |
|
1107 symbols_of_symtab (void) |
|
1108 { |
4233
|
1109 DEFVAR (variables_can_hide_functions, true, variables_can_hide_functions, |
3448
|
1110 "-*- texinfo -*-\n\ |
|
1111 @defvr variables_can_hide_functions\n\ |
|
1112 If the value of this variable is nonzero, assignments to variables may\n\ |
|
1113 hide previously defined functions of the same name. A negative value\n\ |
|
1114 will cause Octave to print a warning, but allow the operation.\n\ |
|
1115 @end defvr"); |
|
1116 |
4238
|
1117 DEFVAR (debug_symtab_lookups, false, debug_symtab_lookups, |
|
1118 "-*- texinfo -*-\n\ |
|
1119 @defvr debug_symtab_lookups\n\ |
|
1120 If the value of htis variable is nonzero, print debugging info when\n\ |
|
1121 searching for symbols in the symbol tables"); |
3308
|
1122 } |
|
1123 |
|
1124 |
1
|
1125 /* |
|
1126 ;;; Local Variables: *** |
|
1127 ;;; mode: C++ *** |
|
1128 ;;; End: *** |
|
1129 */ |