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