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