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