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