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 |
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"; |
4208
|
67 else if (is_command ()) |
|
68 retval = "built-in command"; |
3355
|
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 |
2979
|
335 octave_lvalue |
2390
|
336 symbol_record::variable_reference (void) |
|
337 { |
4234
|
338 if ((Vvariables_can_hide_functions <= 0 || ! can_hide_function) |
3308
|
339 && (is_function () |
|
340 || (! is_defined () && is_valid_function (nm)))) |
|
341 { |
4234
|
342 if (Vvariables_can_hide_functions < 0 && can_hide_function) |
3308
|
343 warning ("variable `%s' hides function", nm.c_str ()); |
|
344 else |
|
345 { |
|
346 error ("variable `%s' hides function", nm.c_str ()); |
|
347 return octave_lvalue (); |
|
348 } |
|
349 } |
|
350 |
3258
|
351 if (is_function () || is_constant ()) |
2390
|
352 clear (); |
|
353 |
|
354 if (! is_defined ()) |
|
355 { |
|
356 if (! (is_formal_parameter () || is_linked_to_global ())) |
3258
|
357 link_to_builtin_variable (); |
2390
|
358 |
|
359 if (! is_defined ()) |
2975
|
360 { |
|
361 octave_value tmp; |
|
362 define (tmp); |
|
363 } |
2390
|
364 } |
|
365 |
3013
|
366 return octave_lvalue (&(def ()), chg_fcn); |
195
|
367 } |
|
368 |
220
|
369 void |
|
370 symbol_record::push_context (void) |
|
371 { |
2846
|
372 if (! is_static ()) |
|
373 { |
|
374 context.push (definition); |
3013
|
375 |
|
376 definition = new symbol_def (); |
395
|
377 |
2893
|
378 global_link_context.push (static_cast<unsigned int> (linked_to_global)); |
3013
|
379 |
2846
|
380 linked_to_global = 0; |
|
381 } |
220
|
382 } |
|
383 |
|
384 void |
|
385 symbol_record::pop_context (void) |
|
386 { |
1653
|
387 // It is possible for context to be empty if new symbols have been |
|
388 // inserted in the symbol table during recursive calls. This can |
|
389 // happen as a result of calls to eval() and feval(). |
220
|
390 |
1653
|
391 if (! context.empty ()) |
220
|
392 { |
4009
|
393 if (--definition->count <= 0) |
|
394 delete definition; |
|
395 |
4214
|
396 definition = context.top (); |
|
397 context.pop (); |
1653
|
398 |
4214
|
399 linked_to_global = global_link_context.top (); |
|
400 global_link_context.pop (); |
220
|
401 } |
|
402 } |
|
403 |
3013
|
404 void |
3933
|
405 symbol_record::print_symbol_info_line (std::ostream& os) const |
3013
|
406 { |
|
407 os << (is_read_only () ? " r-" : " rw") |
|
408 << (is_eternal () ? "-" : "d") |
|
409 << " " |
3548
|
410 << std::setiosflags (std::ios::left) << std::setw (24) |
|
411 << type_name () . c_str (); |
3013
|
412 |
3548
|
413 os << std::resetiosflags (std::ios::left); |
3013
|
414 |
|
415 int nr = rows (); |
|
416 int nc = columns (); |
|
417 |
|
418 if (nr < 0) |
|
419 os << " -"; |
|
420 else |
3548
|
421 os << std::setiosflags (std::ios::right) << std::setw (7) << nr; |
3013
|
422 |
|
423 if (nc < 0) |
|
424 os << " -"; |
|
425 else |
3548
|
426 os << std::setiosflags (std::ios::right) << std::setw (7) << nc; |
3013
|
427 |
3548
|
428 os << std::resetiosflags (std::ios::right); |
3013
|
429 |
|
430 os << " " << name () << "\n"; |
|
431 } |
|
432 |
3239
|
433 void |
3944
|
434 symbol_record::print_info (std::ostream& os, const std::string& prefix) const |
3239
|
435 { |
|
436 if (definition) |
3933
|
437 definition->print_info (os, prefix); |
3239
|
438 else |
3933
|
439 os << prefix << "symbol " << name () << " is undefined\n"; |
3239
|
440 } |
|
441 |
3013
|
442 bool |
2791
|
443 symbol_record::read_only_error (const char *action) |
195
|
444 { |
|
445 if (is_read_only ()) |
|
446 { |
3258
|
447 if (is_variable () || is_constant ()) |
3013
|
448 ::error ("can't %s read-only constant `%s'", action, nm.c_str ()); |
195
|
449 else if (is_function ()) |
3013
|
450 ::error ("can't %s read-only function `%s'", action, nm.c_str ()); |
1045
|
451 else |
3013
|
452 ::error ("can't %s read-only symbol `%s'", action, nm.c_str ()); |
195
|
453 |
3013
|
454 return true; |
195
|
455 } |
|
456 else |
3013
|
457 return false; |
195
|
458 } |
|
459 |
767
|
460 // A symbol table. |
1
|
461 |
|
462 symbol_record * |
3523
|
463 symbol_table::lookup (const std::string& nm, bool insert, bool warn) |
1
|
464 { |
3013
|
465 unsigned int index = hash (nm); |
1
|
466 |
|
467 symbol_record *ptr = table[index].next (); |
|
468 |
530
|
469 while (ptr) |
1
|
470 { |
1755
|
471 if (ptr->name () == nm) |
1
|
472 return ptr; |
3013
|
473 |
1
|
474 ptr = ptr->next (); |
|
475 } |
|
476 |
|
477 if (insert) |
|
478 { |
3013
|
479 symbol_record *sr = new symbol_record (nm, table[index].next ()); |
|
480 |
|
481 table[index].chain (sr); |
|
482 |
|
483 return sr; |
1
|
484 } |
|
485 else if (warn) |
3356
|
486 warning ("lookup: symbol `%s' not found", nm.c_str ()); |
1
|
487 |
530
|
488 return 0; |
1
|
489 } |
|
490 |
|
491 void |
3523
|
492 symbol_table::rename (const std::string& old_name, const std::string& new_name) |
572
|
493 { |
3013
|
494 unsigned int index = hash (old_name); |
572
|
495 |
|
496 symbol_record *prev = &table[index]; |
|
497 symbol_record *ptr = prev->next (); |
|
498 |
|
499 while (ptr) |
|
500 { |
1755
|
501 if (ptr->name () == old_name) |
572
|
502 { |
|
503 ptr->rename (new_name); |
|
504 |
2791
|
505 if (! error_state) |
|
506 { |
|
507 prev->chain (ptr->next ()); |
|
508 |
3013
|
509 index = hash (new_name); |
3125
|
510 ptr->chain (table[index].next ()); |
2791
|
511 table[index].chain (ptr); |
|
512 |
|
513 return; |
|
514 } |
|
515 |
|
516 break; |
572
|
517 } |
|
518 |
|
519 prev = ptr; |
|
520 ptr = ptr->next (); |
|
521 } |
|
522 |
1755
|
523 error ("unable to rename `%s' to `%s'", old_name.c_str (), |
|
524 new_name.c_str ()); |
572
|
525 } |
|
526 |
4009
|
527 // XXX FIXME XXX -- it would be nice to eliminate a lot of the |
|
528 // following duplicate code. |
|
529 |
572
|
530 void |
4009
|
531 symbol_table::clear (void) |
|
532 { |
|
533 for (unsigned int i = 0; i < table_size; i++) |
|
534 { |
|
535 symbol_record *ptr = table[i].next (); |
|
536 |
|
537 while (ptr) |
|
538 { |
|
539 ptr->clear (); |
|
540 |
|
541 ptr = ptr->next (); |
|
542 } |
|
543 } |
|
544 } |
|
545 |
|
546 void |
|
547 symbol_table::clear_variables (void) |
|
548 { |
|
549 for (unsigned int i = 0; i < table_size; i++) |
|
550 { |
|
551 symbol_record *ptr = table[i].next (); |
|
552 |
|
553 while (ptr) |
|
554 { |
|
555 if (ptr->is_user_variable ()) |
|
556 ptr->clear (); |
|
557 |
|
558 ptr = ptr->next (); |
|
559 } |
|
560 } |
|
561 } |
|
562 |
|
563 // Really only clear functions that can be reloaded. |
|
564 |
|
565 void |
|
566 symbol_table::clear_functions (void) |
1
|
567 { |
3013
|
568 for (unsigned int i = 0; i < table_size; i++) |
1
|
569 { |
169
|
570 symbol_record *ptr = table[i].next (); |
1
|
571 |
530
|
572 while (ptr) |
169
|
573 { |
4009
|
574 if (ptr->is_user_function () || ptr->is_dld_function ()) |
|
575 ptr->clear (); |
|
576 |
|
577 ptr = ptr->next (); |
|
578 } |
|
579 } |
|
580 } |
|
581 |
|
582 void |
|
583 symbol_table::clear_globals (void) |
|
584 { |
|
585 for (unsigned int i = 0; i < table_size; i++) |
|
586 { |
|
587 symbol_record *ptr = table[i].next (); |
|
588 |
|
589 while (ptr) |
|
590 { |
|
591 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
592 ptr->clear (); |
195
|
593 |
169
|
594 ptr = ptr->next (); |
1
|
595 } |
|
596 } |
|
597 } |
|
598 |
3013
|
599 bool |
4009
|
600 symbol_table::clear (const std::string& nm) |
|
601 { |
|
602 unsigned int index = hash (nm); |
|
603 |
|
604 symbol_record *ptr = table[index].next (); |
|
605 |
|
606 while (ptr) |
|
607 { |
|
608 if (ptr->name () == nm) |
|
609 { |
|
610 ptr->clear (); |
|
611 return true; |
|
612 } |
|
613 ptr = ptr->next (); |
|
614 } |
|
615 |
|
616 return false; |
|
617 } |
|
618 |
|
619 bool |
|
620 symbol_table::clear_variable (const std::string& nm) |
|
621 { |
|
622 unsigned int index = hash (nm); |
|
623 |
|
624 symbol_record *ptr = table[index].next (); |
|
625 |
|
626 while (ptr) |
|
627 { |
|
628 if (ptr->name () == nm && ptr->is_user_variable ()) |
|
629 { |
|
630 ptr->clear (); |
|
631 return true; |
|
632 } |
|
633 ptr = ptr->next (); |
|
634 } |
|
635 |
|
636 return false; |
|
637 } |
|
638 |
|
639 bool |
|
640 symbol_table::clear_global (const std::string& nm) |
1
|
641 { |
3013
|
642 unsigned int index = hash (nm); |
1
|
643 |
195
|
644 symbol_record *ptr = table[index].next (); |
1
|
645 |
530
|
646 while (ptr) |
1
|
647 { |
1755
|
648 if (ptr->name () == nm |
4009
|
649 && ptr->is_user_variable () |
|
650 && ptr->is_linked_to_global ()) |
|
651 { |
|
652 ptr->clear (); |
|
653 return true; |
|
654 } |
|
655 ptr = ptr->next (); |
|
656 } |
|
657 |
|
658 return false; |
|
659 } |
|
660 |
|
661 // Really only clear functions that can be reloaded. |
|
662 |
|
663 bool |
|
664 symbol_table::clear_function (const std::string& nm) |
|
665 { |
|
666 unsigned int index = hash (nm); |
|
667 |
|
668 symbol_record *ptr = table[index].next (); |
|
669 |
|
670 while (ptr) |
|
671 { |
|
672 if (ptr->name () == nm |
|
673 && (ptr->is_user_function () || ptr->is_dld_function ())) |
1
|
674 { |
195
|
675 ptr->clear (); |
3013
|
676 return true; |
1
|
677 } |
195
|
678 ptr = ptr->next (); |
1
|
679 } |
|
680 |
3013
|
681 return false; |
1
|
682 } |
|
683 |
4009
|
684 bool |
|
685 symbol_table::clear_variable_pattern (const std::string& pat) |
|
686 { |
|
687 bool retval = false; |
|
688 |
|
689 for (unsigned int i = 0; i < table_size; i++) |
|
690 { |
|
691 symbol_record *ptr = table[i].next (); |
|
692 |
|
693 while (ptr) |
|
694 { |
|
695 if (ptr->is_user_variable ()) |
|
696 { |
|
697 glob_match pattern (pat); |
|
698 |
|
699 if (pattern.match (ptr->name ())) |
|
700 { |
|
701 ptr->clear (); |
|
702 |
|
703 retval = true; |
|
704 } |
|
705 } |
|
706 |
|
707 ptr = ptr->next (); |
|
708 } |
|
709 } |
|
710 |
|
711 return retval; |
|
712 } |
|
713 |
|
714 bool |
|
715 symbol_table::clear_global_pattern (const std::string& pat) |
|
716 { |
|
717 bool retval = false; |
|
718 |
|
719 for (unsigned int i = 0; i < table_size; i++) |
|
720 { |
|
721 symbol_record *ptr = table[i].next (); |
|
722 |
|
723 while (ptr) |
|
724 { |
|
725 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
726 { |
|
727 glob_match pattern (pat); |
|
728 |
|
729 if (pattern.match (ptr->name ())) |
|
730 { |
|
731 ptr->clear (); |
|
732 |
|
733 retval = true; |
|
734 } |
|
735 } |
|
736 |
|
737 ptr = ptr->next (); |
|
738 } |
|
739 } |
|
740 |
|
741 return retval; |
|
742 } |
|
743 |
|
744 // Really only clear functions that can be reloaded. |
|
745 |
|
746 bool |
|
747 symbol_table::clear_function_pattern (const std::string& pat) |
|
748 { |
|
749 bool retval = false; |
|
750 |
|
751 for (unsigned int i = 0; i < table_size; i++) |
|
752 { |
|
753 symbol_record *ptr = table[i].next (); |
|
754 |
|
755 while (ptr) |
|
756 { |
|
757 if (ptr->is_user_function () || ptr->is_dld_function ()) |
|
758 { |
|
759 glob_match pattern (pat); |
|
760 |
|
761 if (pattern.match (ptr->name ())) |
|
762 { |
|
763 ptr->clear (); |
|
764 |
|
765 retval = true; |
|
766 } |
|
767 } |
|
768 |
|
769 ptr = ptr->next (); |
|
770 } |
|
771 } |
|
772 |
|
773 return retval; |
|
774 } |
|
775 |
1
|
776 int |
164
|
777 symbol_table::size (void) const |
1
|
778 { |
|
779 int count = 0; |
3013
|
780 |
|
781 for (unsigned int i = 0; i < table_size; i++) |
1
|
782 { |
|
783 symbol_record *ptr = table[i].next (); |
3013
|
784 |
530
|
785 while (ptr) |
1
|
786 { |
|
787 count++; |
|
788 ptr = ptr->next (); |
|
789 } |
|
790 } |
3013
|
791 |
1
|
792 return count; |
|
793 } |
|
794 |
3013
|
795 static bool |
3523
|
796 matches_patterns (const std::string& name, const string_vector& pats) |
1
|
797 { |
3013
|
798 int npats = pats.length (); |
1
|
799 |
3013
|
800 if (npats == 0) |
|
801 return true; |
|
802 |
|
803 glob_match pattern (pats); |
|
804 |
|
805 return pattern.match (name); |
1
|
806 } |
|
807 |
3013
|
808 Array<symbol_record *> |
3355
|
809 symbol_table::symbol_list (const string_vector& pats, |
3013
|
810 unsigned int type, unsigned int scope) const |
1
|
811 { |
3355
|
812 int count = 0; |
3013
|
813 |
1
|
814 int n = size (); |
3013
|
815 |
3355
|
816 Array<symbol_record *> symbols (n); |
|
817 |
1
|
818 if (n == 0) |
3355
|
819 return symbols; |
3013
|
820 |
|
821 for (unsigned int i = 0; i < table_size; i++) |
1
|
822 { |
|
823 symbol_record *ptr = table[i].next (); |
3013
|
824 |
530
|
825 while (ptr) |
1
|
826 { |
|
827 assert (count < n); |
867
|
828 |
2893
|
829 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
830 |
2893
|
831 unsigned int my_type = ptr->type (); |
867
|
832 |
3523
|
833 std::string my_name = ptr->name (); |
867
|
834 |
|
835 if ((type & my_type) && (scope & my_scope) |
3013
|
836 && matches_patterns (my_name, pats)) |
|
837 symbols(count++) = ptr; |
605
|
838 |
1
|
839 ptr = ptr->next (); |
|
840 } |
|
841 } |
1755
|
842 |
|
843 symbols.resize (count); |
1
|
844 |
3013
|
845 return symbols; |
|
846 } |
|
847 |
|
848 string_vector |
3355
|
849 symbol_table::name_list (const string_vector& pats, bool sort, |
3013
|
850 unsigned int type, unsigned int scope) const |
|
851 { |
3355
|
852 Array<symbol_record *> symbols = symbol_list (pats, type, scope); |
3013
|
853 |
|
854 string_vector names; |
|
855 |
|
856 int n = symbols.length (); |
|
857 |
|
858 if (n > 0) |
|
859 { |
|
860 names.resize (n); |
|
861 |
|
862 for (int i = 0; i < n; i++) |
|
863 names[i] = symbols(i)->name (); |
|
864 } |
|
865 |
|
866 if (sort) |
|
867 names.qsort (); |
|
868 |
|
869 return names; |
|
870 } |
|
871 |
|
872 static int |
3145
|
873 maybe_list_cmp_fcn (const void *a_arg, const void *b_arg) |
3013
|
874 { |
3145
|
875 const symbol_record *a = *(X_CAST (const symbol_record **, a_arg)); |
|
876 const symbol_record *b = *(X_CAST (const symbol_record **, b_arg)); |
3013
|
877 |
3523
|
878 std::string a_nm = a->name (); |
|
879 std::string b_nm = b->name (); |
3145
|
880 |
|
881 return a_nm.compare (b_nm); |
3013
|
882 } |
1
|
883 |
3013
|
884 int |
|
885 symbol_table::maybe_list (const char *header, const string_vector& argv, |
3523
|
886 std::ostream& os, bool show_verbose, |
3013
|
887 unsigned type, unsigned scope) |
|
888 { |
|
889 int status = 0; |
|
890 |
|
891 if (show_verbose) |
|
892 { |
3355
|
893 Array<symbol_record *> symbols = symbol_list (argv, type, scope); |
3013
|
894 |
|
895 int len = symbols.length (); |
|
896 |
3355
|
897 if (len > 0) |
3013
|
898 { |
|
899 os << "\n" << header << "\n\n" |
|
900 << "prot type rows cols name\n" |
|
901 << "==== ==== ==== ==== ====\n"; |
|
902 |
|
903 symbols.qsort (maybe_list_cmp_fcn); |
|
904 |
|
905 for (int i = 0; i < len; i++) |
|
906 symbols(i)->print_symbol_info_line (os); |
|
907 |
|
908 status = 1; |
|
909 } |
|
910 } |
|
911 else |
|
912 { |
3355
|
913 string_vector symbols = name_list (argv, 1, type, scope); |
3013
|
914 |
3355
|
915 if (! symbols.empty ()) |
3013
|
916 { |
|
917 os << "\n" << header << "\n\n"; |
|
918 |
|
919 symbols.list_in_columns (os); |
|
920 |
|
921 status = 1; |
|
922 } |
|
923 } |
|
924 |
|
925 return status; |
1
|
926 } |
|
927 |
3355
|
928 Array<symbol_record *> |
3523
|
929 symbol_table::glob (const std::string& pat, unsigned int type, |
2893
|
930 unsigned int scope) const |
605
|
931 { |
3355
|
932 int count = 0; |
|
933 |
605
|
934 int n = size (); |
3355
|
935 |
|
936 Array<symbol_record *> symbols (n); |
|
937 |
605
|
938 if (n == 0) |
3355
|
939 return symbols; |
3013
|
940 |
|
941 for (unsigned int i = 0; i < table_size; i++) |
605
|
942 { |
|
943 symbol_record *ptr = table[i].next (); |
3013
|
944 |
605
|
945 while (ptr) |
|
946 { |
|
947 assert (count < n); |
|
948 |
2893
|
949 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
950 |
2893
|
951 unsigned int my_type = ptr->type (); |
605
|
952 |
1792
|
953 glob_match pattern (pat); |
1755
|
954 |
605
|
955 if ((type & my_type) && (scope & my_scope) |
1792
|
956 && pattern.match (ptr->name ())) |
605
|
957 { |
3355
|
958 symbols(count++) = ptr; |
605
|
959 } |
|
960 |
|
961 ptr = ptr->next (); |
|
962 } |
|
963 } |
3355
|
964 |
|
965 symbols.resize (count); |
605
|
966 |
|
967 return symbols; |
|
968 } |
|
969 |
220
|
970 void |
|
971 symbol_table::push_context (void) |
|
972 { |
3013
|
973 for (unsigned int i = 0; i < table_size; i++) |
220
|
974 { |
|
975 symbol_record *ptr = table[i].next (); |
|
976 |
530
|
977 while (ptr) |
220
|
978 { |
|
979 ptr->push_context (); |
|
980 ptr = ptr->next (); |
|
981 } |
|
982 } |
|
983 } |
|
984 |
|
985 void |
|
986 symbol_table::pop_context (void) |
|
987 { |
3013
|
988 for (unsigned int i = 0; i < table_size; i++) |
220
|
989 { |
|
990 symbol_record *ptr = table[i].next (); |
|
991 |
530
|
992 while (ptr) |
220
|
993 { |
|
994 ptr->pop_context (); |
|
995 ptr = ptr->next (); |
|
996 } |
|
997 } |
|
998 } |
|
999 |
3013
|
1000 void |
3944
|
1001 symbol_table::print_info (std::ostream& os) const |
3013
|
1002 { |
|
1003 int count = 0; |
|
1004 int empty_chains = 0; |
|
1005 int max_chain_length = 0; |
|
1006 int min_chain_length = INT_MAX; |
|
1007 |
|
1008 for (unsigned int i = 0; i < table_size; i++) |
|
1009 { |
|
1010 int num_this_chain = 0; |
|
1011 |
|
1012 symbol_record *ptr = table[i].next (); |
|
1013 |
|
1014 if (ptr) |
3933
|
1015 os << "chain number " << i << ":\n"; |
3013
|
1016 else |
|
1017 { |
|
1018 empty_chains++; |
|
1019 min_chain_length = 0; |
|
1020 } |
|
1021 |
|
1022 while (ptr) |
|
1023 { |
|
1024 num_this_chain++; |
|
1025 |
3933
|
1026 os << " " << ptr->name () << "\n"; |
|
1027 |
|
1028 ptr->print_info (os, " "); |
3013
|
1029 |
|
1030 ptr = ptr->next (); |
|
1031 } |
|
1032 |
|
1033 count += num_this_chain; |
|
1034 |
|
1035 if (num_this_chain > max_chain_length) |
|
1036 max_chain_length = num_this_chain; |
|
1037 |
|
1038 if (num_this_chain < min_chain_length) |
|
1039 min_chain_length = num_this_chain; |
|
1040 |
|
1041 if (num_this_chain > 0) |
3933
|
1042 os << "\n"; |
3013
|
1043 } |
|
1044 |
3933
|
1045 os << "max chain length: " << max_chain_length << "\n"; |
|
1046 os << "min chain length: " << min_chain_length << "\n"; |
|
1047 os << "empty chains: " << empty_chains << "\n"; |
|
1048 os << "total chains: " << table_size << "\n"; |
|
1049 os << "total symbols: " << count << "\n"; |
3013
|
1050 } |
|
1051 |
1
|
1052 // Chris Torek's fave hash function. |
|
1053 |
|
1054 unsigned int |
3523
|
1055 symbol_table::hash (const std::string& str) |
1
|
1056 { |
2893
|
1057 unsigned int h = 0; |
3013
|
1058 |
2893
|
1059 for (unsigned int i = 0; i < str.length (); i++) |
1755
|
1060 h = h * 33 + str[i]; |
1
|
1061 |
3013
|
1062 return h & (table_size - 1); |
2790
|
1063 } |
|
1064 |
3308
|
1065 |
|
1066 static int |
|
1067 variables_can_hide_functions (void) |
|
1068 { |
|
1069 Vvariables_can_hide_functions |
|
1070 = check_preference ("variables_can_hide_functions"); |
|
1071 |
|
1072 return 0; |
|
1073 } |
|
1074 |
|
1075 void |
|
1076 symbols_of_symtab (void) |
|
1077 { |
4233
|
1078 DEFVAR (variables_can_hide_functions, true, variables_can_hide_functions, |
3448
|
1079 "-*- texinfo -*-\n\ |
|
1080 @defvr variables_can_hide_functions\n\ |
|
1081 If the value of this variable is nonzero, assignments to variables may\n\ |
|
1082 hide previously defined functions of the same name. A negative value\n\ |
|
1083 will cause Octave to print a warning, but allow the operation.\n\ |
|
1084 @end defvr"); |
|
1085 |
3308
|
1086 } |
|
1087 |
|
1088 |
1
|
1089 /* |
|
1090 ;;; Local Variables: *** |
|
1091 ;;; mode: C++ *** |
|
1092 ;;; End: *** |
|
1093 */ |