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 |
|
843 symbol_table::clear_globals (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_user_variable () && ptr->is_linked_to_global ()) |
|
852 ptr->clear (); |
195
|
853 |
169
|
854 ptr = ptr->next (); |
1
|
855 } |
|
856 } |
|
857 } |
|
858 |
3013
|
859 bool |
4009
|
860 symbol_table::clear (const std::string& nm) |
|
861 { |
|
862 unsigned int index = hash (nm); |
|
863 |
|
864 symbol_record *ptr = table[index].next (); |
|
865 |
|
866 while (ptr) |
|
867 { |
|
868 if (ptr->name () == nm) |
|
869 { |
|
870 ptr->clear (); |
5397
|
871 |
4009
|
872 return true; |
|
873 } |
|
874 ptr = ptr->next (); |
|
875 } |
|
876 |
|
877 return false; |
|
878 } |
|
879 |
|
880 bool |
|
881 symbol_table::clear_variable (const std::string& nm) |
|
882 { |
|
883 unsigned int index = hash (nm); |
|
884 |
|
885 symbol_record *ptr = table[index].next (); |
|
886 |
|
887 while (ptr) |
|
888 { |
|
889 if (ptr->name () == nm && ptr->is_user_variable ()) |
|
890 { |
|
891 ptr->clear (); |
|
892 return true; |
|
893 } |
|
894 ptr = ptr->next (); |
|
895 } |
|
896 |
|
897 return false; |
|
898 } |
|
899 |
|
900 bool |
|
901 symbol_table::clear_global (const std::string& nm) |
1
|
902 { |
3013
|
903 unsigned int index = hash (nm); |
1
|
904 |
195
|
905 symbol_record *ptr = table[index].next (); |
1
|
906 |
530
|
907 while (ptr) |
1
|
908 { |
1755
|
909 if (ptr->name () == nm |
4009
|
910 && ptr->is_user_variable () |
|
911 && ptr->is_linked_to_global ()) |
|
912 { |
|
913 ptr->clear (); |
|
914 return true; |
|
915 } |
|
916 ptr = ptr->next (); |
|
917 } |
|
918 |
|
919 return false; |
|
920 } |
|
921 |
|
922 // Really only clear functions that can be reloaded. |
|
923 |
|
924 bool |
|
925 symbol_table::clear_function (const std::string& nm) |
|
926 { |
|
927 unsigned int index = hash (nm); |
|
928 |
|
929 symbol_record *ptr = table[index].next (); |
|
930 |
|
931 while (ptr) |
|
932 { |
|
933 if (ptr->name () == nm |
5864
|
934 && (ptr->is_user_function () |
|
935 || ptr->is_dld_function () |
|
936 || ptr->is_mex_function ())) |
1
|
937 { |
195
|
938 ptr->clear (); |
3013
|
939 return true; |
1
|
940 } |
195
|
941 ptr = ptr->next (); |
1
|
942 } |
|
943 |
3013
|
944 return false; |
1
|
945 } |
|
946 |
4009
|
947 bool |
|
948 symbol_table::clear_variable_pattern (const std::string& pat) |
|
949 { |
|
950 bool retval = false; |
|
951 |
|
952 for (unsigned int i = 0; i < table_size; i++) |
|
953 { |
|
954 symbol_record *ptr = table[i].next (); |
|
955 |
|
956 while (ptr) |
|
957 { |
|
958 if (ptr->is_user_variable ()) |
|
959 { |
|
960 glob_match pattern (pat); |
|
961 |
|
962 if (pattern.match (ptr->name ())) |
|
963 { |
|
964 ptr->clear (); |
|
965 |
|
966 retval = true; |
|
967 } |
|
968 } |
|
969 |
|
970 ptr = ptr->next (); |
|
971 } |
|
972 } |
|
973 |
|
974 return retval; |
|
975 } |
|
976 |
|
977 bool |
|
978 symbol_table::clear_global_pattern (const std::string& pat) |
|
979 { |
|
980 bool retval = false; |
|
981 |
|
982 for (unsigned int i = 0; i < table_size; i++) |
|
983 { |
|
984 symbol_record *ptr = table[i].next (); |
|
985 |
|
986 while (ptr) |
|
987 { |
|
988 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
989 { |
|
990 glob_match pattern (pat); |
|
991 |
|
992 if (pattern.match (ptr->name ())) |
|
993 { |
|
994 ptr->clear (); |
|
995 |
|
996 retval = true; |
|
997 } |
|
998 } |
|
999 |
|
1000 ptr = ptr->next (); |
|
1001 } |
|
1002 } |
|
1003 |
|
1004 return retval; |
|
1005 } |
|
1006 |
|
1007 // Really only clear functions that can be reloaded. |
|
1008 |
|
1009 bool |
|
1010 symbol_table::clear_function_pattern (const std::string& pat) |
|
1011 { |
|
1012 bool retval = false; |
|
1013 |
|
1014 for (unsigned int i = 0; i < table_size; i++) |
|
1015 { |
|
1016 symbol_record *ptr = table[i].next (); |
|
1017 |
|
1018 while (ptr) |
|
1019 { |
5864
|
1020 if (ptr->is_user_function () |
|
1021 || ptr->is_dld_function () |
|
1022 || ptr->is_mex_function ()) |
4009
|
1023 { |
|
1024 glob_match pattern (pat); |
|
1025 |
|
1026 if (pattern.match (ptr->name ())) |
|
1027 { |
|
1028 ptr->clear (); |
|
1029 |
|
1030 retval = true; |
|
1031 } |
|
1032 } |
|
1033 |
|
1034 ptr = ptr->next (); |
|
1035 } |
|
1036 } |
|
1037 |
|
1038 return retval; |
|
1039 } |
|
1040 |
1
|
1041 int |
164
|
1042 symbol_table::size (void) const |
1
|
1043 { |
|
1044 int count = 0; |
3013
|
1045 |
|
1046 for (unsigned int i = 0; i < table_size; i++) |
1
|
1047 { |
|
1048 symbol_record *ptr = table[i].next (); |
3013
|
1049 |
530
|
1050 while (ptr) |
1
|
1051 { |
|
1052 count++; |
|
1053 ptr = ptr->next (); |
|
1054 } |
|
1055 } |
3013
|
1056 |
1
|
1057 return count; |
|
1058 } |
|
1059 |
3013
|
1060 static bool |
3523
|
1061 matches_patterns (const std::string& name, const string_vector& pats) |
1
|
1062 { |
3013
|
1063 int npats = pats.length (); |
1
|
1064 |
3013
|
1065 if (npats == 0) |
|
1066 return true; |
|
1067 |
|
1068 glob_match pattern (pats); |
|
1069 |
|
1070 return pattern.match (name); |
1
|
1071 } |
|
1072 |
3013
|
1073 Array<symbol_record *> |
4913
|
1074 symbol_table::subsymbol_list (const string_vector& pats, |
|
1075 unsigned int type, unsigned int scope) const |
|
1076 { |
|
1077 int count = 0; |
|
1078 |
|
1079 int n = size (); |
|
1080 |
5659
|
1081 Array<symbol_record *> subsymbols (dim_vector (n, 1)); |
4913
|
1082 int pats_length = pats.length (); |
|
1083 |
|
1084 if (n == 0) |
|
1085 return subsymbols; |
|
1086 |
|
1087 // Look for separators like .({ |
|
1088 for (int j = 0; j < pats_length; j++) |
|
1089 { |
|
1090 std::string var_name = pats (j); |
|
1091 |
|
1092 size_t pos = var_name.find_first_of (".({"); |
|
1093 |
|
1094 if ((pos != NPOS) && (pos > 0)) |
|
1095 { |
|
1096 std::string first_name = var_name.substr(0,pos); |
|
1097 |
|
1098 for (unsigned int i = 0; i < table_size; i++) |
|
1099 { |
|
1100 symbol_record *ptr = table[i].next (); |
|
1101 |
|
1102 while (ptr) |
|
1103 { |
|
1104 assert (count < n); |
|
1105 |
|
1106 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
|
1107 |
|
1108 unsigned int my_type = ptr->type (); |
|
1109 |
|
1110 std::string my_name = ptr->name (); |
|
1111 |
|
1112 if ((type & my_type) && (scope & my_scope) && (first_name == my_name)) |
|
1113 { |
|
1114 symbol_record *sym_ptr = new symbol_record (); |
|
1115 octave_value value; |
|
1116 int parse_status; |
4914
|
1117 |
4913
|
1118 value = eval_string (var_name, true, parse_status); |
4914
|
1119 |
4913
|
1120 sym_ptr->define (value); |
|
1121 sym_ptr->rename (var_name); |
|
1122 subsymbols(count++) = sym_ptr; |
|
1123 } |
|
1124 |
|
1125 ptr = ptr->next (); |
|
1126 } |
|
1127 } |
|
1128 } |
|
1129 } |
|
1130 |
5659
|
1131 subsymbols.resize (dim_vector (count, 1)); |
4913
|
1132 |
|
1133 return subsymbols; |
|
1134 } |
|
1135 |
|
1136 Array<symbol_record *> |
3355
|
1137 symbol_table::symbol_list (const string_vector& pats, |
3013
|
1138 unsigned int type, unsigned int scope) const |
1
|
1139 { |
3355
|
1140 int count = 0; |
3013
|
1141 |
1
|
1142 int n = size (); |
3013
|
1143 |
5659
|
1144 Array<symbol_record *> symbols (dim_vector (n, 1)); |
3355
|
1145 |
1
|
1146 if (n == 0) |
3355
|
1147 return symbols; |
3013
|
1148 |
|
1149 for (unsigned int i = 0; i < table_size; i++) |
1
|
1150 { |
|
1151 symbol_record *ptr = table[i].next (); |
3013
|
1152 |
530
|
1153 while (ptr) |
1
|
1154 { |
5399
|
1155 if (ptr->is_visible ()) |
5397
|
1156 { |
|
1157 assert (count < n); |
867
|
1158 |
5397
|
1159 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
1160 |
5397
|
1161 unsigned int my_type = ptr->type (); |
|
1162 |
|
1163 std::string my_name = ptr->name (); |
867
|
1164 |
5397
|
1165 if ((type & my_type) && (scope & my_scope) && (matches_patterns (my_name, pats))) |
|
1166 symbols(count++) = ptr; |
|
1167 } |
5399
|
1168 |
1
|
1169 ptr = ptr->next (); |
|
1170 } |
|
1171 } |
1755
|
1172 |
5659
|
1173 symbols.resize (dim_vector (count, 1)); |
1
|
1174 |
3013
|
1175 return symbols; |
|
1176 } |
|
1177 |
|
1178 string_vector |
3355
|
1179 symbol_table::name_list (const string_vector& pats, bool sort, |
3013
|
1180 unsigned int type, unsigned int scope) const |
|
1181 { |
3355
|
1182 Array<symbol_record *> symbols = symbol_list (pats, type, scope); |
3013
|
1183 |
|
1184 string_vector names; |
|
1185 |
|
1186 int n = symbols.length (); |
|
1187 |
|
1188 if (n > 0) |
|
1189 { |
|
1190 names.resize (n); |
|
1191 |
|
1192 for (int i = 0; i < n; i++) |
|
1193 names[i] = symbols(i)->name (); |
|
1194 } |
|
1195 |
|
1196 if (sort) |
|
1197 names.qsort (); |
|
1198 |
|
1199 return names; |
|
1200 } |
|
1201 |
|
1202 static int |
3145
|
1203 maybe_list_cmp_fcn (const void *a_arg, const void *b_arg) |
3013
|
1204 { |
5817
|
1205 const symbol_record *a = *(static_cast<symbol_record *const*> (a_arg)); |
|
1206 const symbol_record *b = *(static_cast<symbol_record *const*> (b_arg)); |
3013
|
1207 |
3523
|
1208 std::string a_nm = a->name (); |
|
1209 std::string b_nm = b->name (); |
3145
|
1210 |
|
1211 return a_nm.compare (b_nm); |
3013
|
1212 } |
1
|
1213 |
4913
|
1214 void |
|
1215 symbol_table::print_descriptor (std::ostream& os, |
|
1216 std::list<whos_parameter> params) const |
|
1217 { |
|
1218 // This method prints a line of information on a given symbol |
|
1219 std::list<whos_parameter>::iterator i = params.begin (); |
5765
|
1220 std::ostringstream param_buf; |
4913
|
1221 |
|
1222 while (i != params.end ()) |
|
1223 { |
|
1224 whos_parameter param = * i; |
|
1225 |
|
1226 if (param.command != '\0') |
|
1227 { |
|
1228 // Do the actual printing |
|
1229 switch (param.modifier) |
|
1230 { |
|
1231 case 'l': |
|
1232 os << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1233 param_buf << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1234 break; |
|
1235 |
|
1236 case 'r': |
|
1237 os << std::setiosflags (std::ios::right) << std::setw (param.parameter_length); |
|
1238 param_buf << std::setiosflags (std::ios::right) << std::setw (param.parameter_length); |
|
1239 break; |
|
1240 |
|
1241 case 'c': |
4914
|
1242 if (param.command != 's') |
4913
|
1243 { |
|
1244 os << std::setiosflags (std::ios::left) |
|
1245 << std::setw (param.parameter_length); |
|
1246 param_buf << std::setiosflags (std::ios::left) |
|
1247 << std::setw (param.parameter_length); |
|
1248 } |
|
1249 break; |
|
1250 |
|
1251 default: |
|
1252 os << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1253 param_buf << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1254 } |
|
1255 |
|
1256 if (param.command == 's' && param.modifier == 'c') |
|
1257 { |
|
1258 int a, b; |
4914
|
1259 |
4913
|
1260 if (param.modifier == 'c') |
|
1261 { |
|
1262 a = param.first_parameter_length - param.balance; |
|
1263 a = (a < 0 ? 0 : a); |
|
1264 b = param.parameter_length - a - param.text . length (); |
|
1265 b = (b < 0 ? 0 : b); |
|
1266 os << std::setiosflags (std::ios::left) << std::setw (a) |
|
1267 << "" << std::resetiosflags (std::ios::left) << param.text |
|
1268 << std::setiosflags (std::ios::left) |
|
1269 << std::setw (b) << "" |
|
1270 << std::resetiosflags (std::ios::left); |
|
1271 param_buf << std::setiosflags (std::ios::left) << std::setw (a) |
|
1272 << "" << std::resetiosflags (std::ios::left) << param.line |
|
1273 << std::setiosflags (std::ios::left) |
|
1274 << std::setw (b) << "" |
|
1275 << std::resetiosflags (std::ios::left); |
|
1276 } |
|
1277 } |
|
1278 else |
|
1279 { |
|
1280 os << param.text; |
|
1281 param_buf << param.line; |
|
1282 } |
|
1283 os << std::resetiosflags (std::ios::left) |
|
1284 << std::resetiosflags (std::ios::right); |
|
1285 param_buf << std::resetiosflags (std::ios::left) |
|
1286 << std::resetiosflags (std::ios::right); |
|
1287 i++; |
|
1288 } |
|
1289 else |
|
1290 { |
|
1291 os << param.text; |
|
1292 param_buf << param.line; |
|
1293 i++; |
|
1294 } |
|
1295 } |
|
1296 |
5765
|
1297 os << param_buf.str (); |
4913
|
1298 } |
|
1299 |
|
1300 std::list<whos_parameter> |
|
1301 symbol_table::parse_whos_line_format (Array<symbol_record *>& symbols) const |
|
1302 { |
|
1303 // This method parses the string whos_line_format, and returns |
|
1304 // a parameter list, containing all information needed to print |
|
1305 // the given attributtes of the symbols |
|
1306 int idx; |
|
1307 size_t format_len = Vwhos_line_format.length (); |
|
1308 char garbage; |
|
1309 std::list<whos_parameter> params; |
|
1310 |
|
1311 size_t bytes1; |
|
1312 int elements1; |
|
1313 |
5817
|
1314 int len = symbols.length (); |
4913
|
1315 |
5817
|
1316 std::string param_string = "bcenpst"; |
5659
|
1317 Array<int> param_length (dim_vector (param_string.length (), 1)); |
|
1318 Array<std::string> param_names (dim_vector (param_string.length (), 1)); |
5817
|
1319 size_t pos_b, pos_c, pos_e, pos_n, pos_p, pos_s, pos_t; |
4913
|
1320 |
|
1321 pos_b = param_string.find ('b'); // Bytes |
5817
|
1322 pos_c = param_string.find ('c'); // Class |
4913
|
1323 pos_e = param_string.find ('e'); // Elements |
|
1324 pos_n = param_string.find ('n'); // Name |
|
1325 pos_p = param_string.find ('p'); // Protected |
|
1326 pos_s = param_string.find ('s'); // Size |
5817
|
1327 pos_t = param_string.find ('t'); // Type |
4913
|
1328 |
|
1329 param_names(pos_b) = "Bytes"; |
5817
|
1330 param_names(pos_c) = "Class"; |
4913
|
1331 param_names(pos_e) = "Elements"; |
|
1332 param_names(pos_n) = "Name"; |
|
1333 param_names(pos_p) = "Prot"; |
|
1334 param_names(pos_s) = "Size"; |
5817
|
1335 param_names(pos_t) = "Type"; |
4913
|
1336 |
5817
|
1337 for (size_t i = 0; i < param_string.length (); i++) |
4913
|
1338 param_length(i) = param_names(i) . length (); |
|
1339 |
|
1340 // Calculating necessary spacing for name column, |
|
1341 // bytes column, elements column and class column |
5817
|
1342 for (int i = 0; i < static_cast<int> (len); i++) |
4913
|
1343 { |
|
1344 std::stringstream ss1, ss2; |
|
1345 std::string str; |
|
1346 |
|
1347 str = symbols(i)->name (); |
5049
|
1348 param_length(pos_n) = ((str.length () |
|
1349 > static_cast<size_t> (param_length(pos_n))) |
|
1350 ? str.length () : param_length(pos_n)); |
4913
|
1351 |
|
1352 str = symbols(i)->type_name (); |
5049
|
1353 param_length(pos_t) = ((str.length () |
|
1354 > static_cast<size_t> (param_length(pos_t))) |
|
1355 ? str.length () : param_length(pos_t)); |
4913
|
1356 |
5164
|
1357 elements1 = symbols(i)->capacity (); |
4913
|
1358 ss1 << elements1; |
|
1359 str = ss1.str (); |
5049
|
1360 param_length(pos_e) = ((str.length () |
|
1361 > static_cast<size_t> (param_length(pos_e))) |
|
1362 ? str.length () : param_length(pos_e)); |
4913
|
1363 |
|
1364 bytes1 = symbols(i)->byte_size (); |
|
1365 ss2 << bytes1; |
|
1366 str = ss2.str (); |
5049
|
1367 param_length(pos_b) = ((str.length () |
|
1368 > static_cast<size_t> (param_length(pos_b))) |
|
1369 ? str.length () : param_length (pos_b)); |
4913
|
1370 } |
|
1371 |
|
1372 idx = 0; |
|
1373 while (static_cast<size_t> (idx) < format_len) |
|
1374 { |
|
1375 whos_parameter param; |
|
1376 param.command = '\0'; |
|
1377 |
|
1378 if (Vwhos_line_format[idx] == '%') |
4914
|
1379 { |
|
1380 bool error_encountered = false; |
4913
|
1381 param.modifier = 'r'; |
|
1382 param.parameter_length = 0; |
|
1383 param.dimensions = 8; |
|
1384 |
|
1385 int a = 0, b = -1, c = 8, balance = 1; |
|
1386 unsigned int items; |
|
1387 size_t pos; |
|
1388 std::string cmd; |
|
1389 |
|
1390 // Parse one command from whos_line_format |
|
1391 cmd = Vwhos_line_format.substr (idx, Vwhos_line_format.length ()); |
|
1392 pos = cmd.find (';'); |
|
1393 if (pos != NPOS) |
|
1394 cmd = cmd.substr (0, pos+1); |
|
1395 else |
|
1396 error ("parameter without ; in whos_line_format"); |
|
1397 |
|
1398 idx += cmd.length (); |
4914
|
1399 |
5775
|
1400 // FIXME -- use iostream functions instead of sscanf! |
4914
|
1401 |
4913
|
1402 if (cmd.find_first_of ("crl") != 1) |
|
1403 items = sscanf (cmd.c_str (), "%c%c:%d:%d:%d:%d;", |
|
1404 &garbage, ¶m.command, &a, &b, &c, &balance); |
|
1405 else |
|
1406 items = sscanf (cmd.c_str (), "%c%c%c:%d:%d:%d:%d;", |
4914
|
1407 &garbage, ¶m.modifier, ¶m.command, |
|
1408 &a, &b, &c, &balance) - 1; |
|
1409 |
4913
|
1410 if (items < 2) |
|
1411 { |
|
1412 error ("whos_line_format: parameter structure without command in whos_line_format"); |
4914
|
1413 error_encountered = true; |
4913
|
1414 } |
|
1415 |
|
1416 // Insert data into parameter |
|
1417 param.first_parameter_length = 0; |
|
1418 pos = param_string.find (param.command); |
|
1419 if (pos != NPOS) |
|
1420 { |
|
1421 param.parameter_length = param_length(pos); |
|
1422 param.text = param_names(pos); |
5049
|
1423 param.line.assign (param_names(pos).length (), '='); |
5048
|
1424 |
5049
|
1425 param.parameter_length = (a > param.parameter_length |
|
1426 ? a : param.parameter_length); |
|
1427 if (param.command == 's' && param.modifier == 'c' && b > 0) |
5048
|
1428 param.first_parameter_length = b; |
4913
|
1429 } |
|
1430 else |
|
1431 { |
|
1432 error ("whos_line_format: '%c' is not a command", |
|
1433 param.command); |
4914
|
1434 error_encountered = true; |
4913
|
1435 } |
|
1436 |
|
1437 if (param.command == 's') |
|
1438 { |
|
1439 // Have to calculate space needed for printing matrix dimensions |
|
1440 // Space needed for Size column is hard to determine in prior, |
|
1441 // because it depends on dimensions to be shown. That is why it is |
|
1442 // recalculated for each Size-command |
5048
|
1443 int j, first, rest = 0, total; |
4913
|
1444 param.dimensions = c; |
5048
|
1445 first = param.first_parameter_length; |
|
1446 total = param.parameter_length; |
4914
|
1447 |
4913
|
1448 for (j = 0; j < len; j++) |
5050
|
1449 { |
|
1450 int first1 = symbols(j)->dimensions_string_req_first_space (param.dimensions); |
|
1451 int total1 = symbols(j)->dimensions_string_req_total_space (param.dimensions); |
|
1452 int rest1 = total1 - first1; |
|
1453 rest = (rest1 > rest ? rest1 : rest); |
|
1454 first = (first1 > first ? first1 : first); |
|
1455 total = (total1 > total ? total1 : total); |
|
1456 } |
4913
|
1457 |
|
1458 if (param.modifier == 'c') |
|
1459 { |
|
1460 if (first < balance) |
|
1461 first += balance - first; |
|
1462 if (rest + balance < param.parameter_length) |
|
1463 rest += param.parameter_length - rest - balance; |
|
1464 |
|
1465 param.parameter_length = first + rest; |
|
1466 param.first_parameter_length = first; |
|
1467 param.balance = balance; |
|
1468 } |
|
1469 else |
|
1470 { |
|
1471 param.parameter_length = total; |
|
1472 param.first_parameter_length = 0; |
|
1473 } |
|
1474 } |
|
1475 else if (param.modifier == 'c') |
|
1476 { |
|
1477 error ("whos_line_format: modifier 'c' not available for command '%c'", |
|
1478 param.command); |
4914
|
1479 error_encountered = true; |
4913
|
1480 } |
|
1481 |
|
1482 // What happens if whos_line_format contains negative numbers |
|
1483 // at param_length positions? |
5049
|
1484 param.balance = (b < 0 ? 0 : param.balance); |
|
1485 param.first_parameter_length = (b < 0 ? 0 : |
4913
|
1486 param.first_parameter_length); |
5049
|
1487 param.parameter_length = (a < 0 |
|
1488 ? 0 |
|
1489 : (param.parameter_length |
|
1490 < param_length(pos_s) |
|
1491 ? param_length(pos_s) |
|
1492 : param.parameter_length)); |
4913
|
1493 |
|
1494 // Parameter will not be pushed into parameter list if ... |
4914
|
1495 if (! error_encountered) |
4913
|
1496 params.push_back (param); |
|
1497 } |
|
1498 else |
|
1499 { |
|
1500 // Text string, to be printed as it is ... |
|
1501 std::string text; |
|
1502 size_t pos; |
|
1503 text = Vwhos_line_format.substr (idx, Vwhos_line_format.length ()); |
|
1504 pos = text.find ('%'); |
|
1505 if (pos != NPOS) |
|
1506 text = text.substr (0, pos); |
|
1507 |
|
1508 // Push parameter into list ... |
|
1509 idx += text.length (); |
|
1510 param.text=text; |
|
1511 param.line.assign (text.length(), ' '); |
|
1512 params.push_back (param); |
|
1513 } |
|
1514 } |
|
1515 |
|
1516 return params; |
|
1517 } |
|
1518 |
3013
|
1519 int |
|
1520 symbol_table::maybe_list (const char *header, const string_vector& argv, |
3523
|
1521 std::ostream& os, bool show_verbose, |
3013
|
1522 unsigned type, unsigned scope) |
|
1523 { |
4914
|
1524 // This method prints information for sets of symbols, but only one |
|
1525 // set at a time (like, for instance: all variables, or all |
|
1526 // built-in-functions). |
4913
|
1527 |
4914
|
1528 // This method invokes print_symbol_info_line to print info on every |
|
1529 // symbol. |
4913
|
1530 |
3013
|
1531 int status = 0; |
|
1532 |
|
1533 if (show_verbose) |
|
1534 { |
5775
|
1535 // FIXME Should separate argv to lists with and without dots. |
4914
|
1536 Array<symbol_record *> xsymbols = symbol_list (argv, type, scope); |
|
1537 Array<symbol_record *> xsubsymbols = subsymbol_list (argv, type, scope); |
3013
|
1538 |
4914
|
1539 int sym_len = xsymbols.length (), subsym_len = xsubsymbols.length (), |
4913
|
1540 len = sym_len + subsym_len; |
4914
|
1541 |
5659
|
1542 Array<symbol_record *> symbols (dim_vector (len, 1)); |
3013
|
1543 |
3355
|
1544 if (len > 0) |
3013
|
1545 { |
5005
|
1546 size_t bytes = 0; |
|
1547 size_t elements = 0; |
|
1548 |
|
1549 int i; |
|
1550 |
4913
|
1551 std::list<whos_parameter> params; |
|
1552 |
4914
|
1553 // Joining symbolic tables. |
4913
|
1554 for (i = 0; i < sym_len; i++) |
4914
|
1555 symbols(i) = xsymbols(i); |
4913
|
1556 |
|
1557 for (i = 0; i < subsym_len; i++) |
4914
|
1558 symbols(i+sym_len) = xsubsymbols(i); |
4913
|
1559 |
|
1560 os << "\n" << header << "\n\n"; |
3013
|
1561 |
|
1562 symbols.qsort (maybe_list_cmp_fcn); |
|
1563 |
4913
|
1564 params = parse_whos_line_format (symbols); |
|
1565 |
|
1566 print_descriptor (os, params); |
|
1567 |
|
1568 os << "\n"; |
|
1569 |
|
1570 for (int j = 0; j < len; j++) |
|
1571 { |
|
1572 symbols(j)->print_symbol_info_line (os, params); |
5164
|
1573 elements += symbols(j)->capacity (); |
4913
|
1574 bytes += symbols(j)->byte_size (); |
|
1575 } |
|
1576 |
5005
|
1577 os << "\nTotal is " |
5006
|
1578 << elements << (elements == 1 ? " element" : " elements") |
5005
|
1579 << " using " |
5006
|
1580 << bytes << (bytes == 1 ? " byte" : " bytes") |
5005
|
1581 << "\n"; |
3013
|
1582 |
|
1583 status = 1; |
|
1584 } |
|
1585 } |
|
1586 else |
|
1587 { |
3355
|
1588 string_vector symbols = name_list (argv, 1, type, scope); |
3013
|
1589 |
3355
|
1590 if (! symbols.empty ()) |
4914
|
1591 { |
3013
|
1592 os << "\n" << header << "\n\n"; |
|
1593 |
|
1594 symbols.list_in_columns (os); |
|
1595 |
|
1596 status = 1; |
|
1597 } |
|
1598 } |
|
1599 |
|
1600 return status; |
1
|
1601 } |
|
1602 |
3355
|
1603 Array<symbol_record *> |
3523
|
1604 symbol_table::glob (const std::string& pat, unsigned int type, |
2893
|
1605 unsigned int scope) const |
605
|
1606 { |
3355
|
1607 int count = 0; |
|
1608 |
605
|
1609 int n = size (); |
3355
|
1610 |
5659
|
1611 Array<symbol_record *> symbols (dim_vector (n, 1)); |
3355
|
1612 |
605
|
1613 if (n == 0) |
3355
|
1614 return symbols; |
3013
|
1615 |
|
1616 for (unsigned int i = 0; i < table_size; i++) |
605
|
1617 { |
|
1618 symbol_record *ptr = table[i].next (); |
3013
|
1619 |
605
|
1620 while (ptr) |
|
1621 { |
|
1622 assert (count < n); |
|
1623 |
2893
|
1624 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
1625 |
2893
|
1626 unsigned int my_type = ptr->type (); |
605
|
1627 |
1792
|
1628 glob_match pattern (pat); |
1755
|
1629 |
605
|
1630 if ((type & my_type) && (scope & my_scope) |
1792
|
1631 && pattern.match (ptr->name ())) |
605
|
1632 { |
3355
|
1633 symbols(count++) = ptr; |
605
|
1634 } |
|
1635 |
|
1636 ptr = ptr->next (); |
|
1637 } |
|
1638 } |
3355
|
1639 |
5659
|
1640 symbols.resize (dim_vector (count, 1)); |
605
|
1641 |
|
1642 return symbols; |
|
1643 } |
|
1644 |
220
|
1645 void |
|
1646 symbol_table::push_context (void) |
|
1647 { |
3013
|
1648 for (unsigned int i = 0; i < table_size; i++) |
220
|
1649 { |
|
1650 symbol_record *ptr = table[i].next (); |
|
1651 |
530
|
1652 while (ptr) |
220
|
1653 { |
|
1654 ptr->push_context (); |
|
1655 ptr = ptr->next (); |
|
1656 } |
|
1657 } |
|
1658 } |
|
1659 |
|
1660 void |
|
1661 symbol_table::pop_context (void) |
|
1662 { |
3013
|
1663 for (unsigned int i = 0; i < table_size; i++) |
220
|
1664 { |
|
1665 symbol_record *ptr = table[i].next (); |
|
1666 |
530
|
1667 while (ptr) |
220
|
1668 { |
|
1669 ptr->pop_context (); |
|
1670 ptr = ptr->next (); |
|
1671 } |
|
1672 } |
|
1673 } |
|
1674 |
5861
|
1675 // Create a new symbol table with the same entries. Only the symbol |
|
1676 // names and some attributes are copied, not values. |
|
1677 |
|
1678 symbol_table * |
|
1679 symbol_table::dup (void) |
|
1680 { |
|
1681 symbol_table *new_sym_tab = new symbol_table (table_size); |
|
1682 |
|
1683 for (unsigned int i = 0; i < table_size; i++) |
|
1684 { |
|
1685 symbol_record *ptr = table[i].next (); |
|
1686 |
|
1687 while (ptr) |
|
1688 { |
|
1689 std::string nm = ptr->name (); |
|
1690 |
|
1691 symbol_record *sr = new_sym_tab->lookup (nm, true); |
|
1692 |
|
1693 if (sr) |
|
1694 { |
|
1695 if (ptr->is_formal_parameter ()) |
|
1696 sr->mark_as_formal_parameter (); |
|
1697 |
|
1698 if (ptr->is_automatic_variable ()) |
|
1699 sr->mark_as_automatic_variable (); |
|
1700 |
|
1701 if (ptr->is_static ()) |
|
1702 sr->mark_as_static (); |
|
1703 } |
|
1704 |
|
1705 ptr = ptr->next (); |
|
1706 } |
|
1707 } |
|
1708 |
|
1709 return new_sym_tab; |
|
1710 } |
|
1711 |
|
1712 void |
|
1713 symbol_table::inherit (symbol_table *parent_sym_tab) |
|
1714 { |
|
1715 for (unsigned int i = 0; i < table_size; i++) |
|
1716 { |
|
1717 symbol_record *ptr = table[i].next (); |
|
1718 |
|
1719 while (ptr) |
|
1720 { |
|
1721 std::string nm = ptr->name (); |
|
1722 |
|
1723 if (! (nm == "__retval__" |
|
1724 || ptr->is_automatic_variable () |
|
1725 || ptr->is_formal_parameter ())) |
|
1726 { |
|
1727 symbol_record *sr = parent_sym_tab->lookup (nm); |
|
1728 |
|
1729 if (sr) |
|
1730 { |
|
1731 ptr->define (sr->variable_value ()); |
|
1732 |
|
1733 ptr->mark_as_static (); |
|
1734 } |
|
1735 } |
|
1736 |
|
1737 ptr = ptr->next (); |
|
1738 } |
|
1739 } |
|
1740 } |
|
1741 |
3013
|
1742 void |
3944
|
1743 symbol_table::print_info (std::ostream& os) const |
3013
|
1744 { |
|
1745 int count = 0; |
|
1746 int empty_chains = 0; |
|
1747 int max_chain_length = 0; |
|
1748 int min_chain_length = INT_MAX; |
|
1749 |
|
1750 for (unsigned int i = 0; i < table_size; i++) |
|
1751 { |
|
1752 int num_this_chain = 0; |
|
1753 |
|
1754 symbol_record *ptr = table[i].next (); |
|
1755 |
|
1756 if (ptr) |
3933
|
1757 os << "chain number " << i << ":\n"; |
3013
|
1758 else |
|
1759 { |
|
1760 empty_chains++; |
|
1761 min_chain_length = 0; |
|
1762 } |
|
1763 |
|
1764 while (ptr) |
|
1765 { |
|
1766 num_this_chain++; |
|
1767 |
3933
|
1768 os << " " << ptr->name () << "\n"; |
|
1769 |
|
1770 ptr->print_info (os, " "); |
3013
|
1771 |
|
1772 ptr = ptr->next (); |
|
1773 } |
|
1774 |
|
1775 count += num_this_chain; |
|
1776 |
|
1777 if (num_this_chain > max_chain_length) |
|
1778 max_chain_length = num_this_chain; |
|
1779 |
|
1780 if (num_this_chain < min_chain_length) |
|
1781 min_chain_length = num_this_chain; |
|
1782 |
|
1783 if (num_this_chain > 0) |
3933
|
1784 os << "\n"; |
3013
|
1785 } |
|
1786 |
3933
|
1787 os << "max chain length: " << max_chain_length << "\n"; |
|
1788 os << "min chain length: " << min_chain_length << "\n"; |
|
1789 os << "empty chains: " << empty_chains << "\n"; |
|
1790 os << "total chains: " << table_size << "\n"; |
|
1791 os << "total symbols: " << count << "\n"; |
3013
|
1792 } |
|
1793 |
1
|
1794 // Chris Torek's fave hash function. |
|
1795 |
|
1796 unsigned int |
3523
|
1797 symbol_table::hash (const std::string& str) |
1
|
1798 { |
2893
|
1799 unsigned int h = 0; |
3013
|
1800 |
2893
|
1801 for (unsigned int i = 0; i < str.length (); i++) |
1755
|
1802 h = h * 33 + str[i]; |
1
|
1803 |
3013
|
1804 return h & (table_size - 1); |
2790
|
1805 } |
|
1806 |
5794
|
1807 DEFUN (debug_symtab_lookups, args, nargout, |
|
1808 "-*- texinfo -*-\n\ |
|
1809 @deftypefn {Built-in Function} {@var{val} =} debug_symtab_lookups ()\n\ |
|
1810 @deftypefnx {Built-in Function} {@var{old_val} =} debug_symtab_lookups (@var{new_val})\n\ |
|
1811 Query or set the internal variable that controls whether debugging\n\ |
|
1812 information is printed when searching for symbols in the symbol tables.\n\ |
|
1813 @end deftypefn") |
3308
|
1814 { |
5794
|
1815 return SET_INTERNAL_VARIABLE (debug_symtab_lookups); |
4238
|
1816 } |
|
1817 |
5794
|
1818 DEFUN (whos_line_format, args, nargout, |
|
1819 "-*- texinfo -*-\n\ |
|
1820 @deftypefn {Built-in Function} {@var{val} =} whos_line_format ()\n\ |
|
1821 @deftypefnx {Built-in Function} {@var{old_val} =} whos_line_format (@var{new_val})\n\ |
|
1822 Query or set the format string used by the @code{whos}.\n\ |
|
1823 \n\ |
|
1824 The following escape sequences may be used in the format:\n\ |
4913
|
1825 @table @code\n\ |
|
1826 @item %b\n\ |
|
1827 Prints number of bytes occupied by variables.\n\ |
5817
|
1828 @item %c\n\ |
|
1829 Prints class names of variables.\n\ |
4913
|
1830 @item %e\n\ |
|
1831 Prints elements held by variables.\n\ |
|
1832 @item %n\n\ |
|
1833 Prints variable names.\n\ |
|
1834 @item %p\n\ |
|
1835 Prints protection attributtes of variables.\n\ |
|
1836 @item %s\n\ |
|
1837 Prints dimensions of variables.\n\ |
|
1838 @item %t\n\ |
|
1839 Prints type names of variables.\n\ |
|
1840 @end table\n\ |
|
1841 \n\ |
|
1842 Every command may also have a modifier:\n\ |
|
1843 @table @code\n\ |
|
1844 @item l\n\ |
|
1845 Left alignment.\n\ |
|
1846 @item r\n\ |
|
1847 Right alignment (this is the default).\n\ |
|
1848 @item c\n\ |
|
1849 Centered (may only be applied to command %s).\n\ |
|
1850 @end table\n\ |
|
1851 \n\ |
|
1852 A command is composed like this:\n\ |
|
1853 %[modifier]<command>[:size_of_parameter[:center-specific[:print_dims[:balance]]]];\n\ |
|
1854 \n\ |
|
1855 Command and modifier is already explained. Size_of_parameter\n\ |
|
1856 tells how many columns the parameter will need for printing.\n\ |
|
1857 print_dims tells how many dimensions to print. If number of\n\ |
|
1858 dimensions exceeds print_dims, dimensions will be printed like\n\ |
|
1859 x-D.\n\ |
|
1860 center-specific and print_dims may only be applied to command\n\ |
|
1861 %s. A negative value for print_dims will cause Octave to print all\n\ |
|
1862 dimensions whatsoever.\n\ |
|
1863 balance specifies the offset for printing of the dimensions string.\n\ |
|
1864 \n\ |
5817
|
1865 The default format is \" %p:4; %ln:6; %cs:16:6:8:1; %rb:12; %lc:-1;\\n\".\n\ |
5794
|
1866 @end deftypefn") |
|
1867 { |
|
1868 return SET_INTERNAL_VARIABLE (whos_line_format); |
|
1869 } |
|
1870 |
|
1871 DEFUN (variables_can_hide_functions, args, nargout, |
|
1872 "-*- texinfo -*-\n\ |
|
1873 @deftypefn {Built-in Function} {@var{val} =} variables_can_hide_functions ()\n\ |
|
1874 @deftypefnx {Built-in Function} {@var{old_val} =} variables_can_hide_functions (@var{new_val})\n\ |
|
1875 Query or set the internal variable that controls whether assignments\n\ |
|
1876 to variables may hide previously defined functions of the same name.\n\ |
|
1877 If set to a nonzero value allows hiding, zero causes Octave to\n\ |
|
1878 generate an error, and a negative value cause Octave to print a\n\ |
|
1879 warning, but allow the operation.\n\ |
|
1880 @end deftypefn") |
|
1881 { |
|
1882 return SET_INTERNAL_VARIABLE (variables_can_hide_functions); |
3308
|
1883 } |
|
1884 |
1
|
1885 /* |
|
1886 ;;; Local Variables: *** |
|
1887 ;;; mode: C++ *** |
|
1888 ;;; End: *** |
|
1889 */ |