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