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