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 |
|
35 #include <iomanip.h> |
1962
|
36 |
2926
|
37 #include "glob-match.h" |
1755
|
38 #include "str-vec.h" |
|
39 |
3308
|
40 #include "defun.h" |
1352
|
41 #include "error.h" |
2979
|
42 #include "oct-lvalue.h" |
2975
|
43 #include "ov.h" |
3013
|
44 #include "pager.h" |
1755
|
45 #include "symtab.h" |
1
|
46 #include "utils.h" |
1352
|
47 #include "variables.h" |
1
|
48 |
3308
|
49 // Should variables be allowed to hide functions of the same name? A |
|
50 // positive value means yes. A negative value means yes, but print a |
|
51 // warning message. Zero means it should be considered an error. |
|
52 static int Vvariables_can_hide_functions; |
|
53 |
3013
|
54 octave_allocator |
|
55 symbol_record::symbol_def::allocator (sizeof (symbol_record::symbol_def)); |
195
|
56 |
3355
|
57 #define SYMBOL_DEF symbol_record::symbol_def |
|
58 |
|
59 string |
|
60 SYMBOL_DEF::type_as_string (void) const |
|
61 { |
|
62 string retval = "<unknown type>"; |
|
63 |
|
64 if (is_user_variable ()) |
|
65 retval = "user-defined variable"; |
|
66 else if (is_text_function ()) |
|
67 retval = "built-in text function"; |
|
68 else if (is_mapper_function ()) |
|
69 retval = "built-in mapper function"; |
|
70 else if (is_user_function ()) |
|
71 retval = "user-defined function"; |
|
72 else if (is_builtin_constant ()) |
|
73 retval = "built-in constant"; |
|
74 else if (is_builtin_variable ()) |
|
75 retval = "built-in variable"; |
|
76 else if (is_builtin_function ()) |
|
77 retval = "built-in function"; |
|
78 else if (is_dld_function ()) |
|
79 retval = "dynamically-linked function"; |
|
80 |
|
81 return retval; |
|
82 } |
|
83 |
|
84 string |
|
85 SYMBOL_DEF::which (const string& name) |
|
86 { |
|
87 string retval; |
|
88 |
|
89 if (is_user_function () || is_dld_function ()) |
|
90 { |
|
91 octave_function *defn = definition.function_value (); |
|
92 |
|
93 if (defn) |
|
94 retval = defn->fcn_file_name (); |
|
95 } |
|
96 else |
|
97 retval = name + " is a " + type_as_string (); |
|
98 |
|
99 return retval; |
|
100 } |
|
101 |
3239
|
102 void |
3355
|
103 SYMBOL_DEF::which (ostream& os, const string& name) |
|
104 { |
|
105 os << name; |
|
106 |
|
107 if (is_user_function () || is_dld_function ()) |
|
108 { |
|
109 octave_function *defn = definition.function_value (); |
|
110 |
|
111 string fn = defn ? defn->fcn_file_name () : string (); |
|
112 |
|
113 if (! fn.empty ()) |
|
114 { |
|
115 os << " is the " << type_as_string () << " from the file\n" |
|
116 << fn << "\n"; |
|
117 |
|
118 return; |
|
119 } |
|
120 } |
|
121 |
|
122 os << " is a " << type_as_string () << "\n"; |
|
123 } |
|
124 |
|
125 void |
|
126 SYMBOL_DEF::dump_symbol_info (void) |
3239
|
127 { |
|
128 octave_stdout << "symbol_def::count: " << count << "\n"; |
|
129 octave_stdout << "def.type_name(): " << definition.type_name () << "\n"; |
3240
|
130 octave_stdout << "def.count(): " << definition.get_count () << "\n"; |
3239
|
131 } |
|
132 |
767
|
133 // Individual records in a symbol table. |
|
134 |
3013
|
135 // XXX FIXME XXX -- there are lots of places below where we should |
|
136 // probably be temporarily ignoring interrupts. |
1
|
137 |
572
|
138 void |
1755
|
139 symbol_record::rename (const string& new_name) |
572
|
140 { |
2791
|
141 if (! read_only_error ("rename")) |
|
142 nm = new_name; |
572
|
143 } |
|
144 |
3013
|
145 void |
|
146 symbol_record::define (const octave_value& v, unsigned int sym_type) |
195
|
147 { |
3013
|
148 if (! (is_variable () && read_only_error ("redefine"))) |
|
149 { |
3258
|
150 if (is_function () || is_constant ()) |
3308
|
151 { |
|
152 if (Vvariables_can_hide_functions) |
|
153 { |
|
154 push_def (new symbol_def ()); |
|
155 |
|
156 if (Vvariables_can_hide_functions < 0) |
|
157 warning ("variable `%s' hides function", nm.c_str ()); |
|
158 } |
|
159 else |
|
160 error ("variable `%s' hides function", nm.c_str ()); |
|
161 } |
195
|
162 |
3013
|
163 if (definition->type () == symbol_record::BUILTIN_VARIABLE) |
|
164 sym_type = symbol_record::BUILTIN_VARIABLE; |
1
|
165 |
3013
|
166 definition->define (v, sym_type); |
195
|
167 } |
|
168 } |
|
169 |
|
170 void |
3013
|
171 symbol_record::define_builtin_var (const octave_value& v) |
195
|
172 { |
3013
|
173 define (v, symbol_record::BUILTIN_VARIABLE); |
195
|
174 |
3013
|
175 if (chg_fcn) |
|
176 chg_fcn (); |
1
|
177 } |
|
178 |
3013
|
179 bool |
3258
|
180 symbol_record::define_builtin_const (const octave_value& v) |
1
|
181 { |
3013
|
182 bool retval = false; |
1
|
183 |
3258
|
184 if (! read_only_error ("redefine")) |
195
|
185 { |
3258
|
186 replace_all_defs (new symbol_def (v, symbol_record::BUILTIN_CONSTANT)); |
2893
|
187 |
3013
|
188 retval = true; |
1
|
189 } |
|
190 |
2893
|
191 return retval; |
2390
|
192 } |
|
193 |
3013
|
194 bool |
|
195 symbol_record::define (octave_function *f, unsigned int sym_type) |
1
|
196 { |
3013
|
197 bool retval = false; |
2949
|
198 |
3013
|
199 if (! read_only_error ("redefine")) |
|
200 { |
|
201 octave_value tmp (f); |
|
202 |
|
203 replace_all_defs (new symbol_def (tmp, sym_type)); |
|
204 |
|
205 retval = true; |
|
206 } |
2949
|
207 |
|
208 return retval; |
1
|
209 } |
|
210 |
|
211 void |
195
|
212 symbol_record::clear (void) |
1
|
213 { |
195
|
214 if (linked_to_global) |
1
|
215 { |
3013
|
216 if (--definition->count <= 0) |
|
217 delete definition; |
|
218 |
|
219 definition = new symbol_def (); |
|
220 |
195
|
221 linked_to_global = 0; |
1
|
222 } |
2846
|
223 else if (! tagged_static) |
195
|
224 { |
3013
|
225 remove_top_def (); |
|
226 |
|
227 if (! definition) |
|
228 definition = new symbol_def (); |
195
|
229 } |
1
|
230 } |
|
231 |
|
232 void |
3013
|
233 symbol_record::alias (symbol_record *s, bool /* force */) |
1
|
234 { |
3013
|
235 chg_fcn = s->chg_fcn; |
195
|
236 |
3013
|
237 replace_all_defs (s->definition); |
|
238 |
|
239 definition->count++; |
1
|
240 } |
|
241 |
|
242 void |
|
243 symbol_record::mark_as_formal_parameter (void) |
|
244 { |
3013
|
245 if (is_linked_to_global ()) |
|
246 error ("can't mark global variable `%s' as function parameter", |
|
247 nm.c_str ()); |
|
248 else if (is_static ()) |
|
249 error ("can't mark static variable `%s' as function paraemter", |
|
250 nm.c_str ()); |
|
251 else |
|
252 formal_param = 1; |
1
|
253 } |
|
254 |
|
255 void |
195
|
256 symbol_record::mark_as_linked_to_global (void) |
122
|
257 { |
2975
|
258 if (is_formal_parameter ()) |
|
259 error ("can't make function parameter `%s' global", nm.c_str ()); |
|
260 else if (is_static ()) |
|
261 error ("can't make static variable `%s' global", nm.c_str ()); |
|
262 else |
|
263 linked_to_global = 1; |
122
|
264 } |
|
265 |
2846
|
266 void |
|
267 symbol_record::mark_as_static (void) |
|
268 { |
|
269 if (is_linked_to_global ()) |
2975
|
270 error ("can't make global variable `%s' static", nm.c_str ()); |
2846
|
271 else if (is_formal_parameter ()) |
2975
|
272 error ("can't make formal parameter `%s' static", nm.c_str ()); |
2846
|
273 else |
|
274 tagged_static = 1; |
|
275 } |
|
276 |
2856
|
277 bool |
3013
|
278 symbol_record::hides_fcn (void) const |
2846
|
279 { |
3013
|
280 bool retval = false; |
|
281 |
|
282 if (is_variable () && is_defined ()) |
|
283 { |
|
284 symbol_def *hidden_def = definition->next_elem; |
|
285 |
3325
|
286 if (hidden_def && hidden_def->is_user_function ()) |
3013
|
287 retval = true; |
|
288 } |
|
289 |
|
290 return retval; |
|
291 } |
|
292 |
|
293 bool |
|
294 symbol_record::hides_builtin (void) const |
|
295 { |
|
296 bool retval = false; |
|
297 |
|
298 if (is_variable () && is_defined ()) |
|
299 { |
|
300 symbol_def *hidden_def = definition->next_elem; |
|
301 |
3325
|
302 if (hidden_def && hidden_def->is_builtin_function ()) |
3013
|
303 retval = true; |
|
304 } |
|
305 |
|
306 return retval; |
2846
|
307 } |
|
308 |
2975
|
309 octave_value& |
|
310 symbol_record::variable_value (void) |
2390
|
311 { |
2975
|
312 static octave_value foo; |
2390
|
313 |
2975
|
314 return is_variable () ? def () : foo; |
2390
|
315 } |
|
316 |
3258
|
317 inline void |
|
318 symbol_record::link_to_builtin_variable (void) |
|
319 { |
|
320 symbol_record *tmp_sym = global_sym_tab->lookup (name ()); |
|
321 |
|
322 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
323 alias (tmp_sym); |
|
324 } |
|
325 |
|
326 |
2979
|
327 octave_lvalue |
2390
|
328 symbol_record::variable_reference (void) |
|
329 { |
3308
|
330 if (Vvariables_can_hide_functions <= 0 |
|
331 && (is_function () |
|
332 || (! is_defined () && is_valid_function (nm)))) |
|
333 { |
|
334 if (Vvariables_can_hide_functions < 0) |
|
335 warning ("variable `%s' hides function", nm.c_str ()); |
|
336 else |
|
337 { |
|
338 error ("variable `%s' hides function", nm.c_str ()); |
|
339 return octave_lvalue (); |
|
340 } |
|
341 } |
|
342 |
3258
|
343 if (is_function () || is_constant ()) |
2390
|
344 clear (); |
|
345 |
|
346 if (! is_defined ()) |
|
347 { |
|
348 if (! (is_formal_parameter () || is_linked_to_global ())) |
3258
|
349 link_to_builtin_variable (); |
2390
|
350 |
|
351 if (! is_defined ()) |
2975
|
352 { |
|
353 octave_value tmp; |
|
354 define (tmp); |
|
355 } |
2390
|
356 } |
|
357 |
3013
|
358 return octave_lvalue (&(def ()), chg_fcn); |
195
|
359 } |
|
360 |
220
|
361 void |
|
362 symbol_record::push_context (void) |
|
363 { |
2846
|
364 if (! is_static ()) |
|
365 { |
|
366 context.push (definition); |
3013
|
367 |
|
368 definition = new symbol_def (); |
395
|
369 |
2893
|
370 global_link_context.push (static_cast<unsigned int> (linked_to_global)); |
3013
|
371 |
2846
|
372 linked_to_global = 0; |
|
373 } |
220
|
374 } |
|
375 |
|
376 void |
|
377 symbol_record::pop_context (void) |
|
378 { |
1653
|
379 // It is possible for context to be empty if new symbols have been |
|
380 // inserted in the symbol table during recursive calls. This can |
|
381 // happen as a result of calls to eval() and feval(). |
220
|
382 |
1653
|
383 if (! context.empty ()) |
220
|
384 { |
3013
|
385 replace_all_defs (context.pop ()); |
1653
|
386 |
|
387 linked_to_global = global_link_context.pop (); |
220
|
388 } |
|
389 } |
|
390 |
3013
|
391 void |
|
392 symbol_record::print_symbol_info_line (ostream& os) |
|
393 { |
|
394 os << (is_read_only () ? " r-" : " rw") |
|
395 << (is_eternal () ? "-" : "d") |
|
396 #if 0 |
|
397 << (hides_fcn () ? "f" : (hides_builtin () ? "F" : "-")) |
|
398 #endif |
|
399 << " " |
|
400 << setiosflags (ios::left) << setw (24) << type_name () . c_str (); |
|
401 |
|
402 os << resetiosflags (ios::left); |
|
403 |
|
404 int nr = rows (); |
|
405 int nc = columns (); |
|
406 |
|
407 if (nr < 0) |
|
408 os << " -"; |
|
409 else |
|
410 os << setiosflags (ios::right) << setw (7) << nr; |
|
411 |
|
412 if (nc < 0) |
|
413 os << " -"; |
|
414 else |
|
415 os << setiosflags (ios::right) << setw (7) << nc; |
|
416 |
|
417 os << resetiosflags (ios::right); |
|
418 |
|
419 os << " " << name () << "\n"; |
|
420 } |
|
421 |
3239
|
422 void |
|
423 symbol_record::dump_symbol_info (void) |
|
424 { |
|
425 if (definition) |
|
426 definition->dump_symbol_info (); |
|
427 else |
|
428 octave_stdout << "symbol " << name () << " is undefined\n"; |
|
429 } |
|
430 |
3013
|
431 bool |
2791
|
432 symbol_record::read_only_error (const char *action) |
195
|
433 { |
|
434 if (is_read_only ()) |
|
435 { |
3258
|
436 if (is_variable () || is_constant ()) |
3013
|
437 ::error ("can't %s read-only constant `%s'", action, nm.c_str ()); |
195
|
438 else if (is_function ()) |
3013
|
439 ::error ("can't %s read-only function `%s'", action, nm.c_str ()); |
1045
|
440 else |
3013
|
441 ::error ("can't %s read-only symbol `%s'", action, nm.c_str ()); |
195
|
442 |
3013
|
443 return true; |
195
|
444 } |
|
445 else |
3013
|
446 return false; |
195
|
447 } |
|
448 |
|
449 void |
|
450 symbol_record::push_def (symbol_def *sd) |
|
451 { |
530
|
452 if (! sd) |
195
|
453 return; |
|
454 |
3013
|
455 assert (definition == 0 || definition->next_elem == 0); |
|
456 |
195
|
457 sd->next_elem = definition; |
3013
|
458 |
195
|
459 definition = sd; |
|
460 } |
|
461 |
3013
|
462 void |
|
463 symbol_record::remove_top_def (void) |
195
|
464 { |
|
465 symbol_def *top = definition; |
|
466 |
3013
|
467 definition = definition->next_elem; |
2404
|
468 |
3013
|
469 if (--top->count <= 0) |
|
470 delete top; |
195
|
471 } |
|
472 |
3013
|
473 void |
|
474 symbol_record::replace_all_defs (symbol_def *sd) |
195
|
475 { |
3013
|
476 while (definition) |
|
477 remove_top_def (); |
2404
|
478 |
3325
|
479 if (! sd) |
|
480 sd = new symbol_def (); |
|
481 |
3013
|
482 push_def (sd); |
195
|
483 } |
|
484 |
767
|
485 // A symbol table. |
1
|
486 |
|
487 symbol_record * |
2856
|
488 symbol_table::lookup (const string& nm, bool insert, bool warn) |
1
|
489 { |
3013
|
490 unsigned int index = hash (nm); |
1
|
491 |
|
492 symbol_record *ptr = table[index].next (); |
|
493 |
530
|
494 while (ptr) |
1
|
495 { |
1755
|
496 if (ptr->name () == nm) |
1
|
497 return ptr; |
3013
|
498 |
1
|
499 ptr = ptr->next (); |
|
500 } |
|
501 |
|
502 if (insert) |
|
503 { |
3013
|
504 symbol_record *sr = new symbol_record (nm, table[index].next ()); |
|
505 |
|
506 table[index].chain (sr); |
|
507 |
|
508 return sr; |
1
|
509 } |
|
510 else if (warn) |
1755
|
511 warning ("lookup: symbol`%s' not found", nm.c_str ()); |
1
|
512 |
530
|
513 return 0; |
1
|
514 } |
|
515 |
|
516 void |
1755
|
517 symbol_table::rename (const string& old_name, const string& new_name) |
572
|
518 { |
3013
|
519 unsigned int index = hash (old_name); |
572
|
520 |
|
521 symbol_record *prev = &table[index]; |
|
522 symbol_record *ptr = prev->next (); |
|
523 |
|
524 while (ptr) |
|
525 { |
1755
|
526 if (ptr->name () == old_name) |
572
|
527 { |
|
528 ptr->rename (new_name); |
|
529 |
2791
|
530 if (! error_state) |
|
531 { |
|
532 prev->chain (ptr->next ()); |
|
533 |
3013
|
534 index = hash (new_name); |
3125
|
535 ptr->chain (table[index].next ()); |
2791
|
536 table[index].chain (ptr); |
|
537 |
|
538 return; |
|
539 } |
|
540 |
|
541 break; |
572
|
542 } |
|
543 |
|
544 prev = ptr; |
|
545 ptr = ptr->next (); |
|
546 } |
|
547 |
1755
|
548 error ("unable to rename `%s' to `%s'", old_name.c_str (), |
|
549 new_name.c_str ()); |
572
|
550 } |
|
551 |
|
552 void |
2856
|
553 symbol_table::clear (bool clear_user_functions) |
1
|
554 { |
3013
|
555 for (unsigned int i = 0; i < table_size; i++) |
1
|
556 { |
169
|
557 symbol_record *ptr = table[i].next (); |
1
|
558 |
530
|
559 while (ptr) |
169
|
560 { |
195
|
561 if (ptr->is_user_variable () |
3325
|
562 || (clear_user_functions |
|
563 && (ptr->is_user_function () || ptr->is_dld_function ()))) |
195
|
564 { |
|
565 ptr->clear (); |
|
566 } |
|
567 |
169
|
568 ptr = ptr->next (); |
1
|
569 } |
|
570 } |
|
571 } |
|
572 |
3013
|
573 bool |
2856
|
574 symbol_table::clear (const string& nm, bool clear_user_functions) |
1
|
575 { |
3013
|
576 unsigned int index = hash (nm); |
1
|
577 |
195
|
578 symbol_record *ptr = table[index].next (); |
1
|
579 |
530
|
580 while (ptr) |
1
|
581 { |
1755
|
582 if (ptr->name () == nm |
195
|
583 && (ptr->is_user_variable () |
3325
|
584 || (clear_user_functions |
|
585 && (ptr->is_user_function () || ptr->is_dld_function ())))) |
1
|
586 { |
195
|
587 ptr->clear (); |
3013
|
588 return true; |
1
|
589 } |
195
|
590 ptr = ptr->next (); |
1
|
591 } |
|
592 |
3013
|
593 return false; |
1
|
594 } |
|
595 |
|
596 int |
164
|
597 symbol_table::size (void) const |
1
|
598 { |
|
599 int count = 0; |
3013
|
600 |
|
601 for (unsigned int i = 0; i < table_size; i++) |
1
|
602 { |
|
603 symbol_record *ptr = table[i].next (); |
3013
|
604 |
530
|
605 while (ptr) |
1
|
606 { |
|
607 count++; |
|
608 ptr = ptr->next (); |
|
609 } |
|
610 } |
3013
|
611 |
1
|
612 return count; |
|
613 } |
|
614 |
3013
|
615 static bool |
|
616 matches_patterns (const string& name, const string_vector& pats) |
1
|
617 { |
3013
|
618 int npats = pats.length (); |
1
|
619 |
3013
|
620 if (npats == 0) |
|
621 return true; |
|
622 |
|
623 glob_match pattern (pats); |
|
624 |
|
625 return pattern.match (name); |
1
|
626 } |
|
627 |
3013
|
628 Array<symbol_record *> |
3355
|
629 symbol_table::symbol_list (const string_vector& pats, |
3013
|
630 unsigned int type, unsigned int scope) const |
1
|
631 { |
3355
|
632 int count = 0; |
3013
|
633 |
1
|
634 int n = size (); |
3013
|
635 |
3355
|
636 Array<symbol_record *> symbols (n); |
|
637 |
1
|
638 if (n == 0) |
3355
|
639 return symbols; |
3013
|
640 |
|
641 for (unsigned int i = 0; i < table_size; i++) |
1
|
642 { |
|
643 symbol_record *ptr = table[i].next (); |
3013
|
644 |
530
|
645 while (ptr) |
1
|
646 { |
|
647 assert (count < n); |
867
|
648 |
2893
|
649 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
650 |
2893
|
651 unsigned int my_type = ptr->type (); |
867
|
652 |
1755
|
653 string my_name = ptr->name (); |
867
|
654 |
|
655 if ((type & my_type) && (scope & my_scope) |
3013
|
656 && matches_patterns (my_name, pats)) |
|
657 symbols(count++) = ptr; |
605
|
658 |
1
|
659 ptr = ptr->next (); |
|
660 } |
|
661 } |
1755
|
662 |
|
663 symbols.resize (count); |
1
|
664 |
3013
|
665 return symbols; |
|
666 } |
|
667 |
|
668 string_vector |
3355
|
669 symbol_table::name_list (const string_vector& pats, bool sort, |
3013
|
670 unsigned int type, unsigned int scope) const |
|
671 { |
3355
|
672 Array<symbol_record *> symbols = symbol_list (pats, type, scope); |
3013
|
673 |
|
674 string_vector names; |
|
675 |
|
676 int n = symbols.length (); |
|
677 |
|
678 if (n > 0) |
|
679 { |
|
680 names.resize (n); |
|
681 |
|
682 for (int i = 0; i < n; i++) |
|
683 names[i] = symbols(i)->name (); |
|
684 } |
|
685 |
|
686 if (sort) |
|
687 names.qsort (); |
|
688 |
|
689 return names; |
|
690 } |
|
691 |
|
692 static int |
3145
|
693 maybe_list_cmp_fcn (const void *a_arg, const void *b_arg) |
3013
|
694 { |
3145
|
695 const symbol_record *a = *(X_CAST (const symbol_record **, a_arg)); |
|
696 const symbol_record *b = *(X_CAST (const symbol_record **, b_arg)); |
3013
|
697 |
3145
|
698 string a_nm = a->name (); |
|
699 string b_nm = b->name (); |
|
700 |
|
701 return a_nm.compare (b_nm); |
3013
|
702 } |
1
|
703 |
3013
|
704 int |
|
705 symbol_table::maybe_list (const char *header, const string_vector& argv, |
|
706 ostream& os, bool show_verbose, |
|
707 unsigned type, unsigned scope) |
|
708 { |
|
709 int status = 0; |
|
710 |
|
711 if (show_verbose) |
|
712 { |
3355
|
713 Array<symbol_record *> symbols = symbol_list (argv, type, scope); |
3013
|
714 |
|
715 int len = symbols.length (); |
|
716 |
3355
|
717 if (len > 0) |
3013
|
718 { |
|
719 os << "\n" << header << "\n\n" |
|
720 << "prot type rows cols name\n" |
|
721 << "==== ==== ==== ==== ====\n"; |
|
722 |
|
723 symbols.qsort (maybe_list_cmp_fcn); |
|
724 |
|
725 for (int i = 0; i < len; i++) |
|
726 symbols(i)->print_symbol_info_line (os); |
|
727 |
|
728 status = 1; |
|
729 } |
|
730 } |
|
731 else |
|
732 { |
3355
|
733 string_vector symbols = name_list (argv, 1, type, scope); |
3013
|
734 |
3355
|
735 if (! symbols.empty ()) |
3013
|
736 { |
|
737 os << "\n" << header << "\n\n"; |
|
738 |
|
739 symbols.list_in_columns (os); |
|
740 |
|
741 status = 1; |
|
742 } |
|
743 } |
|
744 |
|
745 return status; |
1
|
746 } |
|
747 |
3355
|
748 Array<symbol_record *> |
|
749 symbol_table::glob (const string& pat, unsigned int type, |
2893
|
750 unsigned int scope) const |
605
|
751 { |
3355
|
752 int count = 0; |
|
753 |
605
|
754 int n = size (); |
3355
|
755 |
|
756 Array<symbol_record *> symbols (n); |
|
757 |
605
|
758 if (n == 0) |
3355
|
759 return symbols; |
3013
|
760 |
|
761 for (unsigned int i = 0; i < table_size; i++) |
605
|
762 { |
|
763 symbol_record *ptr = table[i].next (); |
3013
|
764 |
605
|
765 while (ptr) |
|
766 { |
|
767 assert (count < n); |
|
768 |
2893
|
769 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
770 |
2893
|
771 unsigned int my_type = ptr->type (); |
605
|
772 |
1792
|
773 glob_match pattern (pat); |
1755
|
774 |
605
|
775 if ((type & my_type) && (scope & my_scope) |
1792
|
776 && pattern.match (ptr->name ())) |
605
|
777 { |
3355
|
778 symbols(count++) = ptr; |
605
|
779 } |
|
780 |
|
781 ptr = ptr->next (); |
|
782 } |
|
783 } |
3355
|
784 |
|
785 symbols.resize (count); |
605
|
786 |
|
787 return symbols; |
|
788 } |
|
789 |
220
|
790 void |
|
791 symbol_table::push_context (void) |
|
792 { |
3013
|
793 for (unsigned int i = 0; i < table_size; i++) |
220
|
794 { |
|
795 symbol_record *ptr = table[i].next (); |
|
796 |
530
|
797 while (ptr) |
220
|
798 { |
|
799 ptr->push_context (); |
|
800 ptr = ptr->next (); |
|
801 } |
|
802 } |
|
803 } |
|
804 |
|
805 void |
|
806 symbol_table::pop_context (void) |
|
807 { |
3013
|
808 for (unsigned int i = 0; i < table_size; i++) |
220
|
809 { |
|
810 symbol_record *ptr = table[i].next (); |
|
811 |
530
|
812 while (ptr) |
220
|
813 { |
|
814 ptr->pop_context (); |
|
815 ptr = ptr->next (); |
|
816 } |
|
817 } |
|
818 } |
|
819 |
3013
|
820 void |
|
821 symbol_table::print_stats (void) |
|
822 { |
|
823 int count = 0; |
|
824 int empty_chains = 0; |
|
825 int max_chain_length = 0; |
|
826 int min_chain_length = INT_MAX; |
|
827 |
|
828 for (unsigned int i = 0; i < table_size; i++) |
|
829 { |
|
830 int num_this_chain = 0; |
|
831 |
|
832 symbol_record *ptr = table[i].next (); |
|
833 |
|
834 if (ptr) |
|
835 octave_stdout << "chain number " << i << ":\n"; |
|
836 else |
|
837 { |
|
838 empty_chains++; |
|
839 min_chain_length = 0; |
|
840 } |
|
841 |
|
842 while (ptr) |
|
843 { |
|
844 num_this_chain++; |
|
845 |
|
846 octave_stdout << " " << ptr->name () << "\n"; |
|
847 |
|
848 ptr = ptr->next (); |
|
849 } |
|
850 |
|
851 count += num_this_chain; |
|
852 |
|
853 if (num_this_chain > max_chain_length) |
|
854 max_chain_length = num_this_chain; |
|
855 |
|
856 if (num_this_chain < min_chain_length) |
|
857 min_chain_length = num_this_chain; |
|
858 |
|
859 if (num_this_chain > 0) |
|
860 octave_stdout << "\n"; |
|
861 } |
|
862 |
|
863 octave_stdout << "max chain length: " << max_chain_length << "\n"; |
|
864 octave_stdout << "min chain length: " << min_chain_length << "\n"; |
|
865 octave_stdout << "empty chains: " << empty_chains << "\n"; |
|
866 octave_stdout << "total chains: " << table_size << "\n"; |
|
867 octave_stdout << "total symbols: " << count << "\n"; |
|
868 } |
|
869 |
1
|
870 // Chris Torek's fave hash function. |
|
871 |
|
872 unsigned int |
1755
|
873 symbol_table::hash (const string& str) |
1
|
874 { |
2893
|
875 unsigned int h = 0; |
3013
|
876 |
2893
|
877 for (unsigned int i = 0; i < str.length (); i++) |
1755
|
878 h = h * 33 + str[i]; |
1
|
879 |
3013
|
880 return h & (table_size - 1); |
2790
|
881 } |
|
882 |
3308
|
883 |
|
884 static int |
|
885 variables_can_hide_functions (void) |
|
886 { |
|
887 Vvariables_can_hide_functions |
|
888 = check_preference ("variables_can_hide_functions"); |
|
889 |
|
890 return 0; |
|
891 } |
|
892 |
|
893 void |
|
894 symbols_of_symtab (void) |
|
895 { |
|
896 DEFVAR (variables_can_hide_functions, 1.0, variables_can_hide_functions, |
|
897 "Should variables be allowed to hide functions of the same name?"); |
|
898 } |
|
899 |
|
900 |
1
|
901 /* |
|
902 ;;; Local Variables: *** |
|
903 ;;; mode: C++ *** |
|
904 ;;; End: *** |
|
905 */ |