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