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