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