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