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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
3013
|
28 #include <cassert> |
1962
|
29 #include <cctype> |
3013
|
30 #include <climits> |
4914
|
31 #include <cstdio> |
3013
|
32 |
3503
|
33 #include <iomanip> |
|
34 #include <fstream> |
1962
|
35 |
2926
|
36 #include "glob-match.h" |
1755
|
37 #include "str-vec.h" |
|
38 |
3308
|
39 #include "defun.h" |
1352
|
40 #include "error.h" |
2979
|
41 #include "oct-lvalue.h" |
2975
|
42 #include "ov.h" |
3356
|
43 #include "pt-pr-code.h" |
1755
|
44 #include "symtab.h" |
1
|
45 #include "utils.h" |
1352
|
46 #include "variables.h" |
1
|
47 |
4913
|
48 #include "gripes.h" |
|
49 #include "lo-mappers.h" |
|
50 |
|
51 #include "parse.h" |
|
52 |
4238
|
53 unsigned long int symbol_table::symtab_count = 0; |
|
54 |
3308
|
55 // Should variables be allowed to hide functions of the same name? A |
|
56 // positive value means yes. A negative value means yes, but print a |
|
57 // warning message. Zero means it should be considered an error. |
|
58 static int Vvariables_can_hide_functions; |
|
59 |
4238
|
60 // Nonzero means we print debugging info about symbol table lookups. |
|
61 static int Vdebug_symtab_lookups; |
|
62 |
4913
|
63 // Defines layout for the whos/who -long command |
|
64 std::string Vwhos_line_format; |
|
65 |
3013
|
66 octave_allocator |
|
67 symbol_record::symbol_def::allocator (sizeof (symbol_record::symbol_def)); |
195
|
68 |
3355
|
69 #define SYMBOL_DEF symbol_record::symbol_def |
|
70 |
3536
|
71 std::string |
3355
|
72 SYMBOL_DEF::type_as_string (void) const |
|
73 { |
3523
|
74 std::string retval = "<unknown type>"; |
3355
|
75 |
|
76 if (is_user_variable ()) |
|
77 retval = "user-defined variable"; |
4208
|
78 else if (is_command ()) |
|
79 retval = "built-in command"; |
3355
|
80 else if (is_mapper_function ()) |
|
81 retval = "built-in mapper function"; |
|
82 else if (is_user_function ()) |
|
83 retval = "user-defined function"; |
|
84 else if (is_builtin_constant ()) |
|
85 retval = "built-in constant"; |
|
86 else if (is_builtin_variable ()) |
|
87 retval = "built-in variable"; |
|
88 else if (is_builtin_function ()) |
|
89 retval = "built-in function"; |
|
90 else if (is_dld_function ()) |
|
91 retval = "dynamically-linked function"; |
|
92 |
|
93 return retval; |
|
94 } |
|
95 |
3356
|
96 void |
3523
|
97 SYMBOL_DEF::type (std::ostream& os, const std::string& name, bool pr_type_info, |
3356
|
98 bool quiet, bool pr_orig_txt) |
|
99 { |
|
100 if (is_user_function ()) |
|
101 { |
|
102 octave_function *defn = definition.function_value (); |
|
103 |
3523
|
104 std::string fn = defn ? defn->fcn_file_name () : std::string (); |
3356
|
105 |
|
106 if (pr_orig_txt && ! fn.empty ()) |
|
107 { |
3544
|
108 std::ifstream fs (fn.c_str (), std::ios::in); |
3356
|
109 |
|
110 if (fs) |
|
111 { |
|
112 if (pr_type_info && ! quiet) |
|
113 os << name << " is the " << type_as_string () |
|
114 << " defined from: " << fn << "\n\n"; |
|
115 |
|
116 char ch; |
|
117 |
|
118 while (fs.get (ch)) |
|
119 os << ch; |
|
120 } |
|
121 else |
|
122 os << "unable to open `" << fn << "' for reading!\n"; |
|
123 } |
|
124 else |
|
125 { |
|
126 if (pr_type_info && ! quiet) |
|
127 os << name << " is a " << type_as_string () << ":\n\n"; |
|
128 |
|
129 tree_print_code tpc (os, "", pr_orig_txt); |
|
130 |
|
131 defn->accept (tpc); |
|
132 } |
|
133 } |
|
134 else if (is_user_variable () |
|
135 || is_builtin_variable () |
|
136 || is_builtin_constant ()) |
|
137 { |
|
138 if (pr_type_info && ! quiet) |
|
139 os << name << " is a " << type_as_string () << "\n"; |
|
140 |
|
141 definition.print_raw (os, true); |
|
142 |
|
143 if (pr_type_info) |
|
144 os << "\n"; |
|
145 } |
|
146 else |
|
147 os << name << " is a " << type_as_string () << "\n"; |
|
148 } |
|
149 |
3536
|
150 std::string |
3523
|
151 SYMBOL_DEF::which (const std::string& name) |
3355
|
152 { |
3523
|
153 std::string retval; |
3355
|
154 |
|
155 if (is_user_function () || is_dld_function ()) |
|
156 { |
|
157 octave_function *defn = definition.function_value (); |
|
158 |
|
159 if (defn) |
|
160 retval = defn->fcn_file_name (); |
|
161 } |
|
162 else |
|
163 retval = name + " is a " + type_as_string (); |
|
164 |
|
165 return retval; |
|
166 } |
|
167 |
3239
|
168 void |
3523
|
169 SYMBOL_DEF::which (std::ostream& os, const std::string& name) |
3355
|
170 { |
|
171 os << name; |
|
172 |
|
173 if (is_user_function () || is_dld_function ()) |
|
174 { |
|
175 octave_function *defn = definition.function_value (); |
|
176 |
3523
|
177 std::string fn = defn ? defn->fcn_file_name () : std::string (); |
3355
|
178 |
|
179 if (! fn.empty ()) |
|
180 { |
|
181 os << " is the " << type_as_string () << " from the file\n" |
|
182 << fn << "\n"; |
|
183 |
|
184 return; |
|
185 } |
|
186 } |
|
187 |
|
188 os << " is a " << type_as_string () << "\n"; |
|
189 } |
|
190 |
|
191 void |
3944
|
192 SYMBOL_DEF::print_info (std::ostream& os, const std::string& prefix) const |
3239
|
193 { |
3933
|
194 os << prefix << "symbol_def::count: " << count << "\n"; |
|
195 |
|
196 definition.print_info (os, prefix + " "); |
3239
|
197 } |
|
198 |
767
|
199 // Individual records in a symbol table. |
|
200 |
572
|
201 void |
3523
|
202 symbol_record::rename (const std::string& new_name) |
572
|
203 { |
2791
|
204 if (! read_only_error ("rename")) |
|
205 nm = new_name; |
572
|
206 } |
|
207 |
3013
|
208 void |
|
209 symbol_record::define (const octave_value& v, unsigned int sym_type) |
195
|
210 { |
3013
|
211 if (! (is_variable () && read_only_error ("redefine"))) |
|
212 { |
|
213 if (definition->type () == symbol_record::BUILTIN_VARIABLE) |
|
214 sym_type = symbol_record::BUILTIN_VARIABLE; |
1
|
215 |
3013
|
216 definition->define (v, sym_type); |
195
|
217 } |
|
218 } |
|
219 |
|
220 void |
3013
|
221 symbol_record::define_builtin_var (const octave_value& v) |
195
|
222 { |
3013
|
223 define (v, symbol_record::BUILTIN_VARIABLE); |
195
|
224 |
3013
|
225 if (chg_fcn) |
|
226 chg_fcn (); |
1
|
227 } |
|
228 |
3013
|
229 bool |
3258
|
230 symbol_record::define_builtin_const (const octave_value& v) |
1
|
231 { |
3013
|
232 bool retval = false; |
1
|
233 |
3258
|
234 if (! read_only_error ("redefine")) |
195
|
235 { |
3811
|
236 definition->define (v, symbol_record::BUILTIN_CONSTANT); |
2893
|
237 |
3013
|
238 retval = true; |
1
|
239 } |
|
240 |
2893
|
241 return retval; |
2390
|
242 } |
|
243 |
3013
|
244 bool |
|
245 symbol_record::define (octave_function *f, unsigned int sym_type) |
1
|
246 { |
3013
|
247 bool retval = false; |
2949
|
248 |
3013
|
249 if (! read_only_error ("redefine")) |
|
250 { |
5397
|
251 maybe_delete_def (); |
|
252 |
3013
|
253 octave_value tmp (f); |
|
254 |
5397
|
255 definition = new symbol_def (tmp, sym_type); |
3013
|
256 |
|
257 retval = true; |
|
258 } |
2949
|
259 |
|
260 return retval; |
1
|
261 } |
|
262 |
|
263 void |
195
|
264 symbol_record::clear (void) |
1
|
265 { |
4261
|
266 if (is_defined ()) |
1
|
267 { |
4261
|
268 if (! tagged_static) |
|
269 { |
5397
|
270 while (! aliases_to_clear.empty ()) |
|
271 { |
|
272 symbol_record *sr = aliases_to_clear.top (); |
|
273 aliases_to_clear.pop (); |
|
274 sr->clear (); |
|
275 } |
|
276 |
|
277 maybe_delete_def (); |
3013
|
278 |
4261
|
279 definition = new symbol_def (); |
|
280 } |
3013
|
281 |
4261
|
282 if (linked_to_global) |
|
283 linked_to_global = 0; |
|
284 } |
1
|
285 } |
|
286 |
|
287 void |
5397
|
288 symbol_record::alias (symbol_record *s, bool mark_to_clear) |
1
|
289 { |
3013
|
290 chg_fcn = s->chg_fcn; |
195
|
291 |
5397
|
292 maybe_delete_def (); |
4009
|
293 |
5397
|
294 if (mark_to_clear) |
|
295 s->push_alias_to_clear (this); |
|
296 |
|
297 definition = s->definition; |
3013
|
298 |
|
299 definition->count++; |
1
|
300 } |
|
301 |
|
302 void |
|
303 symbol_record::mark_as_formal_parameter (void) |
|
304 { |
3013
|
305 if (is_linked_to_global ()) |
|
306 error ("can't mark global variable `%s' as function parameter", |
|
307 nm.c_str ()); |
|
308 else if (is_static ()) |
|
309 error ("can't mark static variable `%s' as function paraemter", |
|
310 nm.c_str ()); |
|
311 else |
|
312 formal_param = 1; |
1
|
313 } |
|
314 |
|
315 void |
195
|
316 symbol_record::mark_as_linked_to_global (void) |
122
|
317 { |
2975
|
318 if (is_formal_parameter ()) |
|
319 error ("can't make function parameter `%s' global", nm.c_str ()); |
|
320 else if (is_static ()) |
|
321 error ("can't make static variable `%s' global", nm.c_str ()); |
|
322 else |
|
323 linked_to_global = 1; |
122
|
324 } |
|
325 |
2846
|
326 void |
|
327 symbol_record::mark_as_static (void) |
|
328 { |
|
329 if (is_linked_to_global ()) |
2975
|
330 error ("can't make global variable `%s' static", nm.c_str ()); |
2846
|
331 else if (is_formal_parameter ()) |
2975
|
332 error ("can't make formal parameter `%s' static", nm.c_str ()); |
2846
|
333 else |
|
334 tagged_static = 1; |
|
335 } |
|
336 |
2975
|
337 octave_value& |
|
338 symbol_record::variable_value (void) |
2390
|
339 { |
2975
|
340 static octave_value foo; |
2390
|
341 |
2975
|
342 return is_variable () ? def () : foo; |
2390
|
343 } |
|
344 |
3258
|
345 inline void |
|
346 symbol_record::link_to_builtin_variable (void) |
|
347 { |
4009
|
348 symbol_record *tmp_sym = fbi_sym_tab->lookup (name ()); |
3258
|
349 |
|
350 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
351 alias (tmp_sym); |
|
352 } |
|
353 |
2979
|
354 octave_lvalue |
2390
|
355 symbol_record::variable_reference (void) |
|
356 { |
4234
|
357 if ((Vvariables_can_hide_functions <= 0 || ! can_hide_function) |
3308
|
358 && (is_function () |
|
359 || (! is_defined () && is_valid_function (nm)))) |
|
360 { |
4234
|
361 if (Vvariables_can_hide_functions < 0 && can_hide_function) |
3308
|
362 warning ("variable `%s' hides function", nm.c_str ()); |
|
363 else |
|
364 { |
|
365 error ("variable `%s' hides function", nm.c_str ()); |
|
366 return octave_lvalue (); |
|
367 } |
|
368 } |
|
369 |
3258
|
370 if (is_function () || is_constant ()) |
2390
|
371 clear (); |
|
372 |
|
373 if (! is_defined ()) |
|
374 { |
|
375 if (! (is_formal_parameter () || is_linked_to_global ())) |
3258
|
376 link_to_builtin_variable (); |
2390
|
377 |
|
378 if (! is_defined ()) |
2975
|
379 { |
|
380 octave_value tmp; |
|
381 define (tmp); |
|
382 } |
2390
|
383 } |
|
384 |
3013
|
385 return octave_lvalue (&(def ()), chg_fcn); |
195
|
386 } |
|
387 |
220
|
388 void |
|
389 symbol_record::push_context (void) |
|
390 { |
2846
|
391 if (! is_static ()) |
|
392 { |
|
393 context.push (definition); |
3013
|
394 |
|
395 definition = new symbol_def (); |
395
|
396 |
2893
|
397 global_link_context.push (static_cast<unsigned int> (linked_to_global)); |
3013
|
398 |
2846
|
399 linked_to_global = 0; |
|
400 } |
220
|
401 } |
|
402 |
|
403 void |
|
404 symbol_record::pop_context (void) |
|
405 { |
1653
|
406 // It is possible for context to be empty if new symbols have been |
|
407 // inserted in the symbol table during recursive calls. This can |
|
408 // happen as a result of calls to eval() and feval(). |
220
|
409 |
1653
|
410 if (! context.empty ()) |
220
|
411 { |
5397
|
412 maybe_delete_def (); |
4009
|
413 |
4214
|
414 definition = context.top (); |
|
415 context.pop (); |
1653
|
416 |
4214
|
417 linked_to_global = global_link_context.top (); |
|
418 global_link_context.pop (); |
220
|
419 } |
|
420 } |
|
421 |
4914
|
422 // Calculate how much space needs to be reserved for the first part of |
|
423 // the dimensions string. For example, |
|
424 // |
|
425 // mat is a 12x3 matrix |
|
426 // ^^ => 2 columns |
|
427 |
4913
|
428 int |
4914
|
429 symbol_record::dimensions_string_req_first_space (int print_dims) const |
3013
|
430 { |
4913
|
431 int first_param_space = 0; |
|
432 |
4914
|
433 // Calculating dimensions. |
|
434 |
4913
|
435 std::string dim_str = ""; |
|
436 std::stringstream ss; |
5050
|
437 dim_vector dimensions = dims (); |
|
438 long dim = dimensions.length (); |
4913
|
439 |
|
440 first_param_space = (first_param_space >= 1 ? first_param_space : 1); |
|
441 |
4914
|
442 // Preparing dimension string. |
|
443 |
4913
|
444 if ((dim <= print_dims || print_dims < 0) && print_dims != 0) |
|
445 { |
4914
|
446 // Dimensions string must be printed like this: 2x3x4x2. |
|
447 |
4913
|
448 if (dim == 0 || dim == 1) |
4914
|
449 first_param_space = 1; // First parameter is 1. |
4913
|
450 else |
|
451 { |
|
452 ss << dimensions (0); |
4914
|
453 |
4913
|
454 dim_str = ss.str (); |
|
455 first_param_space = dim_str.length (); |
|
456 } |
|
457 } |
4914
|
458 else |
4913
|
459 { |
4914
|
460 // Printing dimension string as: a-D. |
|
461 |
4913
|
462 ss << dim; |
3013
|
463 |
4913
|
464 dim_str = ss.str (); |
|
465 first_param_space = dim_str.length (); |
|
466 } |
|
467 |
|
468 return first_param_space; |
|
469 } |
|
470 |
4914
|
471 // Calculate how much space needs to be reserved for the the |
|
472 // dimensions string. For example, |
|
473 // |
|
474 // mat is a 12x3 matrix |
|
475 // ^^^^ => 4 columns |
5050
|
476 // |
|
477 // XXX FIXME XXX -- why not just use the dim_vector::str () method? |
4914
|
478 |
4913
|
479 int |
|
480 symbol_record::dimensions_string_req_total_space (int print_dims) const |
|
481 { |
|
482 std::string dim_str = ""; |
|
483 std::stringstream ss; |
|
484 |
|
485 ss << make_dimensions_string (print_dims); |
|
486 dim_str = ss.str (); |
|
487 |
|
488 return dim_str.length (); |
|
489 } |
3013
|
490 |
4914
|
491 // Make the dimensions-string. For example: mat is a 2x3 matrix. |
|
492 // ^^^ |
5050
|
493 // |
|
494 // XXX FIXME XXX -- why not just use the dim_vector::str () method? |
4914
|
495 |
4913
|
496 std::string |
4914
|
497 symbol_record::make_dimensions_string (int print_dims) const |
4913
|
498 { |
4914
|
499 // Calculating dimensions. |
|
500 |
4913
|
501 std::string dim_str = ""; |
|
502 std::stringstream ss; |
5050
|
503 dim_vector dimensions = dims (); |
|
504 long dim = dimensions.length (); |
3013
|
505 |
4914
|
506 // Preparing dimension string. |
|
507 |
4913
|
508 if ((dim <= print_dims || print_dims < 0) && print_dims != 0) |
|
509 { |
|
510 // Only printing the dimension string as: axbxc... |
4914
|
511 |
4913
|
512 if (dim == 0) |
|
513 ss << "1x1"; |
|
514 else |
|
515 { |
4914
|
516 for (int i = 0; i < dim; i++) |
4913
|
517 { |
|
518 if (i == 0) |
|
519 { |
|
520 if (dim == 1) |
4914
|
521 { |
|
522 // Looks like this is not going to happen in |
|
523 // Octave, but ... |
|
524 |
|
525 ss << "1x" << dimensions (i); |
|
526 } |
4913
|
527 else |
|
528 ss << dimensions (i); |
|
529 } |
4914
|
530 else if (i < dim && dim != 1) |
4913
|
531 ss << "x" << dimensions (i); |
|
532 } |
|
533 } |
|
534 } |
4914
|
535 else |
4913
|
536 { |
4914
|
537 // Printing dimension string as: a-D. |
|
538 |
4913
|
539 ss << dim << "-D"; |
|
540 } |
|
541 |
|
542 dim_str = ss.str (); |
|
543 |
|
544 return dim_str; |
|
545 } |
|
546 |
4914
|
547 // Print a line of information on a given symbol. |
|
548 |
4913
|
549 void |
|
550 symbol_record::print_symbol_info_line (std::ostream& os, |
|
551 std::list<whos_parameter>& params) const |
|
552 { |
|
553 std::list<whos_parameter>::iterator i = params.begin (); |
|
554 while (i != params.end ()) |
|
555 { |
|
556 whos_parameter param = * i; |
|
557 |
|
558 if (param.command != '\0') |
|
559 { |
4914
|
560 // Do the actual printing. |
|
561 |
4913
|
562 switch (param.modifier) |
|
563 { |
|
564 case 'l': |
4914
|
565 os << std::setiosflags (std::ios::left) |
|
566 << std::setw (param.parameter_length); |
4913
|
567 break; |
|
568 |
|
569 case 'r': |
4914
|
570 os << std::setiosflags (std::ios::right) |
|
571 << std::setw (param.parameter_length); |
4913
|
572 break; |
|
573 |
|
574 case 'c': |
|
575 if (param.command == 's') |
|
576 { |
4914
|
577 int front = param.first_parameter_length |
|
578 - dimensions_string_req_first_space (param.dimensions); |
|
579 int back = param.parameter_length |
|
580 - dimensions_string_req_total_space (param.dimensions) |
|
581 - front; |
4913
|
582 front = (front > 0) ? front : 0; |
|
583 back = (back > 0) ? back : 0; |
3013
|
584 |
4914
|
585 os << std::setiosflags (std::ios::left) |
4913
|
586 << std::setw (front) |
4914
|
587 << "" |
|
588 << std::resetiosflags (std::ios::left) |
4913
|
589 << make_dimensions_string (param.dimensions) |
|
590 << std::setiosflags (std::ios::left) |
|
591 << std::setw (back) |
4914
|
592 << "" |
|
593 << std::resetiosflags (std::ios::left); |
4913
|
594 } |
|
595 else |
|
596 { |
|
597 os << std::setiosflags (std::ios::left) |
|
598 << std::setw (param.parameter_length); |
|
599 } |
|
600 break; |
|
601 |
|
602 default: |
4914
|
603 error ("whos_line_format: modifier `%c' unknown", |
|
604 param.modifier); |
|
605 |
4913
|
606 os << std::setiosflags (std::ios::right) |
|
607 << std::setw (param.parameter_length); |
|
608 } |
|
609 |
|
610 switch (param.command) |
|
611 { |
|
612 case 'b': |
|
613 os << byte_size (); |
|
614 break; |
|
615 |
|
616 case 'e': |
5164
|
617 os << capacity (); |
4913
|
618 break; |
|
619 |
|
620 case 'n': |
|
621 os << name (); |
|
622 break; |
3013
|
623 |
4913
|
624 case 'p': |
|
625 { |
|
626 std::stringstream ss; |
|
627 std::string str; |
|
628 |
|
629 ss << (is_read_only () ? "r-" : "rw") |
|
630 << (is_static () || is_eternal () ? "-" : "d"); |
|
631 str = ss.str (); |
|
632 |
|
633 os << str; |
|
634 } |
|
635 break; |
|
636 |
|
637 case 's': |
|
638 if (param.modifier != 'c') |
|
639 os << make_dimensions_string (param.dimensions); |
|
640 break; |
3013
|
641 |
4913
|
642 case 't': |
|
643 os << type_name (); |
|
644 break; |
4914
|
645 |
|
646 default: |
4913
|
647 error ("whos_line_format: command `%c' unknown", param.command); |
|
648 } |
|
649 |
|
650 os << std::resetiosflags (std::ios::left) |
|
651 << std::resetiosflags (std::ios::right); |
|
652 i++; |
|
653 } |
|
654 else |
|
655 { |
|
656 os << param.text; |
|
657 i++; |
|
658 } |
|
659 } |
3013
|
660 } |
|
661 |
3239
|
662 void |
3944
|
663 symbol_record::print_info (std::ostream& os, const std::string& prefix) const |
3239
|
664 { |
5399
|
665 os << prefix << "formal param: " << formal_param << "\n" |
|
666 << prefix << "linked to global: " << linked_to_global << "\n" |
|
667 << prefix << "tagged static: " << tagged_static << "\n" |
|
668 << prefix << "can hide function: " << can_hide_function << "\n" |
|
669 << prefix << "visible: " << visible << "\n"; |
|
670 |
3239
|
671 if (definition) |
3933
|
672 definition->print_info (os, prefix); |
3239
|
673 else |
3933
|
674 os << prefix << "symbol " << name () << " is undefined\n"; |
3239
|
675 } |
|
676 |
3013
|
677 bool |
2791
|
678 symbol_record::read_only_error (const char *action) |
195
|
679 { |
|
680 if (is_read_only ()) |
|
681 { |
3258
|
682 if (is_variable () || is_constant ()) |
3013
|
683 ::error ("can't %s read-only constant `%s'", action, nm.c_str ()); |
195
|
684 else if (is_function ()) |
3013
|
685 ::error ("can't %s read-only function `%s'", action, nm.c_str ()); |
1045
|
686 else |
3013
|
687 ::error ("can't %s read-only symbol `%s'", action, nm.c_str ()); |
195
|
688 |
3013
|
689 return true; |
195
|
690 } |
|
691 else |
3013
|
692 return false; |
195
|
693 } |
|
694 |
767
|
695 // A symbol table. |
1
|
696 |
5013
|
697 symbol_table::~symbol_table (void) |
|
698 { |
|
699 for (unsigned int i = 0; i < table_size; i++) |
|
700 { |
|
701 symbol_record *ptr = table[i].next (); |
|
702 |
|
703 while (ptr) |
|
704 { |
|
705 symbol_record *tmp = ptr; |
|
706 |
|
707 ptr = ptr->next (); |
|
708 |
|
709 delete tmp; |
|
710 } |
|
711 } |
|
712 |
|
713 delete [] table; |
|
714 } |
|
715 |
1
|
716 symbol_record * |
3523
|
717 symbol_table::lookup (const std::string& nm, bool insert, bool warn) |
1
|
718 { |
4238
|
719 if (Vdebug_symtab_lookups) |
|
720 { |
4269
|
721 std::cerr << (table_name.empty () ? std::string ("???") : table_name) |
4238
|
722 << " symtab::lookup [" |
|
723 << (insert ? "I" : "-") |
|
724 << (warn ? "W" : "-") |
|
725 << "] \"" << nm << "\"\n"; |
|
726 } |
|
727 |
3013
|
728 unsigned int index = hash (nm); |
1
|
729 |
|
730 symbol_record *ptr = table[index].next (); |
|
731 |
530
|
732 while (ptr) |
1
|
733 { |
1755
|
734 if (ptr->name () == nm) |
1
|
735 return ptr; |
3013
|
736 |
1
|
737 ptr = ptr->next (); |
|
738 } |
|
739 |
|
740 if (insert) |
|
741 { |
3013
|
742 symbol_record *sr = new symbol_record (nm, table[index].next ()); |
|
743 |
|
744 table[index].chain (sr); |
|
745 |
|
746 return sr; |
1
|
747 } |
|
748 else if (warn) |
3356
|
749 warning ("lookup: symbol `%s' not found", nm.c_str ()); |
1
|
750 |
530
|
751 return 0; |
1
|
752 } |
|
753 |
|
754 void |
3523
|
755 symbol_table::rename (const std::string& old_name, const std::string& new_name) |
572
|
756 { |
4238
|
757 if (Vdebug_symtab_lookups) |
|
758 { |
4269
|
759 std::cerr << (table_name.empty () ? std::string ("???") : table_name) |
4238
|
760 << " symtab::rename " |
|
761 << "\"" << old_name << "\"" |
|
762 << " to " |
|
763 << "\"" << new_name << "\"\n"; |
|
764 } |
|
765 |
3013
|
766 unsigned int index = hash (old_name); |
572
|
767 |
|
768 symbol_record *prev = &table[index]; |
|
769 symbol_record *ptr = prev->next (); |
|
770 |
|
771 while (ptr) |
|
772 { |
1755
|
773 if (ptr->name () == old_name) |
572
|
774 { |
|
775 ptr->rename (new_name); |
|
776 |
2791
|
777 if (! error_state) |
|
778 { |
|
779 prev->chain (ptr->next ()); |
|
780 |
3013
|
781 index = hash (new_name); |
3125
|
782 ptr->chain (table[index].next ()); |
2791
|
783 table[index].chain (ptr); |
|
784 |
|
785 return; |
|
786 } |
|
787 |
|
788 break; |
572
|
789 } |
|
790 |
|
791 prev = ptr; |
|
792 ptr = ptr->next (); |
|
793 } |
|
794 |
1755
|
795 error ("unable to rename `%s' to `%s'", old_name.c_str (), |
|
796 new_name.c_str ()); |
572
|
797 } |
|
798 |
4009
|
799 // XXX FIXME XXX -- it would be nice to eliminate a lot of the |
|
800 // following duplicate code. |
|
801 |
572
|
802 void |
4009
|
803 symbol_table::clear (void) |
|
804 { |
|
805 for (unsigned int i = 0; i < table_size; i++) |
|
806 { |
|
807 symbol_record *ptr = table[i].next (); |
|
808 |
|
809 while (ptr) |
|
810 { |
|
811 ptr->clear (); |
|
812 |
|
813 ptr = ptr->next (); |
|
814 } |
|
815 } |
|
816 } |
|
817 |
|
818 void |
|
819 symbol_table::clear_variables (void) |
|
820 { |
|
821 for (unsigned int i = 0; i < table_size; i++) |
|
822 { |
|
823 symbol_record *ptr = table[i].next (); |
|
824 |
|
825 while (ptr) |
|
826 { |
|
827 if (ptr->is_user_variable ()) |
|
828 ptr->clear (); |
|
829 |
|
830 ptr = ptr->next (); |
|
831 } |
|
832 } |
|
833 } |
|
834 |
|
835 // Really only clear functions that can be reloaded. |
|
836 |
|
837 void |
|
838 symbol_table::clear_functions (void) |
1
|
839 { |
3013
|
840 for (unsigned int i = 0; i < table_size; i++) |
1
|
841 { |
169
|
842 symbol_record *ptr = table[i].next (); |
1
|
843 |
530
|
844 while (ptr) |
169
|
845 { |
4009
|
846 if (ptr->is_user_function () || ptr->is_dld_function ()) |
|
847 ptr->clear (); |
|
848 |
|
849 ptr = ptr->next (); |
|
850 } |
|
851 } |
|
852 } |
|
853 |
|
854 void |
|
855 symbol_table::clear_globals (void) |
|
856 { |
|
857 for (unsigned int i = 0; i < table_size; i++) |
|
858 { |
|
859 symbol_record *ptr = table[i].next (); |
|
860 |
|
861 while (ptr) |
|
862 { |
|
863 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
864 ptr->clear (); |
195
|
865 |
169
|
866 ptr = ptr->next (); |
1
|
867 } |
|
868 } |
|
869 } |
|
870 |
3013
|
871 bool |
4009
|
872 symbol_table::clear (const std::string& nm) |
|
873 { |
|
874 unsigned int index = hash (nm); |
|
875 |
|
876 symbol_record *ptr = table[index].next (); |
|
877 |
|
878 while (ptr) |
|
879 { |
|
880 if (ptr->name () == nm) |
|
881 { |
|
882 ptr->clear (); |
5397
|
883 |
4009
|
884 return true; |
|
885 } |
|
886 ptr = ptr->next (); |
|
887 } |
|
888 |
|
889 return false; |
|
890 } |
|
891 |
|
892 bool |
|
893 symbol_table::clear_variable (const std::string& nm) |
|
894 { |
|
895 unsigned int index = hash (nm); |
|
896 |
|
897 symbol_record *ptr = table[index].next (); |
|
898 |
|
899 while (ptr) |
|
900 { |
|
901 if (ptr->name () == nm && ptr->is_user_variable ()) |
|
902 { |
|
903 ptr->clear (); |
|
904 return true; |
|
905 } |
|
906 ptr = ptr->next (); |
|
907 } |
|
908 |
|
909 return false; |
|
910 } |
|
911 |
|
912 bool |
|
913 symbol_table::clear_global (const std::string& nm) |
1
|
914 { |
3013
|
915 unsigned int index = hash (nm); |
1
|
916 |
195
|
917 symbol_record *ptr = table[index].next (); |
1
|
918 |
530
|
919 while (ptr) |
1
|
920 { |
1755
|
921 if (ptr->name () == nm |
4009
|
922 && ptr->is_user_variable () |
|
923 && ptr->is_linked_to_global ()) |
|
924 { |
|
925 ptr->clear (); |
|
926 return true; |
|
927 } |
|
928 ptr = ptr->next (); |
|
929 } |
|
930 |
|
931 return false; |
|
932 } |
|
933 |
|
934 // Really only clear functions that can be reloaded. |
|
935 |
|
936 bool |
|
937 symbol_table::clear_function (const std::string& nm) |
|
938 { |
|
939 unsigned int index = hash (nm); |
|
940 |
|
941 symbol_record *ptr = table[index].next (); |
|
942 |
|
943 while (ptr) |
|
944 { |
|
945 if (ptr->name () == nm |
|
946 && (ptr->is_user_function () || ptr->is_dld_function ())) |
1
|
947 { |
195
|
948 ptr->clear (); |
3013
|
949 return true; |
1
|
950 } |
195
|
951 ptr = ptr->next (); |
1
|
952 } |
|
953 |
3013
|
954 return false; |
1
|
955 } |
|
956 |
4009
|
957 bool |
|
958 symbol_table::clear_variable_pattern (const std::string& pat) |
|
959 { |
|
960 bool retval = false; |
|
961 |
|
962 for (unsigned int i = 0; i < table_size; i++) |
|
963 { |
|
964 symbol_record *ptr = table[i].next (); |
|
965 |
|
966 while (ptr) |
|
967 { |
|
968 if (ptr->is_user_variable ()) |
|
969 { |
|
970 glob_match pattern (pat); |
|
971 |
|
972 if (pattern.match (ptr->name ())) |
|
973 { |
|
974 ptr->clear (); |
|
975 |
|
976 retval = true; |
|
977 } |
|
978 } |
|
979 |
|
980 ptr = ptr->next (); |
|
981 } |
|
982 } |
|
983 |
|
984 return retval; |
|
985 } |
|
986 |
|
987 bool |
|
988 symbol_table::clear_global_pattern (const std::string& pat) |
|
989 { |
|
990 bool retval = false; |
|
991 |
|
992 for (unsigned int i = 0; i < table_size; i++) |
|
993 { |
|
994 symbol_record *ptr = table[i].next (); |
|
995 |
|
996 while (ptr) |
|
997 { |
|
998 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
999 { |
|
1000 glob_match pattern (pat); |
|
1001 |
|
1002 if (pattern.match (ptr->name ())) |
|
1003 { |
|
1004 ptr->clear (); |
|
1005 |
|
1006 retval = true; |
|
1007 } |
|
1008 } |
|
1009 |
|
1010 ptr = ptr->next (); |
|
1011 } |
|
1012 } |
|
1013 |
|
1014 return retval; |
|
1015 } |
|
1016 |
|
1017 // Really only clear functions that can be reloaded. |
|
1018 |
|
1019 bool |
|
1020 symbol_table::clear_function_pattern (const std::string& pat) |
|
1021 { |
|
1022 bool retval = false; |
|
1023 |
|
1024 for (unsigned int i = 0; i < table_size; i++) |
|
1025 { |
|
1026 symbol_record *ptr = table[i].next (); |
|
1027 |
|
1028 while (ptr) |
|
1029 { |
|
1030 if (ptr->is_user_function () || ptr->is_dld_function ()) |
|
1031 { |
|
1032 glob_match pattern (pat); |
|
1033 |
|
1034 if (pattern.match (ptr->name ())) |
|
1035 { |
|
1036 ptr->clear (); |
|
1037 |
|
1038 retval = true; |
|
1039 } |
|
1040 } |
|
1041 |
|
1042 ptr = ptr->next (); |
|
1043 } |
|
1044 } |
|
1045 |
|
1046 return retval; |
|
1047 } |
|
1048 |
1
|
1049 int |
164
|
1050 symbol_table::size (void) const |
1
|
1051 { |
|
1052 int count = 0; |
3013
|
1053 |
|
1054 for (unsigned int i = 0; i < table_size; i++) |
1
|
1055 { |
|
1056 symbol_record *ptr = table[i].next (); |
3013
|
1057 |
530
|
1058 while (ptr) |
1
|
1059 { |
|
1060 count++; |
|
1061 ptr = ptr->next (); |
|
1062 } |
|
1063 } |
3013
|
1064 |
1
|
1065 return count; |
|
1066 } |
|
1067 |
3013
|
1068 static bool |
3523
|
1069 matches_patterns (const std::string& name, const string_vector& pats) |
1
|
1070 { |
3013
|
1071 int npats = pats.length (); |
1
|
1072 |
3013
|
1073 if (npats == 0) |
|
1074 return true; |
|
1075 |
|
1076 glob_match pattern (pats); |
|
1077 |
|
1078 return pattern.match (name); |
1
|
1079 } |
|
1080 |
3013
|
1081 Array<symbol_record *> |
4913
|
1082 symbol_table::subsymbol_list (const string_vector& pats, |
|
1083 unsigned int type, unsigned int scope) const |
|
1084 { |
|
1085 int count = 0; |
|
1086 |
|
1087 int n = size (); |
|
1088 |
5659
|
1089 Array<symbol_record *> subsymbols (dim_vector (n, 1)); |
4913
|
1090 int pats_length = pats.length (); |
|
1091 |
|
1092 if (n == 0) |
|
1093 return subsymbols; |
|
1094 |
|
1095 // Look for separators like .({ |
|
1096 for (int j = 0; j < pats_length; j++) |
|
1097 { |
|
1098 std::string var_name = pats (j); |
|
1099 |
|
1100 size_t pos = var_name.find_first_of (".({"); |
|
1101 |
|
1102 if ((pos != NPOS) && (pos > 0)) |
|
1103 { |
|
1104 std::string first_name = var_name.substr(0,pos); |
|
1105 |
|
1106 for (unsigned int i = 0; i < table_size; i++) |
|
1107 { |
|
1108 symbol_record *ptr = table[i].next (); |
|
1109 |
|
1110 while (ptr) |
|
1111 { |
|
1112 assert (count < n); |
|
1113 |
|
1114 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
|
1115 |
|
1116 unsigned int my_type = ptr->type (); |
|
1117 |
|
1118 std::string my_name = ptr->name (); |
|
1119 |
|
1120 if ((type & my_type) && (scope & my_scope) && (first_name == my_name)) |
|
1121 { |
|
1122 symbol_record *sym_ptr = new symbol_record (); |
|
1123 octave_value value; |
|
1124 int parse_status; |
4914
|
1125 |
4913
|
1126 value = eval_string (var_name, true, parse_status); |
4914
|
1127 |
4913
|
1128 sym_ptr->define (value); |
|
1129 sym_ptr->rename (var_name); |
|
1130 subsymbols(count++) = sym_ptr; |
|
1131 } |
|
1132 |
|
1133 ptr = ptr->next (); |
|
1134 } |
|
1135 } |
|
1136 } |
|
1137 } |
|
1138 |
5659
|
1139 subsymbols.resize (dim_vector (count, 1)); |
4913
|
1140 |
|
1141 return subsymbols; |
|
1142 } |
|
1143 |
|
1144 Array<symbol_record *> |
3355
|
1145 symbol_table::symbol_list (const string_vector& pats, |
3013
|
1146 unsigned int type, unsigned int scope) const |
1
|
1147 { |
3355
|
1148 int count = 0; |
3013
|
1149 |
1
|
1150 int n = size (); |
3013
|
1151 |
5659
|
1152 Array<symbol_record *> symbols (dim_vector (n, 1)); |
3355
|
1153 |
1
|
1154 if (n == 0) |
3355
|
1155 return symbols; |
3013
|
1156 |
|
1157 for (unsigned int i = 0; i < table_size; i++) |
1
|
1158 { |
|
1159 symbol_record *ptr = table[i].next (); |
3013
|
1160 |
530
|
1161 while (ptr) |
1
|
1162 { |
5399
|
1163 if (ptr->is_visible ()) |
5397
|
1164 { |
|
1165 assert (count < n); |
867
|
1166 |
5397
|
1167 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
1168 |
5397
|
1169 unsigned int my_type = ptr->type (); |
|
1170 |
|
1171 std::string my_name = ptr->name (); |
867
|
1172 |
5397
|
1173 if ((type & my_type) && (scope & my_scope) && (matches_patterns (my_name, pats))) |
|
1174 symbols(count++) = ptr; |
|
1175 } |
5399
|
1176 |
1
|
1177 ptr = ptr->next (); |
|
1178 } |
|
1179 } |
1755
|
1180 |
5659
|
1181 symbols.resize (dim_vector (count, 1)); |
1
|
1182 |
3013
|
1183 return symbols; |
|
1184 } |
|
1185 |
|
1186 string_vector |
3355
|
1187 symbol_table::name_list (const string_vector& pats, bool sort, |
3013
|
1188 unsigned int type, unsigned int scope) const |
|
1189 { |
3355
|
1190 Array<symbol_record *> symbols = symbol_list (pats, type, scope); |
3013
|
1191 |
|
1192 string_vector names; |
|
1193 |
|
1194 int n = symbols.length (); |
|
1195 |
|
1196 if (n > 0) |
|
1197 { |
|
1198 names.resize (n); |
|
1199 |
|
1200 for (int i = 0; i < n; i++) |
|
1201 names[i] = symbols(i)->name (); |
|
1202 } |
|
1203 |
|
1204 if (sort) |
|
1205 names.qsort (); |
|
1206 |
|
1207 return names; |
|
1208 } |
|
1209 |
|
1210 static int |
3145
|
1211 maybe_list_cmp_fcn (const void *a_arg, const void *b_arg) |
3013
|
1212 { |
3145
|
1213 const symbol_record *a = *(X_CAST (const symbol_record **, a_arg)); |
|
1214 const symbol_record *b = *(X_CAST (const symbol_record **, b_arg)); |
3013
|
1215 |
3523
|
1216 std::string a_nm = a->name (); |
|
1217 std::string b_nm = b->name (); |
3145
|
1218 |
|
1219 return a_nm.compare (b_nm); |
3013
|
1220 } |
1
|
1221 |
4913
|
1222 void |
|
1223 symbol_table::print_descriptor (std::ostream& os, |
|
1224 std::list<whos_parameter> params) const |
|
1225 { |
|
1226 // This method prints a line of information on a given symbol |
|
1227 std::list<whos_parameter>::iterator i = params.begin (); |
|
1228 OSSTREAM param_buf; |
|
1229 |
|
1230 while (i != params.end ()) |
|
1231 { |
|
1232 whos_parameter param = * i; |
|
1233 |
|
1234 if (param.command != '\0') |
|
1235 { |
|
1236 // Do the actual printing |
|
1237 switch (param.modifier) |
|
1238 { |
|
1239 case 'l': |
|
1240 os << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1241 param_buf << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1242 break; |
|
1243 |
|
1244 case 'r': |
|
1245 os << std::setiosflags (std::ios::right) << std::setw (param.parameter_length); |
|
1246 param_buf << std::setiosflags (std::ios::right) << std::setw (param.parameter_length); |
|
1247 break; |
|
1248 |
|
1249 case 'c': |
4914
|
1250 if (param.command != 's') |
4913
|
1251 { |
|
1252 os << std::setiosflags (std::ios::left) |
|
1253 << std::setw (param.parameter_length); |
|
1254 param_buf << std::setiosflags (std::ios::left) |
|
1255 << std::setw (param.parameter_length); |
|
1256 } |
|
1257 break; |
|
1258 |
|
1259 default: |
|
1260 os << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1261 param_buf << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1262 } |
|
1263 |
|
1264 if (param.command == 's' && param.modifier == 'c') |
|
1265 { |
|
1266 int a, b; |
4914
|
1267 |
4913
|
1268 if (param.modifier == 'c') |
|
1269 { |
|
1270 a = param.first_parameter_length - param.balance; |
|
1271 a = (a < 0 ? 0 : a); |
|
1272 b = param.parameter_length - a - param.text . length (); |
|
1273 b = (b < 0 ? 0 : b); |
|
1274 os << std::setiosflags (std::ios::left) << std::setw (a) |
|
1275 << "" << std::resetiosflags (std::ios::left) << param.text |
|
1276 << std::setiosflags (std::ios::left) |
|
1277 << std::setw (b) << "" |
|
1278 << std::resetiosflags (std::ios::left); |
|
1279 param_buf << std::setiosflags (std::ios::left) << std::setw (a) |
|
1280 << "" << std::resetiosflags (std::ios::left) << param.line |
|
1281 << std::setiosflags (std::ios::left) |
|
1282 << std::setw (b) << "" |
|
1283 << std::resetiosflags (std::ios::left); |
|
1284 } |
|
1285 } |
|
1286 else |
|
1287 { |
|
1288 os << param.text; |
|
1289 param_buf << param.line; |
|
1290 } |
|
1291 os << std::resetiosflags (std::ios::left) |
|
1292 << std::resetiosflags (std::ios::right); |
|
1293 param_buf << std::resetiosflags (std::ios::left) |
|
1294 << std::resetiosflags (std::ios::right); |
|
1295 i++; |
|
1296 } |
|
1297 else |
|
1298 { |
|
1299 os << param.text; |
|
1300 param_buf << param.line; |
|
1301 i++; |
|
1302 } |
|
1303 } |
|
1304 |
|
1305 param_buf << OSSTREAM_ENDS; |
|
1306 os << OSSTREAM_C_STR (param_buf); |
|
1307 OSSTREAM_FREEZE (param_buf); |
|
1308 } |
|
1309 |
|
1310 std::list<whos_parameter> |
|
1311 symbol_table::parse_whos_line_format (Array<symbol_record *>& symbols) const |
|
1312 { |
|
1313 // This method parses the string whos_line_format, and returns |
|
1314 // a parameter list, containing all information needed to print |
|
1315 // the given attributtes of the symbols |
|
1316 int idx; |
|
1317 size_t format_len = Vwhos_line_format.length (); |
|
1318 char garbage; |
|
1319 std::list<whos_parameter> params; |
|
1320 |
|
1321 size_t bytes1; |
|
1322 int elements1; |
|
1323 |
|
1324 int len = symbols.length (), i; |
|
1325 |
|
1326 std::string param_string = "benpst"; |
5659
|
1327 Array<int> param_length (dim_vector (param_string.length (), 1)); |
|
1328 Array<std::string> param_names (dim_vector (param_string.length (), 1)); |
4913
|
1329 size_t pos_b, pos_t, pos_e, pos_n, pos_p, pos_s; |
|
1330 |
|
1331 pos_b = param_string.find ('b'); // Bytes |
|
1332 pos_t = param_string.find ('t'); // (Type aka) Class |
|
1333 pos_e = param_string.find ('e'); // Elements |
|
1334 pos_n = param_string.find ('n'); // Name |
|
1335 pos_p = param_string.find ('p'); // Protected |
|
1336 pos_s = param_string.find ('s'); // Size |
|
1337 |
|
1338 param_names(pos_b) = "Bytes"; |
|
1339 param_names(pos_t) = "Class"; |
|
1340 param_names(pos_e) = "Elements"; |
|
1341 param_names(pos_n) = "Name"; |
|
1342 param_names(pos_p) = "Prot"; |
|
1343 param_names(pos_s) = "Size"; |
|
1344 |
|
1345 for (i = 0; i < 6; i++) |
|
1346 param_length(i) = param_names(i) . length (); |
|
1347 |
|
1348 // Calculating necessary spacing for name column, |
|
1349 // bytes column, elements column and class column |
|
1350 for (i = 0; i < static_cast<int> (len); i++) |
|
1351 { |
|
1352 std::stringstream ss1, ss2; |
|
1353 std::string str; |
|
1354 |
|
1355 str = symbols(i)->name (); |
5049
|
1356 param_length(pos_n) = ((str.length () |
|
1357 > static_cast<size_t> (param_length(pos_n))) |
|
1358 ? str.length () : param_length(pos_n)); |
4913
|
1359 |
|
1360 str = symbols(i)->type_name (); |
5049
|
1361 param_length(pos_t) = ((str.length () |
|
1362 > static_cast<size_t> (param_length(pos_t))) |
|
1363 ? str.length () : param_length(pos_t)); |
4913
|
1364 |
5164
|
1365 elements1 = symbols(i)->capacity (); |
4913
|
1366 ss1 << elements1; |
|
1367 str = ss1.str (); |
5049
|
1368 param_length(pos_e) = ((str.length () |
|
1369 > static_cast<size_t> (param_length(pos_e))) |
|
1370 ? str.length () : param_length(pos_e)); |
4913
|
1371 |
|
1372 bytes1 = symbols(i)->byte_size (); |
|
1373 ss2 << bytes1; |
|
1374 str = ss2.str (); |
5049
|
1375 param_length(pos_b) = ((str.length () |
|
1376 > static_cast<size_t> (param_length(pos_b))) |
|
1377 ? str.length () : param_length (pos_b)); |
4913
|
1378 } |
|
1379 |
|
1380 idx = 0; |
|
1381 while (static_cast<size_t> (idx) < format_len) |
|
1382 { |
|
1383 whos_parameter param; |
|
1384 param.command = '\0'; |
|
1385 |
|
1386 if (Vwhos_line_format[idx] == '%') |
4914
|
1387 { |
|
1388 bool error_encountered = false; |
4913
|
1389 param.modifier = 'r'; |
|
1390 param.parameter_length = 0; |
|
1391 param.dimensions = 8; |
|
1392 |
|
1393 int a = 0, b = -1, c = 8, balance = 1; |
|
1394 unsigned int items; |
|
1395 size_t pos; |
|
1396 std::string cmd; |
|
1397 |
|
1398 // Parse one command from whos_line_format |
|
1399 cmd = Vwhos_line_format.substr (idx, Vwhos_line_format.length ()); |
|
1400 pos = cmd.find (';'); |
|
1401 if (pos != NPOS) |
|
1402 cmd = cmd.substr (0, pos+1); |
|
1403 else |
|
1404 error ("parameter without ; in whos_line_format"); |
|
1405 |
|
1406 idx += cmd.length (); |
4914
|
1407 |
|
1408 // XXX FIXME XXX -- use iostream functions instead of sscanf! |
|
1409 |
4913
|
1410 if (cmd.find_first_of ("crl") != 1) |
|
1411 items = sscanf (cmd.c_str (), "%c%c:%d:%d:%d:%d;", |
|
1412 &garbage, ¶m.command, &a, &b, &c, &balance); |
|
1413 else |
|
1414 items = sscanf (cmd.c_str (), "%c%c%c:%d:%d:%d:%d;", |
4914
|
1415 &garbage, ¶m.modifier, ¶m.command, |
|
1416 &a, &b, &c, &balance) - 1; |
|
1417 |
4913
|
1418 if (items < 2) |
|
1419 { |
|
1420 error ("whos_line_format: parameter structure without command in whos_line_format"); |
4914
|
1421 error_encountered = true; |
4913
|
1422 } |
|
1423 |
|
1424 // Insert data into parameter |
|
1425 param.first_parameter_length = 0; |
|
1426 pos = param_string.find (param.command); |
|
1427 if (pos != NPOS) |
|
1428 { |
|
1429 param.parameter_length = param_length(pos); |
|
1430 param.text = param_names(pos); |
5049
|
1431 param.line.assign (param_names(pos).length (), '='); |
5048
|
1432 |
5049
|
1433 param.parameter_length = (a > param.parameter_length |
|
1434 ? a : param.parameter_length); |
|
1435 if (param.command == 's' && param.modifier == 'c' && b > 0) |
5048
|
1436 param.first_parameter_length = b; |
4913
|
1437 } |
|
1438 else |
|
1439 { |
|
1440 error ("whos_line_format: '%c' is not a command", |
|
1441 param.command); |
4914
|
1442 error_encountered = true; |
4913
|
1443 } |
|
1444 |
|
1445 if (param.command == 's') |
|
1446 { |
|
1447 // Have to calculate space needed for printing matrix dimensions |
|
1448 // Space needed for Size column is hard to determine in prior, |
|
1449 // because it depends on dimensions to be shown. That is why it is |
|
1450 // recalculated for each Size-command |
5048
|
1451 int j, first, rest = 0, total; |
4913
|
1452 param.dimensions = c; |
5048
|
1453 first = param.first_parameter_length; |
|
1454 total = param.parameter_length; |
4914
|
1455 |
4913
|
1456 for (j = 0; j < len; j++) |
5050
|
1457 { |
|
1458 int first1 = symbols(j)->dimensions_string_req_first_space (param.dimensions); |
|
1459 int total1 = symbols(j)->dimensions_string_req_total_space (param.dimensions); |
|
1460 int rest1 = total1 - first1; |
|
1461 rest = (rest1 > rest ? rest1 : rest); |
|
1462 first = (first1 > first ? first1 : first); |
|
1463 total = (total1 > total ? total1 : total); |
|
1464 } |
4913
|
1465 |
|
1466 if (param.modifier == 'c') |
|
1467 { |
|
1468 if (first < balance) |
|
1469 first += balance - first; |
|
1470 if (rest + balance < param.parameter_length) |
|
1471 rest += param.parameter_length - rest - balance; |
|
1472 |
|
1473 param.parameter_length = first + rest; |
|
1474 param.first_parameter_length = first; |
|
1475 param.balance = balance; |
|
1476 } |
|
1477 else |
|
1478 { |
|
1479 param.parameter_length = total; |
|
1480 param.first_parameter_length = 0; |
|
1481 } |
|
1482 } |
|
1483 else if (param.modifier == 'c') |
|
1484 { |
|
1485 error ("whos_line_format: modifier 'c' not available for command '%c'", |
|
1486 param.command); |
4914
|
1487 error_encountered = true; |
4913
|
1488 } |
|
1489 |
|
1490 // What happens if whos_line_format contains negative numbers |
|
1491 // at param_length positions? |
5049
|
1492 param.balance = (b < 0 ? 0 : param.balance); |
|
1493 param.first_parameter_length = (b < 0 ? 0 : |
4913
|
1494 param.first_parameter_length); |
5049
|
1495 param.parameter_length = (a < 0 |
|
1496 ? 0 |
|
1497 : (param.parameter_length |
|
1498 < param_length(pos_s) |
|
1499 ? param_length(pos_s) |
|
1500 : param.parameter_length)); |
4913
|
1501 |
|
1502 // Parameter will not be pushed into parameter list if ... |
4914
|
1503 if (! error_encountered) |
4913
|
1504 params.push_back (param); |
|
1505 } |
|
1506 else |
|
1507 { |
|
1508 // Text string, to be printed as it is ... |
|
1509 std::string text; |
|
1510 size_t pos; |
|
1511 text = Vwhos_line_format.substr (idx, Vwhos_line_format.length ()); |
|
1512 pos = text.find ('%'); |
|
1513 if (pos != NPOS) |
|
1514 text = text.substr (0, pos); |
|
1515 |
|
1516 // Push parameter into list ... |
|
1517 idx += text.length (); |
|
1518 param.text=text; |
|
1519 param.line.assign (text.length(), ' '); |
|
1520 params.push_back (param); |
|
1521 } |
|
1522 } |
|
1523 |
|
1524 return params; |
|
1525 } |
|
1526 |
3013
|
1527 int |
|
1528 symbol_table::maybe_list (const char *header, const string_vector& argv, |
3523
|
1529 std::ostream& os, bool show_verbose, |
3013
|
1530 unsigned type, unsigned scope) |
|
1531 { |
4914
|
1532 // This method prints information for sets of symbols, but only one |
|
1533 // set at a time (like, for instance: all variables, or all |
|
1534 // built-in-functions). |
4913
|
1535 |
4914
|
1536 // This method invokes print_symbol_info_line to print info on every |
|
1537 // symbol. |
4913
|
1538 |
3013
|
1539 int status = 0; |
|
1540 |
|
1541 if (show_verbose) |
|
1542 { |
4914
|
1543 // XXX FIXME XXX Should separate argv to lists with and without dots. |
|
1544 Array<symbol_record *> xsymbols = symbol_list (argv, type, scope); |
|
1545 Array<symbol_record *> xsubsymbols = subsymbol_list (argv, type, scope); |
3013
|
1546 |
4914
|
1547 int sym_len = xsymbols.length (), subsym_len = xsubsymbols.length (), |
4913
|
1548 len = sym_len + subsym_len; |
4914
|
1549 |
5659
|
1550 Array<symbol_record *> symbols (dim_vector (len, 1)); |
3013
|
1551 |
3355
|
1552 if (len > 0) |
3013
|
1553 { |
5005
|
1554 size_t bytes = 0; |
|
1555 size_t elements = 0; |
|
1556 |
|
1557 int i; |
|
1558 |
4913
|
1559 std::list<whos_parameter> params; |
|
1560 |
4914
|
1561 // Joining symbolic tables. |
4913
|
1562 for (i = 0; i < sym_len; i++) |
4914
|
1563 symbols(i) = xsymbols(i); |
4913
|
1564 |
|
1565 for (i = 0; i < subsym_len; i++) |
4914
|
1566 symbols(i+sym_len) = xsubsymbols(i); |
4913
|
1567 |
|
1568 os << "\n" << header << "\n\n"; |
3013
|
1569 |
|
1570 symbols.qsort (maybe_list_cmp_fcn); |
|
1571 |
4913
|
1572 params = parse_whos_line_format (symbols); |
|
1573 |
|
1574 print_descriptor (os, params); |
|
1575 |
|
1576 os << "\n"; |
|
1577 |
|
1578 for (int j = 0; j < len; j++) |
|
1579 { |
|
1580 symbols(j)->print_symbol_info_line (os, params); |
5164
|
1581 elements += symbols(j)->capacity (); |
4913
|
1582 bytes += symbols(j)->byte_size (); |
|
1583 } |
|
1584 |
5005
|
1585 os << "\nTotal is " |
5006
|
1586 << elements << (elements == 1 ? " element" : " elements") |
5005
|
1587 << " using " |
5006
|
1588 << bytes << (bytes == 1 ? " byte" : " bytes") |
5005
|
1589 << "\n"; |
3013
|
1590 |
|
1591 status = 1; |
|
1592 } |
|
1593 } |
|
1594 else |
|
1595 { |
3355
|
1596 string_vector symbols = name_list (argv, 1, type, scope); |
3013
|
1597 |
3355
|
1598 if (! symbols.empty ()) |
4914
|
1599 { |
3013
|
1600 os << "\n" << header << "\n\n"; |
|
1601 |
|
1602 symbols.list_in_columns (os); |
|
1603 |
|
1604 status = 1; |
|
1605 } |
|
1606 } |
|
1607 |
|
1608 return status; |
1
|
1609 } |
|
1610 |
3355
|
1611 Array<symbol_record *> |
3523
|
1612 symbol_table::glob (const std::string& pat, unsigned int type, |
2893
|
1613 unsigned int scope) const |
605
|
1614 { |
3355
|
1615 int count = 0; |
|
1616 |
605
|
1617 int n = size (); |
3355
|
1618 |
5659
|
1619 Array<symbol_record *> symbols (dim_vector (n, 1)); |
3355
|
1620 |
605
|
1621 if (n == 0) |
3355
|
1622 return symbols; |
3013
|
1623 |
|
1624 for (unsigned int i = 0; i < table_size; i++) |
605
|
1625 { |
|
1626 symbol_record *ptr = table[i].next (); |
3013
|
1627 |
605
|
1628 while (ptr) |
|
1629 { |
|
1630 assert (count < n); |
|
1631 |
2893
|
1632 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
1633 |
2893
|
1634 unsigned int my_type = ptr->type (); |
605
|
1635 |
1792
|
1636 glob_match pattern (pat); |
1755
|
1637 |
605
|
1638 if ((type & my_type) && (scope & my_scope) |
1792
|
1639 && pattern.match (ptr->name ())) |
605
|
1640 { |
3355
|
1641 symbols(count++) = ptr; |
605
|
1642 } |
|
1643 |
|
1644 ptr = ptr->next (); |
|
1645 } |
|
1646 } |
3355
|
1647 |
5659
|
1648 symbols.resize (dim_vector (count, 1)); |
605
|
1649 |
|
1650 return symbols; |
|
1651 } |
|
1652 |
220
|
1653 void |
|
1654 symbol_table::push_context (void) |
|
1655 { |
3013
|
1656 for (unsigned int i = 0; i < table_size; i++) |
220
|
1657 { |
|
1658 symbol_record *ptr = table[i].next (); |
|
1659 |
530
|
1660 while (ptr) |
220
|
1661 { |
|
1662 ptr->push_context (); |
|
1663 ptr = ptr->next (); |
|
1664 } |
|
1665 } |
|
1666 } |
|
1667 |
|
1668 void |
|
1669 symbol_table::pop_context (void) |
|
1670 { |
3013
|
1671 for (unsigned int i = 0; i < table_size; i++) |
220
|
1672 { |
|
1673 symbol_record *ptr = table[i].next (); |
|
1674 |
530
|
1675 while (ptr) |
220
|
1676 { |
|
1677 ptr->pop_context (); |
|
1678 ptr = ptr->next (); |
|
1679 } |
|
1680 } |
|
1681 } |
|
1682 |
3013
|
1683 void |
3944
|
1684 symbol_table::print_info (std::ostream& os) const |
3013
|
1685 { |
|
1686 int count = 0; |
|
1687 int empty_chains = 0; |
|
1688 int max_chain_length = 0; |
|
1689 int min_chain_length = INT_MAX; |
|
1690 |
|
1691 for (unsigned int i = 0; i < table_size; i++) |
|
1692 { |
|
1693 int num_this_chain = 0; |
|
1694 |
|
1695 symbol_record *ptr = table[i].next (); |
|
1696 |
|
1697 if (ptr) |
3933
|
1698 os << "chain number " << i << ":\n"; |
3013
|
1699 else |
|
1700 { |
|
1701 empty_chains++; |
|
1702 min_chain_length = 0; |
|
1703 } |
|
1704 |
|
1705 while (ptr) |
|
1706 { |
|
1707 num_this_chain++; |
|
1708 |
3933
|
1709 os << " " << ptr->name () << "\n"; |
|
1710 |
|
1711 ptr->print_info (os, " "); |
3013
|
1712 |
|
1713 ptr = ptr->next (); |
|
1714 } |
|
1715 |
|
1716 count += num_this_chain; |
|
1717 |
|
1718 if (num_this_chain > max_chain_length) |
|
1719 max_chain_length = num_this_chain; |
|
1720 |
|
1721 if (num_this_chain < min_chain_length) |
|
1722 min_chain_length = num_this_chain; |
|
1723 |
|
1724 if (num_this_chain > 0) |
3933
|
1725 os << "\n"; |
3013
|
1726 } |
|
1727 |
3933
|
1728 os << "max chain length: " << max_chain_length << "\n"; |
|
1729 os << "min chain length: " << min_chain_length << "\n"; |
|
1730 os << "empty chains: " << empty_chains << "\n"; |
|
1731 os << "total chains: " << table_size << "\n"; |
|
1732 os << "total symbols: " << count << "\n"; |
3013
|
1733 } |
|
1734 |
1
|
1735 // Chris Torek's fave hash function. |
|
1736 |
|
1737 unsigned int |
3523
|
1738 symbol_table::hash (const std::string& str) |
1
|
1739 { |
2893
|
1740 unsigned int h = 0; |
3013
|
1741 |
2893
|
1742 for (unsigned int i = 0; i < str.length (); i++) |
1755
|
1743 h = h * 33 + str[i]; |
1
|
1744 |
3013
|
1745 return h & (table_size - 1); |
2790
|
1746 } |
|
1747 |
3308
|
1748 |
|
1749 static int |
|
1750 variables_can_hide_functions (void) |
|
1751 { |
|
1752 Vvariables_can_hide_functions |
|
1753 = check_preference ("variables_can_hide_functions"); |
|
1754 |
|
1755 return 0; |
|
1756 } |
|
1757 |
4238
|
1758 static int |
|
1759 debug_symtab_lookups (void) |
|
1760 { |
|
1761 Vdebug_symtab_lookups = check_preference ("debug_symtab_lookups"); |
|
1762 |
|
1763 return 0; |
|
1764 } |
|
1765 |
4913
|
1766 static int |
|
1767 whos_line_format (void) |
|
1768 { |
|
1769 Vwhos_line_format = builtin_string_variable ("whos_line_format"); |
|
1770 |
|
1771 return 0; |
|
1772 } |
|
1773 |
3308
|
1774 void |
|
1775 symbols_of_symtab (void) |
|
1776 { |
4233
|
1777 DEFVAR (variables_can_hide_functions, true, variables_can_hide_functions, |
3448
|
1778 "-*- texinfo -*-\n\ |
5016
|
1779 @defvr {Built-in Variable} variables_can_hide_functions\n\ |
3448
|
1780 If the value of this variable is nonzero, assignments to variables may\n\ |
|
1781 hide previously defined functions of the same name. A negative value\n\ |
|
1782 will cause Octave to print a warning, but allow the operation.\n\ |
|
1783 @end defvr"); |
|
1784 |
4238
|
1785 DEFVAR (debug_symtab_lookups, false, debug_symtab_lookups, |
|
1786 "-*- texinfo -*-\n\ |
|
1787 @defvr debug_symtab_lookups\n\ |
4913
|
1788 If the value of this variable is nonzero, print debugging info when\n\ |
4911
|
1789 searching for symbols in the symbol tables.\n\ |
|
1790 @end defvr"); |
4913
|
1791 |
|
1792 DEFVAR (whos_line_format, " %p:4; %ln:6; %cs:16:6:8:1; %rb:12; %lt:-1;\n", whos_line_format, |
|
1793 "-*- texinfo -*-\n\ |
|
1794 @defvr {Built-in Variable} whos_line_format\n\ |
|
1795 This string decides in what order attributtes of variables are to be printed.\n\ |
|
1796 The following commands are used:\n\ |
|
1797 @table @code\n\ |
|
1798 @item %b\n\ |
|
1799 Prints number of bytes occupied by variables.\n\ |
|
1800 @item %e\n\ |
|
1801 Prints elements held by variables.\n\ |
|
1802 @item %n\n\ |
|
1803 Prints variable names.\n\ |
|
1804 @item %p\n\ |
|
1805 Prints protection attributtes of variables.\n\ |
|
1806 @item %s\n\ |
|
1807 Prints dimensions of variables.\n\ |
|
1808 @item %t\n\ |
|
1809 Prints type names of variables.\n\ |
|
1810 @end table\n\ |
|
1811 \n\ |
|
1812 Every command may also have a modifier:\n\ |
|
1813 @table @code\n\ |
|
1814 @item l\n\ |
|
1815 Left alignment.\n\ |
|
1816 @item r\n\ |
|
1817 Right alignment (this is the default).\n\ |
|
1818 @item c\n\ |
|
1819 Centered (may only be applied to command %s).\n\ |
|
1820 @end table\n\ |
|
1821 \n\ |
|
1822 A command is composed like this:\n\ |
|
1823 %[modifier]<command>[:size_of_parameter[:center-specific[:print_dims[:balance]]]];\n\ |
|
1824 \n\ |
|
1825 Command and modifier is already explained. Size_of_parameter\n\ |
|
1826 tells how many columns the parameter will need for printing.\n\ |
|
1827 print_dims tells how many dimensions to print. If number of\n\ |
|
1828 dimensions exceeds print_dims, dimensions will be printed like\n\ |
|
1829 x-D.\n\ |
|
1830 center-specific and print_dims may only be applied to command\n\ |
|
1831 %s. A negative value for print_dims will cause Octave to print all\n\ |
|
1832 dimensions whatsoever.\n\ |
|
1833 balance specifies the offset for printing of the dimensions string.\n\ |
|
1834 \n\ |
|
1835 Default format is \" %p:4; %ln:6; %cs:16:6:8:1; %rb:12; %lt:-1;\\n\".\n\ |
5646
|
1836 @end defvr"); |
3308
|
1837 } |
|
1838 |
1
|
1839 /* |
|
1840 ;;; Local Variables: *** |
|
1841 ;;; mode: C++ *** |
|
1842 ;;; End: *** |
|
1843 */ |