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 { |
3258
|
205 if (is_function () || is_constant ()) |
3308
|
206 { |
|
207 if (Vvariables_can_hide_functions) |
|
208 { |
|
209 push_def (new symbol_def ()); |
|
210 |
|
211 if (Vvariables_can_hide_functions < 0) |
|
212 warning ("variable `%s' hides function", nm.c_str ()); |
|
213 } |
|
214 else |
|
215 error ("variable `%s' hides function", nm.c_str ()); |
|
216 } |
195
|
217 |
3013
|
218 if (definition->type () == symbol_record::BUILTIN_VARIABLE) |
|
219 sym_type = symbol_record::BUILTIN_VARIABLE; |
1
|
220 |
3013
|
221 definition->define (v, sym_type); |
195
|
222 } |
|
223 } |
|
224 |
|
225 void |
3013
|
226 symbol_record::define_builtin_var (const octave_value& v) |
195
|
227 { |
3013
|
228 define (v, symbol_record::BUILTIN_VARIABLE); |
195
|
229 |
3013
|
230 if (chg_fcn) |
|
231 chg_fcn (); |
1
|
232 } |
|
233 |
3013
|
234 bool |
3258
|
235 symbol_record::define_builtin_const (const octave_value& v) |
1
|
236 { |
3013
|
237 bool retval = false; |
1
|
238 |
3258
|
239 if (! read_only_error ("redefine")) |
195
|
240 { |
3811
|
241 definition->define (v, symbol_record::BUILTIN_CONSTANT); |
2893
|
242 |
3013
|
243 retval = true; |
1
|
244 } |
|
245 |
2893
|
246 return retval; |
2390
|
247 } |
|
248 |
3013
|
249 bool |
|
250 symbol_record::define (octave_function *f, unsigned int sym_type) |
1
|
251 { |
3013
|
252 bool retval = false; |
2949
|
253 |
3013
|
254 if (! read_only_error ("redefine")) |
|
255 { |
|
256 octave_value tmp (f); |
|
257 |
|
258 replace_all_defs (new symbol_def (tmp, sym_type)); |
|
259 |
|
260 retval = true; |
|
261 } |
2949
|
262 |
|
263 return retval; |
1
|
264 } |
|
265 |
|
266 void |
195
|
267 symbol_record::clear (void) |
1
|
268 { |
195
|
269 if (linked_to_global) |
1
|
270 { |
3013
|
271 if (--definition->count <= 0) |
|
272 delete definition; |
|
273 |
|
274 definition = new symbol_def (); |
|
275 |
195
|
276 linked_to_global = 0; |
1
|
277 } |
2846
|
278 else if (! tagged_static) |
195
|
279 { |
3013
|
280 remove_top_def (); |
|
281 |
|
282 if (! definition) |
|
283 definition = new symbol_def (); |
195
|
284 } |
1
|
285 } |
|
286 |
|
287 void |
3013
|
288 symbol_record::alias (symbol_record *s, bool /* force */) |
1
|
289 { |
3013
|
290 chg_fcn = s->chg_fcn; |
195
|
291 |
3013
|
292 replace_all_defs (s->definition); |
|
293 |
|
294 definition->count++; |
1
|
295 } |
|
296 |
|
297 void |
|
298 symbol_record::mark_as_formal_parameter (void) |
|
299 { |
3013
|
300 if (is_linked_to_global ()) |
|
301 error ("can't mark global variable `%s' as function parameter", |
|
302 nm.c_str ()); |
|
303 else if (is_static ()) |
|
304 error ("can't mark static variable `%s' as function paraemter", |
|
305 nm.c_str ()); |
|
306 else |
|
307 formal_param = 1; |
1
|
308 } |
|
309 |
|
310 void |
195
|
311 symbol_record::mark_as_linked_to_global (void) |
122
|
312 { |
2975
|
313 if (is_formal_parameter ()) |
|
314 error ("can't make function parameter `%s' global", nm.c_str ()); |
|
315 else if (is_static ()) |
|
316 error ("can't make static variable `%s' global", nm.c_str ()); |
|
317 else |
|
318 linked_to_global = 1; |
122
|
319 } |
|
320 |
2846
|
321 void |
|
322 symbol_record::mark_as_static (void) |
|
323 { |
|
324 if (is_linked_to_global ()) |
2975
|
325 error ("can't make global variable `%s' static", nm.c_str ()); |
2846
|
326 else if (is_formal_parameter ()) |
2975
|
327 error ("can't make formal parameter `%s' static", nm.c_str ()); |
2846
|
328 else |
|
329 tagged_static = 1; |
|
330 } |
|
331 |
2856
|
332 bool |
3013
|
333 symbol_record::hides_fcn (void) const |
2846
|
334 { |
3013
|
335 bool retval = false; |
|
336 |
|
337 if (is_variable () && is_defined ()) |
|
338 { |
|
339 symbol_def *hidden_def = definition->next_elem; |
|
340 |
3325
|
341 if (hidden_def && hidden_def->is_user_function ()) |
3013
|
342 retval = true; |
|
343 } |
|
344 |
|
345 return retval; |
|
346 } |
|
347 |
|
348 bool |
|
349 symbol_record::hides_builtin (void) const |
|
350 { |
|
351 bool retval = false; |
|
352 |
|
353 if (is_variable () && is_defined ()) |
|
354 { |
|
355 symbol_def *hidden_def = definition->next_elem; |
|
356 |
3325
|
357 if (hidden_def && hidden_def->is_builtin_function ()) |
3013
|
358 retval = true; |
|
359 } |
|
360 |
|
361 return retval; |
2846
|
362 } |
|
363 |
2975
|
364 octave_value& |
|
365 symbol_record::variable_value (void) |
2390
|
366 { |
2975
|
367 static octave_value foo; |
2390
|
368 |
2975
|
369 return is_variable () ? def () : foo; |
2390
|
370 } |
|
371 |
3258
|
372 inline void |
|
373 symbol_record::link_to_builtin_variable (void) |
|
374 { |
|
375 symbol_record *tmp_sym = global_sym_tab->lookup (name ()); |
|
376 |
|
377 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
378 alias (tmp_sym); |
|
379 } |
|
380 |
|
381 |
2979
|
382 octave_lvalue |
2390
|
383 symbol_record::variable_reference (void) |
|
384 { |
3308
|
385 if (Vvariables_can_hide_functions <= 0 |
|
386 && (is_function () |
|
387 || (! is_defined () && is_valid_function (nm)))) |
|
388 { |
|
389 if (Vvariables_can_hide_functions < 0) |
|
390 warning ("variable `%s' hides function", nm.c_str ()); |
|
391 else |
|
392 { |
|
393 error ("variable `%s' hides function", nm.c_str ()); |
|
394 return octave_lvalue (); |
|
395 } |
|
396 } |
|
397 |
3258
|
398 if (is_function () || is_constant ()) |
2390
|
399 clear (); |
|
400 |
|
401 if (! is_defined ()) |
|
402 { |
|
403 if (! (is_formal_parameter () || is_linked_to_global ())) |
3258
|
404 link_to_builtin_variable (); |
2390
|
405 |
|
406 if (! is_defined ()) |
2975
|
407 { |
|
408 octave_value tmp; |
|
409 define (tmp); |
|
410 } |
2390
|
411 } |
|
412 |
3013
|
413 return octave_lvalue (&(def ()), chg_fcn); |
195
|
414 } |
|
415 |
220
|
416 void |
|
417 symbol_record::push_context (void) |
|
418 { |
2846
|
419 if (! is_static ()) |
|
420 { |
|
421 context.push (definition); |
3013
|
422 |
|
423 definition = new symbol_def (); |
395
|
424 |
2893
|
425 global_link_context.push (static_cast<unsigned int> (linked_to_global)); |
3013
|
426 |
2846
|
427 linked_to_global = 0; |
|
428 } |
220
|
429 } |
|
430 |
|
431 void |
|
432 symbol_record::pop_context (void) |
|
433 { |
1653
|
434 // It is possible for context to be empty if new symbols have been |
|
435 // inserted in the symbol table during recursive calls. This can |
|
436 // happen as a result of calls to eval() and feval(). |
220
|
437 |
1653
|
438 if (! context.empty ()) |
220
|
439 { |
3013
|
440 replace_all_defs (context.pop ()); |
1653
|
441 |
|
442 linked_to_global = global_link_context.pop (); |
220
|
443 } |
|
444 } |
|
445 |
3013
|
446 void |
3933
|
447 symbol_record::print_symbol_info_line (std::ostream& os) const |
3013
|
448 { |
|
449 os << (is_read_only () ? " r-" : " rw") |
|
450 << (is_eternal () ? "-" : "d") |
|
451 #if 0 |
|
452 << (hides_fcn () ? "f" : (hides_builtin () ? "F" : "-")) |
|
453 #endif |
|
454 << " " |
3548
|
455 << std::setiosflags (std::ios::left) << std::setw (24) |
|
456 << type_name () . c_str (); |
3013
|
457 |
3548
|
458 os << std::resetiosflags (std::ios::left); |
3013
|
459 |
|
460 int nr = rows (); |
|
461 int nc = columns (); |
|
462 |
|
463 if (nr < 0) |
|
464 os << " -"; |
|
465 else |
3548
|
466 os << std::setiosflags (std::ios::right) << std::setw (7) << nr; |
3013
|
467 |
|
468 if (nc < 0) |
|
469 os << " -"; |
|
470 else |
3548
|
471 os << std::setiosflags (std::ios::right) << std::setw (7) << nc; |
3013
|
472 |
3548
|
473 os << std::resetiosflags (std::ios::right); |
3013
|
474 |
|
475 os << " " << name () << "\n"; |
|
476 } |
|
477 |
3239
|
478 void |
3944
|
479 symbol_record::print_info (std::ostream& os, const std::string& prefix) const |
3239
|
480 { |
|
481 if (definition) |
3933
|
482 definition->print_info (os, prefix); |
3239
|
483 else |
3933
|
484 os << prefix << "symbol " << name () << " is undefined\n"; |
3239
|
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 * |
3523
|
544 symbol_table::lookup (const std::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 |
3523
|
573 symbol_table::rename (const std::string& old_name, const std::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 |
3523
|
630 symbol_table::clear (const std::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 |
3523
|
672 matches_patterns (const std::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 |
3523
|
709 std::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 |
3523
|
754 std::string a_nm = a->name (); |
|
755 std::string b_nm = b->name (); |
3145
|
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, |
3523
|
762 std::ostream& os, bool show_verbose, |
3013
|
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 *> |
3523
|
805 symbol_table::glob (const std::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 |
3944
|
877 symbol_table::print_info (std::ostream& os) const |
3013
|
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) |
3933
|
891 os << "chain number " << i << ":\n"; |
3013
|
892 else |
|
893 { |
|
894 empty_chains++; |
|
895 min_chain_length = 0; |
|
896 } |
|
897 |
|
898 while (ptr) |
|
899 { |
|
900 num_this_chain++; |
|
901 |
3933
|
902 os << " " << ptr->name () << "\n"; |
|
903 |
|
904 ptr->print_info (os, " "); |
3013
|
905 |
|
906 ptr = ptr->next (); |
|
907 } |
|
908 |
|
909 count += num_this_chain; |
|
910 |
|
911 if (num_this_chain > max_chain_length) |
|
912 max_chain_length = num_this_chain; |
|
913 |
|
914 if (num_this_chain < min_chain_length) |
|
915 min_chain_length = num_this_chain; |
|
916 |
|
917 if (num_this_chain > 0) |
3933
|
918 os << "\n"; |
3013
|
919 } |
|
920 |
3933
|
921 os << "max chain length: " << max_chain_length << "\n"; |
|
922 os << "min chain length: " << min_chain_length << "\n"; |
|
923 os << "empty chains: " << empty_chains << "\n"; |
|
924 os << "total chains: " << table_size << "\n"; |
|
925 os << "total symbols: " << count << "\n"; |
3013
|
926 } |
|
927 |
1
|
928 // Chris Torek's fave hash function. |
|
929 |
|
930 unsigned int |
3523
|
931 symbol_table::hash (const std::string& str) |
1
|
932 { |
2893
|
933 unsigned int h = 0; |
3013
|
934 |
2893
|
935 for (unsigned int i = 0; i < str.length (); i++) |
1755
|
936 h = h * 33 + str[i]; |
1
|
937 |
3013
|
938 return h & (table_size - 1); |
2790
|
939 } |
|
940 |
3308
|
941 |
|
942 static int |
|
943 variables_can_hide_functions (void) |
|
944 { |
|
945 Vvariables_can_hide_functions |
|
946 = check_preference ("variables_can_hide_functions"); |
|
947 |
|
948 return 0; |
|
949 } |
|
950 |
|
951 void |
|
952 symbols_of_symtab (void) |
|
953 { |
|
954 DEFVAR (variables_can_hide_functions, 1.0, variables_can_hide_functions, |
3448
|
955 "-*- texinfo -*-\n\ |
|
956 @defvr variables_can_hide_functions\n\ |
|
957 If the value of this variable is nonzero, assignments to variables may\n\ |
|
958 hide previously defined functions of the same name. A negative value\n\ |
|
959 will cause Octave to print a warning, but allow the operation.\n\ |
|
960 @end defvr"); |
|
961 |
3308
|
962 } |
|
963 |
|
964 |
1
|
965 /* |
|
966 ;;; Local Variables: *** |
|
967 ;;; mode: C++ *** |
|
968 ;;; End: *** |
|
969 */ |