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