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