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