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 |
|
66 = " %p:4; %ln:6; %cs:16:6:8:1; %rb:12; %lt:-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 |
|
568 case 'e': |
5164
|
569 os << capacity (); |
4913
|
570 break; |
|
571 |
|
572 case 'n': |
|
573 os << name (); |
|
574 break; |
3013
|
575 |
4913
|
576 case 'p': |
|
577 { |
|
578 std::stringstream ss; |
|
579 std::string str; |
|
580 |
|
581 ss << (is_read_only () ? "r-" : "rw") |
|
582 << (is_static () || is_eternal () ? "-" : "d"); |
|
583 str = ss.str (); |
|
584 |
|
585 os << str; |
|
586 } |
|
587 break; |
|
588 |
|
589 case 's': |
|
590 if (param.modifier != 'c') |
|
591 os << make_dimensions_string (param.dimensions); |
|
592 break; |
3013
|
593 |
4913
|
594 case 't': |
|
595 os << type_name (); |
|
596 break; |
4914
|
597 |
|
598 default: |
4913
|
599 error ("whos_line_format: command `%c' unknown", param.command); |
|
600 } |
|
601 |
|
602 os << std::resetiosflags (std::ios::left) |
|
603 << std::resetiosflags (std::ios::right); |
|
604 i++; |
|
605 } |
|
606 else |
|
607 { |
|
608 os << param.text; |
|
609 i++; |
|
610 } |
|
611 } |
3013
|
612 } |
|
613 |
3239
|
614 void |
3944
|
615 symbol_record::print_info (std::ostream& os, const std::string& prefix) const |
3239
|
616 { |
5399
|
617 os << prefix << "formal param: " << formal_param << "\n" |
|
618 << prefix << "linked to global: " << linked_to_global << "\n" |
|
619 << prefix << "tagged static: " << tagged_static << "\n" |
|
620 << prefix << "can hide function: " << can_hide_function << "\n" |
|
621 << prefix << "visible: " << visible << "\n"; |
|
622 |
3239
|
623 if (definition) |
3933
|
624 definition->print_info (os, prefix); |
3239
|
625 else |
3933
|
626 os << prefix << "symbol " << name () << " is undefined\n"; |
3239
|
627 } |
|
628 |
3013
|
629 bool |
2791
|
630 symbol_record::read_only_error (const char *action) |
195
|
631 { |
|
632 if (is_read_only ()) |
|
633 { |
5780
|
634 if (is_variable ()) |
3013
|
635 ::error ("can't %s read-only constant `%s'", action, nm.c_str ()); |
195
|
636 else if (is_function ()) |
3013
|
637 ::error ("can't %s read-only function `%s'", action, nm.c_str ()); |
1045
|
638 else |
3013
|
639 ::error ("can't %s read-only symbol `%s'", action, nm.c_str ()); |
195
|
640 |
3013
|
641 return true; |
195
|
642 } |
|
643 else |
3013
|
644 return false; |
195
|
645 } |
|
646 |
767
|
647 // A symbol table. |
1
|
648 |
5013
|
649 symbol_table::~symbol_table (void) |
|
650 { |
|
651 for (unsigned int i = 0; i < table_size; i++) |
|
652 { |
|
653 symbol_record *ptr = table[i].next (); |
|
654 |
|
655 while (ptr) |
|
656 { |
|
657 symbol_record *tmp = ptr; |
|
658 |
|
659 ptr = ptr->next (); |
|
660 |
|
661 delete tmp; |
|
662 } |
|
663 } |
|
664 |
|
665 delete [] table; |
|
666 } |
|
667 |
1
|
668 symbol_record * |
3523
|
669 symbol_table::lookup (const std::string& nm, bool insert, bool warn) |
1
|
670 { |
4238
|
671 if (Vdebug_symtab_lookups) |
|
672 { |
4269
|
673 std::cerr << (table_name.empty () ? std::string ("???") : table_name) |
4238
|
674 << " symtab::lookup [" |
|
675 << (insert ? "I" : "-") |
|
676 << (warn ? "W" : "-") |
|
677 << "] \"" << nm << "\"\n"; |
|
678 } |
|
679 |
3013
|
680 unsigned int index = hash (nm); |
1
|
681 |
|
682 symbol_record *ptr = table[index].next (); |
|
683 |
530
|
684 while (ptr) |
1
|
685 { |
1755
|
686 if (ptr->name () == nm) |
1
|
687 return ptr; |
3013
|
688 |
1
|
689 ptr = ptr->next (); |
|
690 } |
|
691 |
|
692 if (insert) |
|
693 { |
3013
|
694 symbol_record *sr = new symbol_record (nm, table[index].next ()); |
|
695 |
|
696 table[index].chain (sr); |
|
697 |
|
698 return sr; |
1
|
699 } |
|
700 else if (warn) |
3356
|
701 warning ("lookup: symbol `%s' not found", nm.c_str ()); |
1
|
702 |
530
|
703 return 0; |
1
|
704 } |
|
705 |
|
706 void |
3523
|
707 symbol_table::rename (const std::string& old_name, const std::string& new_name) |
572
|
708 { |
4238
|
709 if (Vdebug_symtab_lookups) |
|
710 { |
4269
|
711 std::cerr << (table_name.empty () ? std::string ("???") : table_name) |
4238
|
712 << " symtab::rename " |
|
713 << "\"" << old_name << "\"" |
|
714 << " to " |
|
715 << "\"" << new_name << "\"\n"; |
|
716 } |
|
717 |
3013
|
718 unsigned int index = hash (old_name); |
572
|
719 |
|
720 symbol_record *prev = &table[index]; |
|
721 symbol_record *ptr = prev->next (); |
|
722 |
|
723 while (ptr) |
|
724 { |
1755
|
725 if (ptr->name () == old_name) |
572
|
726 { |
|
727 ptr->rename (new_name); |
|
728 |
2791
|
729 if (! error_state) |
|
730 { |
|
731 prev->chain (ptr->next ()); |
|
732 |
3013
|
733 index = hash (new_name); |
3125
|
734 ptr->chain (table[index].next ()); |
2791
|
735 table[index].chain (ptr); |
|
736 |
|
737 return; |
|
738 } |
|
739 |
|
740 break; |
572
|
741 } |
|
742 |
|
743 prev = ptr; |
|
744 ptr = ptr->next (); |
|
745 } |
|
746 |
1755
|
747 error ("unable to rename `%s' to `%s'", old_name.c_str (), |
|
748 new_name.c_str ()); |
572
|
749 } |
|
750 |
5775
|
751 // FIXME -- it would be nice to eliminate a lot of the |
4009
|
752 // following duplicate code. |
|
753 |
572
|
754 void |
4009
|
755 symbol_table::clear (void) |
|
756 { |
|
757 for (unsigned int i = 0; i < table_size; i++) |
|
758 { |
|
759 symbol_record *ptr = table[i].next (); |
|
760 |
|
761 while (ptr) |
|
762 { |
|
763 ptr->clear (); |
|
764 |
|
765 ptr = ptr->next (); |
|
766 } |
|
767 } |
|
768 } |
|
769 |
|
770 void |
|
771 symbol_table::clear_variables (void) |
|
772 { |
|
773 for (unsigned int i = 0; i < table_size; i++) |
|
774 { |
|
775 symbol_record *ptr = table[i].next (); |
|
776 |
|
777 while (ptr) |
|
778 { |
|
779 if (ptr->is_user_variable ()) |
|
780 ptr->clear (); |
|
781 |
|
782 ptr = ptr->next (); |
|
783 } |
|
784 } |
|
785 } |
|
786 |
|
787 // Really only clear functions that can be reloaded. |
|
788 |
|
789 void |
|
790 symbol_table::clear_functions (void) |
1
|
791 { |
3013
|
792 for (unsigned int i = 0; i < table_size; i++) |
1
|
793 { |
169
|
794 symbol_record *ptr = table[i].next (); |
1
|
795 |
530
|
796 while (ptr) |
169
|
797 { |
4009
|
798 if (ptr->is_user_function () || ptr->is_dld_function ()) |
|
799 ptr->clear (); |
|
800 |
|
801 ptr = ptr->next (); |
|
802 } |
|
803 } |
|
804 } |
|
805 |
|
806 void |
|
807 symbol_table::clear_globals (void) |
|
808 { |
|
809 for (unsigned int i = 0; i < table_size; i++) |
|
810 { |
|
811 symbol_record *ptr = table[i].next (); |
|
812 |
|
813 while (ptr) |
|
814 { |
|
815 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
816 ptr->clear (); |
195
|
817 |
169
|
818 ptr = ptr->next (); |
1
|
819 } |
|
820 } |
|
821 } |
|
822 |
3013
|
823 bool |
4009
|
824 symbol_table::clear (const std::string& nm) |
|
825 { |
|
826 unsigned int index = hash (nm); |
|
827 |
|
828 symbol_record *ptr = table[index].next (); |
|
829 |
|
830 while (ptr) |
|
831 { |
|
832 if (ptr->name () == nm) |
|
833 { |
|
834 ptr->clear (); |
5397
|
835 |
4009
|
836 return true; |
|
837 } |
|
838 ptr = ptr->next (); |
|
839 } |
|
840 |
|
841 return false; |
|
842 } |
|
843 |
|
844 bool |
|
845 symbol_table::clear_variable (const std::string& nm) |
|
846 { |
|
847 unsigned int index = hash (nm); |
|
848 |
|
849 symbol_record *ptr = table[index].next (); |
|
850 |
|
851 while (ptr) |
|
852 { |
|
853 if (ptr->name () == nm && ptr->is_user_variable ()) |
|
854 { |
|
855 ptr->clear (); |
|
856 return true; |
|
857 } |
|
858 ptr = ptr->next (); |
|
859 } |
|
860 |
|
861 return false; |
|
862 } |
|
863 |
|
864 bool |
|
865 symbol_table::clear_global (const std::string& nm) |
1
|
866 { |
3013
|
867 unsigned int index = hash (nm); |
1
|
868 |
195
|
869 symbol_record *ptr = table[index].next (); |
1
|
870 |
530
|
871 while (ptr) |
1
|
872 { |
1755
|
873 if (ptr->name () == nm |
4009
|
874 && ptr->is_user_variable () |
|
875 && ptr->is_linked_to_global ()) |
|
876 { |
|
877 ptr->clear (); |
|
878 return true; |
|
879 } |
|
880 ptr = ptr->next (); |
|
881 } |
|
882 |
|
883 return false; |
|
884 } |
|
885 |
|
886 // Really only clear functions that can be reloaded. |
|
887 |
|
888 bool |
|
889 symbol_table::clear_function (const std::string& nm) |
|
890 { |
|
891 unsigned int index = hash (nm); |
|
892 |
|
893 symbol_record *ptr = table[index].next (); |
|
894 |
|
895 while (ptr) |
|
896 { |
|
897 if (ptr->name () == nm |
|
898 && (ptr->is_user_function () || ptr->is_dld_function ())) |
1
|
899 { |
195
|
900 ptr->clear (); |
3013
|
901 return true; |
1
|
902 } |
195
|
903 ptr = ptr->next (); |
1
|
904 } |
|
905 |
3013
|
906 return false; |
1
|
907 } |
|
908 |
4009
|
909 bool |
|
910 symbol_table::clear_variable_pattern (const std::string& pat) |
|
911 { |
|
912 bool retval = false; |
|
913 |
|
914 for (unsigned int i = 0; i < table_size; i++) |
|
915 { |
|
916 symbol_record *ptr = table[i].next (); |
|
917 |
|
918 while (ptr) |
|
919 { |
|
920 if (ptr->is_user_variable ()) |
|
921 { |
|
922 glob_match pattern (pat); |
|
923 |
|
924 if (pattern.match (ptr->name ())) |
|
925 { |
|
926 ptr->clear (); |
|
927 |
|
928 retval = true; |
|
929 } |
|
930 } |
|
931 |
|
932 ptr = ptr->next (); |
|
933 } |
|
934 } |
|
935 |
|
936 return retval; |
|
937 } |
|
938 |
|
939 bool |
|
940 symbol_table::clear_global_pattern (const std::string& pat) |
|
941 { |
|
942 bool retval = false; |
|
943 |
|
944 for (unsigned int i = 0; i < table_size; i++) |
|
945 { |
|
946 symbol_record *ptr = table[i].next (); |
|
947 |
|
948 while (ptr) |
|
949 { |
|
950 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
951 { |
|
952 glob_match pattern (pat); |
|
953 |
|
954 if (pattern.match (ptr->name ())) |
|
955 { |
|
956 ptr->clear (); |
|
957 |
|
958 retval = true; |
|
959 } |
|
960 } |
|
961 |
|
962 ptr = ptr->next (); |
|
963 } |
|
964 } |
|
965 |
|
966 return retval; |
|
967 } |
|
968 |
|
969 // Really only clear functions that can be reloaded. |
|
970 |
|
971 bool |
|
972 symbol_table::clear_function_pattern (const std::string& pat) |
|
973 { |
|
974 bool retval = false; |
|
975 |
|
976 for (unsigned int i = 0; i < table_size; i++) |
|
977 { |
|
978 symbol_record *ptr = table[i].next (); |
|
979 |
|
980 while (ptr) |
|
981 { |
|
982 if (ptr->is_user_function () || ptr->is_dld_function ()) |
|
983 { |
|
984 glob_match pattern (pat); |
|
985 |
|
986 if (pattern.match (ptr->name ())) |
|
987 { |
|
988 ptr->clear (); |
|
989 |
|
990 retval = true; |
|
991 } |
|
992 } |
|
993 |
|
994 ptr = ptr->next (); |
|
995 } |
|
996 } |
|
997 |
|
998 return retval; |
|
999 } |
|
1000 |
1
|
1001 int |
164
|
1002 symbol_table::size (void) const |
1
|
1003 { |
|
1004 int count = 0; |
3013
|
1005 |
|
1006 for (unsigned int i = 0; i < table_size; i++) |
1
|
1007 { |
|
1008 symbol_record *ptr = table[i].next (); |
3013
|
1009 |
530
|
1010 while (ptr) |
1
|
1011 { |
|
1012 count++; |
|
1013 ptr = ptr->next (); |
|
1014 } |
|
1015 } |
3013
|
1016 |
1
|
1017 return count; |
|
1018 } |
|
1019 |
3013
|
1020 static bool |
3523
|
1021 matches_patterns (const std::string& name, const string_vector& pats) |
1
|
1022 { |
3013
|
1023 int npats = pats.length (); |
1
|
1024 |
3013
|
1025 if (npats == 0) |
|
1026 return true; |
|
1027 |
|
1028 glob_match pattern (pats); |
|
1029 |
|
1030 return pattern.match (name); |
1
|
1031 } |
|
1032 |
3013
|
1033 Array<symbol_record *> |
4913
|
1034 symbol_table::subsymbol_list (const string_vector& pats, |
|
1035 unsigned int type, unsigned int scope) const |
|
1036 { |
|
1037 int count = 0; |
|
1038 |
|
1039 int n = size (); |
|
1040 |
5659
|
1041 Array<symbol_record *> subsymbols (dim_vector (n, 1)); |
4913
|
1042 int pats_length = pats.length (); |
|
1043 |
|
1044 if (n == 0) |
|
1045 return subsymbols; |
|
1046 |
|
1047 // Look for separators like .({ |
|
1048 for (int j = 0; j < pats_length; j++) |
|
1049 { |
|
1050 std::string var_name = pats (j); |
|
1051 |
|
1052 size_t pos = var_name.find_first_of (".({"); |
|
1053 |
|
1054 if ((pos != NPOS) && (pos > 0)) |
|
1055 { |
|
1056 std::string first_name = var_name.substr(0,pos); |
|
1057 |
|
1058 for (unsigned int i = 0; i < table_size; i++) |
|
1059 { |
|
1060 symbol_record *ptr = table[i].next (); |
|
1061 |
|
1062 while (ptr) |
|
1063 { |
|
1064 assert (count < n); |
|
1065 |
|
1066 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
|
1067 |
|
1068 unsigned int my_type = ptr->type (); |
|
1069 |
|
1070 std::string my_name = ptr->name (); |
|
1071 |
|
1072 if ((type & my_type) && (scope & my_scope) && (first_name == my_name)) |
|
1073 { |
|
1074 symbol_record *sym_ptr = new symbol_record (); |
|
1075 octave_value value; |
|
1076 int parse_status; |
4914
|
1077 |
4913
|
1078 value = eval_string (var_name, true, parse_status); |
4914
|
1079 |
4913
|
1080 sym_ptr->define (value); |
|
1081 sym_ptr->rename (var_name); |
|
1082 subsymbols(count++) = sym_ptr; |
|
1083 } |
|
1084 |
|
1085 ptr = ptr->next (); |
|
1086 } |
|
1087 } |
|
1088 } |
|
1089 } |
|
1090 |
5659
|
1091 subsymbols.resize (dim_vector (count, 1)); |
4913
|
1092 |
|
1093 return subsymbols; |
|
1094 } |
|
1095 |
|
1096 Array<symbol_record *> |
3355
|
1097 symbol_table::symbol_list (const string_vector& pats, |
3013
|
1098 unsigned int type, unsigned int scope) const |
1
|
1099 { |
3355
|
1100 int count = 0; |
3013
|
1101 |
1
|
1102 int n = size (); |
3013
|
1103 |
5659
|
1104 Array<symbol_record *> symbols (dim_vector (n, 1)); |
3355
|
1105 |
1
|
1106 if (n == 0) |
3355
|
1107 return symbols; |
3013
|
1108 |
|
1109 for (unsigned int i = 0; i < table_size; i++) |
1
|
1110 { |
|
1111 symbol_record *ptr = table[i].next (); |
3013
|
1112 |
530
|
1113 while (ptr) |
1
|
1114 { |
5399
|
1115 if (ptr->is_visible ()) |
5397
|
1116 { |
|
1117 assert (count < n); |
867
|
1118 |
5397
|
1119 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
1120 |
5397
|
1121 unsigned int my_type = ptr->type (); |
|
1122 |
|
1123 std::string my_name = ptr->name (); |
867
|
1124 |
5397
|
1125 if ((type & my_type) && (scope & my_scope) && (matches_patterns (my_name, pats))) |
|
1126 symbols(count++) = ptr; |
|
1127 } |
5399
|
1128 |
1
|
1129 ptr = ptr->next (); |
|
1130 } |
|
1131 } |
1755
|
1132 |
5659
|
1133 symbols.resize (dim_vector (count, 1)); |
1
|
1134 |
3013
|
1135 return symbols; |
|
1136 } |
|
1137 |
|
1138 string_vector |
3355
|
1139 symbol_table::name_list (const string_vector& pats, bool sort, |
3013
|
1140 unsigned int type, unsigned int scope) const |
|
1141 { |
3355
|
1142 Array<symbol_record *> symbols = symbol_list (pats, type, scope); |
3013
|
1143 |
|
1144 string_vector names; |
|
1145 |
|
1146 int n = symbols.length (); |
|
1147 |
|
1148 if (n > 0) |
|
1149 { |
|
1150 names.resize (n); |
|
1151 |
|
1152 for (int i = 0; i < n; i++) |
|
1153 names[i] = symbols(i)->name (); |
|
1154 } |
|
1155 |
|
1156 if (sort) |
|
1157 names.qsort (); |
|
1158 |
|
1159 return names; |
|
1160 } |
|
1161 |
|
1162 static int |
3145
|
1163 maybe_list_cmp_fcn (const void *a_arg, const void *b_arg) |
3013
|
1164 { |
5760
|
1165 const symbol_record *a = static_cast<const symbol_record *> (a_arg); |
|
1166 const symbol_record *b = static_cast<const symbol_record *> (b_arg); |
3013
|
1167 |
3523
|
1168 std::string a_nm = a->name (); |
|
1169 std::string b_nm = b->name (); |
3145
|
1170 |
|
1171 return a_nm.compare (b_nm); |
3013
|
1172 } |
1
|
1173 |
4913
|
1174 void |
|
1175 symbol_table::print_descriptor (std::ostream& os, |
|
1176 std::list<whos_parameter> params) const |
|
1177 { |
|
1178 // This method prints a line of information on a given symbol |
|
1179 std::list<whos_parameter>::iterator i = params.begin (); |
5765
|
1180 std::ostringstream param_buf; |
4913
|
1181 |
|
1182 while (i != params.end ()) |
|
1183 { |
|
1184 whos_parameter param = * i; |
|
1185 |
|
1186 if (param.command != '\0') |
|
1187 { |
|
1188 // Do the actual printing |
|
1189 switch (param.modifier) |
|
1190 { |
|
1191 case 'l': |
|
1192 os << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1193 param_buf << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1194 break; |
|
1195 |
|
1196 case 'r': |
|
1197 os << std::setiosflags (std::ios::right) << std::setw (param.parameter_length); |
|
1198 param_buf << std::setiosflags (std::ios::right) << std::setw (param.parameter_length); |
|
1199 break; |
|
1200 |
|
1201 case 'c': |
4914
|
1202 if (param.command != 's') |
4913
|
1203 { |
|
1204 os << std::setiosflags (std::ios::left) |
|
1205 << std::setw (param.parameter_length); |
|
1206 param_buf << std::setiosflags (std::ios::left) |
|
1207 << std::setw (param.parameter_length); |
|
1208 } |
|
1209 break; |
|
1210 |
|
1211 default: |
|
1212 os << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1213 param_buf << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); |
|
1214 } |
|
1215 |
|
1216 if (param.command == 's' && param.modifier == 'c') |
|
1217 { |
|
1218 int a, b; |
4914
|
1219 |
4913
|
1220 if (param.modifier == 'c') |
|
1221 { |
|
1222 a = param.first_parameter_length - param.balance; |
|
1223 a = (a < 0 ? 0 : a); |
|
1224 b = param.parameter_length - a - param.text . length (); |
|
1225 b = (b < 0 ? 0 : b); |
|
1226 os << std::setiosflags (std::ios::left) << std::setw (a) |
|
1227 << "" << std::resetiosflags (std::ios::left) << param.text |
|
1228 << std::setiosflags (std::ios::left) |
|
1229 << std::setw (b) << "" |
|
1230 << std::resetiosflags (std::ios::left); |
|
1231 param_buf << std::setiosflags (std::ios::left) << std::setw (a) |
|
1232 << "" << std::resetiosflags (std::ios::left) << param.line |
|
1233 << std::setiosflags (std::ios::left) |
|
1234 << std::setw (b) << "" |
|
1235 << std::resetiosflags (std::ios::left); |
|
1236 } |
|
1237 } |
|
1238 else |
|
1239 { |
|
1240 os << param.text; |
|
1241 param_buf << param.line; |
|
1242 } |
|
1243 os << std::resetiosflags (std::ios::left) |
|
1244 << std::resetiosflags (std::ios::right); |
|
1245 param_buf << std::resetiosflags (std::ios::left) |
|
1246 << std::resetiosflags (std::ios::right); |
|
1247 i++; |
|
1248 } |
|
1249 else |
|
1250 { |
|
1251 os << param.text; |
|
1252 param_buf << param.line; |
|
1253 i++; |
|
1254 } |
|
1255 } |
|
1256 |
5765
|
1257 os << param_buf.str (); |
4913
|
1258 } |
|
1259 |
|
1260 std::list<whos_parameter> |
|
1261 symbol_table::parse_whos_line_format (Array<symbol_record *>& symbols) const |
|
1262 { |
|
1263 // This method parses the string whos_line_format, and returns |
|
1264 // a parameter list, containing all information needed to print |
|
1265 // the given attributtes of the symbols |
|
1266 int idx; |
|
1267 size_t format_len = Vwhos_line_format.length (); |
|
1268 char garbage; |
|
1269 std::list<whos_parameter> params; |
|
1270 |
|
1271 size_t bytes1; |
|
1272 int elements1; |
|
1273 |
|
1274 int len = symbols.length (), i; |
|
1275 |
|
1276 std::string param_string = "benpst"; |
5659
|
1277 Array<int> param_length (dim_vector (param_string.length (), 1)); |
|
1278 Array<std::string> param_names (dim_vector (param_string.length (), 1)); |
4913
|
1279 size_t pos_b, pos_t, pos_e, pos_n, pos_p, pos_s; |
|
1280 |
|
1281 pos_b = param_string.find ('b'); // Bytes |
|
1282 pos_t = param_string.find ('t'); // (Type aka) Class |
|
1283 pos_e = param_string.find ('e'); // Elements |
|
1284 pos_n = param_string.find ('n'); // Name |
|
1285 pos_p = param_string.find ('p'); // Protected |
|
1286 pos_s = param_string.find ('s'); // Size |
|
1287 |
|
1288 param_names(pos_b) = "Bytes"; |
|
1289 param_names(pos_t) = "Class"; |
|
1290 param_names(pos_e) = "Elements"; |
|
1291 param_names(pos_n) = "Name"; |
|
1292 param_names(pos_p) = "Prot"; |
|
1293 param_names(pos_s) = "Size"; |
|
1294 |
|
1295 for (i = 0; i < 6; i++) |
|
1296 param_length(i) = param_names(i) . length (); |
|
1297 |
|
1298 // Calculating necessary spacing for name column, |
|
1299 // bytes column, elements column and class column |
|
1300 for (i = 0; i < static_cast<int> (len); i++) |
|
1301 { |
|
1302 std::stringstream ss1, ss2; |
|
1303 std::string str; |
|
1304 |
|
1305 str = symbols(i)->name (); |
5049
|
1306 param_length(pos_n) = ((str.length () |
|
1307 > static_cast<size_t> (param_length(pos_n))) |
|
1308 ? str.length () : param_length(pos_n)); |
4913
|
1309 |
|
1310 str = symbols(i)->type_name (); |
5049
|
1311 param_length(pos_t) = ((str.length () |
|
1312 > static_cast<size_t> (param_length(pos_t))) |
|
1313 ? str.length () : param_length(pos_t)); |
4913
|
1314 |
5164
|
1315 elements1 = symbols(i)->capacity (); |
4913
|
1316 ss1 << elements1; |
|
1317 str = ss1.str (); |
5049
|
1318 param_length(pos_e) = ((str.length () |
|
1319 > static_cast<size_t> (param_length(pos_e))) |
|
1320 ? str.length () : param_length(pos_e)); |
4913
|
1321 |
|
1322 bytes1 = symbols(i)->byte_size (); |
|
1323 ss2 << bytes1; |
|
1324 str = ss2.str (); |
5049
|
1325 param_length(pos_b) = ((str.length () |
|
1326 > static_cast<size_t> (param_length(pos_b))) |
|
1327 ? str.length () : param_length (pos_b)); |
4913
|
1328 } |
|
1329 |
|
1330 idx = 0; |
|
1331 while (static_cast<size_t> (idx) < format_len) |
|
1332 { |
|
1333 whos_parameter param; |
|
1334 param.command = '\0'; |
|
1335 |
|
1336 if (Vwhos_line_format[idx] == '%') |
4914
|
1337 { |
|
1338 bool error_encountered = false; |
4913
|
1339 param.modifier = 'r'; |
|
1340 param.parameter_length = 0; |
|
1341 param.dimensions = 8; |
|
1342 |
|
1343 int a = 0, b = -1, c = 8, balance = 1; |
|
1344 unsigned int items; |
|
1345 size_t pos; |
|
1346 std::string cmd; |
|
1347 |
|
1348 // Parse one command from whos_line_format |
|
1349 cmd = Vwhos_line_format.substr (idx, Vwhos_line_format.length ()); |
|
1350 pos = cmd.find (';'); |
|
1351 if (pos != NPOS) |
|
1352 cmd = cmd.substr (0, pos+1); |
|
1353 else |
|
1354 error ("parameter without ; in whos_line_format"); |
|
1355 |
|
1356 idx += cmd.length (); |
4914
|
1357 |
5775
|
1358 // FIXME -- use iostream functions instead of sscanf! |
4914
|
1359 |
4913
|
1360 if (cmd.find_first_of ("crl") != 1) |
|
1361 items = sscanf (cmd.c_str (), "%c%c:%d:%d:%d:%d;", |
|
1362 &garbage, ¶m.command, &a, &b, &c, &balance); |
|
1363 else |
|
1364 items = sscanf (cmd.c_str (), "%c%c%c:%d:%d:%d:%d;", |
4914
|
1365 &garbage, ¶m.modifier, ¶m.command, |
|
1366 &a, &b, &c, &balance) - 1; |
|
1367 |
4913
|
1368 if (items < 2) |
|
1369 { |
|
1370 error ("whos_line_format: parameter structure without command in whos_line_format"); |
4914
|
1371 error_encountered = true; |
4913
|
1372 } |
|
1373 |
|
1374 // Insert data into parameter |
|
1375 param.first_parameter_length = 0; |
|
1376 pos = param_string.find (param.command); |
|
1377 if (pos != NPOS) |
|
1378 { |
|
1379 param.parameter_length = param_length(pos); |
|
1380 param.text = param_names(pos); |
5049
|
1381 param.line.assign (param_names(pos).length (), '='); |
5048
|
1382 |
5049
|
1383 param.parameter_length = (a > param.parameter_length |
|
1384 ? a : param.parameter_length); |
|
1385 if (param.command == 's' && param.modifier == 'c' && b > 0) |
5048
|
1386 param.first_parameter_length = b; |
4913
|
1387 } |
|
1388 else |
|
1389 { |
|
1390 error ("whos_line_format: '%c' is not a command", |
|
1391 param.command); |
4914
|
1392 error_encountered = true; |
4913
|
1393 } |
|
1394 |
|
1395 if (param.command == 's') |
|
1396 { |
|
1397 // Have to calculate space needed for printing matrix dimensions |
|
1398 // Space needed for Size column is hard to determine in prior, |
|
1399 // because it depends on dimensions to be shown. That is why it is |
|
1400 // recalculated for each Size-command |
5048
|
1401 int j, first, rest = 0, total; |
4913
|
1402 param.dimensions = c; |
5048
|
1403 first = param.first_parameter_length; |
|
1404 total = param.parameter_length; |
4914
|
1405 |
4913
|
1406 for (j = 0; j < len; j++) |
5050
|
1407 { |
|
1408 int first1 = symbols(j)->dimensions_string_req_first_space (param.dimensions); |
|
1409 int total1 = symbols(j)->dimensions_string_req_total_space (param.dimensions); |
|
1410 int rest1 = total1 - first1; |
|
1411 rest = (rest1 > rest ? rest1 : rest); |
|
1412 first = (first1 > first ? first1 : first); |
|
1413 total = (total1 > total ? total1 : total); |
|
1414 } |
4913
|
1415 |
|
1416 if (param.modifier == 'c') |
|
1417 { |
|
1418 if (first < balance) |
|
1419 first += balance - first; |
|
1420 if (rest + balance < param.parameter_length) |
|
1421 rest += param.parameter_length - rest - balance; |
|
1422 |
|
1423 param.parameter_length = first + rest; |
|
1424 param.first_parameter_length = first; |
|
1425 param.balance = balance; |
|
1426 } |
|
1427 else |
|
1428 { |
|
1429 param.parameter_length = total; |
|
1430 param.first_parameter_length = 0; |
|
1431 } |
|
1432 } |
|
1433 else if (param.modifier == 'c') |
|
1434 { |
|
1435 error ("whos_line_format: modifier 'c' not available for command '%c'", |
|
1436 param.command); |
4914
|
1437 error_encountered = true; |
4913
|
1438 } |
|
1439 |
|
1440 // What happens if whos_line_format contains negative numbers |
|
1441 // at param_length positions? |
5049
|
1442 param.balance = (b < 0 ? 0 : param.balance); |
|
1443 param.first_parameter_length = (b < 0 ? 0 : |
4913
|
1444 param.first_parameter_length); |
5049
|
1445 param.parameter_length = (a < 0 |
|
1446 ? 0 |
|
1447 : (param.parameter_length |
|
1448 < param_length(pos_s) |
|
1449 ? param_length(pos_s) |
|
1450 : param.parameter_length)); |
4913
|
1451 |
|
1452 // Parameter will not be pushed into parameter list if ... |
4914
|
1453 if (! error_encountered) |
4913
|
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 { |
4914
|
1482 // This method prints information for sets of symbols, but only one |
|
1483 // set at a time (like, for instance: all variables, or all |
|
1484 // built-in-functions). |
4913
|
1485 |
4914
|
1486 // This method invokes print_symbol_info_line to print info on every |
|
1487 // symbol. |
4913
|
1488 |
3013
|
1489 int status = 0; |
|
1490 |
|
1491 if (show_verbose) |
|
1492 { |
5775
|
1493 // FIXME Should separate argv to lists with and without dots. |
4914
|
1494 Array<symbol_record *> xsymbols = symbol_list (argv, type, scope); |
|
1495 Array<symbol_record *> xsubsymbols = subsymbol_list (argv, type, scope); |
3013
|
1496 |
4914
|
1497 int sym_len = xsymbols.length (), subsym_len = xsubsymbols.length (), |
4913
|
1498 len = sym_len + subsym_len; |
4914
|
1499 |
5659
|
1500 Array<symbol_record *> symbols (dim_vector (len, 1)); |
3013
|
1501 |
3355
|
1502 if (len > 0) |
3013
|
1503 { |
5005
|
1504 size_t bytes = 0; |
|
1505 size_t elements = 0; |
|
1506 |
|
1507 int i; |
|
1508 |
4913
|
1509 std::list<whos_parameter> params; |
|
1510 |
4914
|
1511 // Joining symbolic tables. |
4913
|
1512 for (i = 0; i < sym_len; i++) |
4914
|
1513 symbols(i) = xsymbols(i); |
4913
|
1514 |
|
1515 for (i = 0; i < subsym_len; i++) |
4914
|
1516 symbols(i+sym_len) = xsubsymbols(i); |
4913
|
1517 |
|
1518 os << "\n" << header << "\n\n"; |
3013
|
1519 |
|
1520 symbols.qsort (maybe_list_cmp_fcn); |
|
1521 |
4913
|
1522 params = parse_whos_line_format (symbols); |
|
1523 |
|
1524 print_descriptor (os, params); |
|
1525 |
|
1526 os << "\n"; |
|
1527 |
|
1528 for (int j = 0; j < len; j++) |
|
1529 { |
|
1530 symbols(j)->print_symbol_info_line (os, params); |
5164
|
1531 elements += symbols(j)->capacity (); |
4913
|
1532 bytes += symbols(j)->byte_size (); |
|
1533 } |
|
1534 |
5005
|
1535 os << "\nTotal is " |
5006
|
1536 << elements << (elements == 1 ? " element" : " elements") |
5005
|
1537 << " using " |
5006
|
1538 << bytes << (bytes == 1 ? " byte" : " bytes") |
5005
|
1539 << "\n"; |
3013
|
1540 |
|
1541 status = 1; |
|
1542 } |
|
1543 } |
|
1544 else |
|
1545 { |
3355
|
1546 string_vector symbols = name_list (argv, 1, type, scope); |
3013
|
1547 |
3355
|
1548 if (! symbols.empty ()) |
4914
|
1549 { |
3013
|
1550 os << "\n" << header << "\n\n"; |
|
1551 |
|
1552 symbols.list_in_columns (os); |
|
1553 |
|
1554 status = 1; |
|
1555 } |
|
1556 } |
|
1557 |
|
1558 return status; |
1
|
1559 } |
|
1560 |
3355
|
1561 Array<symbol_record *> |
3523
|
1562 symbol_table::glob (const std::string& pat, unsigned int type, |
2893
|
1563 unsigned int scope) const |
605
|
1564 { |
3355
|
1565 int count = 0; |
|
1566 |
605
|
1567 int n = size (); |
3355
|
1568 |
5659
|
1569 Array<symbol_record *> symbols (dim_vector (n, 1)); |
3355
|
1570 |
605
|
1571 if (n == 0) |
3355
|
1572 return symbols; |
3013
|
1573 |
|
1574 for (unsigned int i = 0; i < table_size; i++) |
605
|
1575 { |
|
1576 symbol_record *ptr = table[i].next (); |
3013
|
1577 |
605
|
1578 while (ptr) |
|
1579 { |
|
1580 assert (count < n); |
|
1581 |
2893
|
1582 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
1583 |
2893
|
1584 unsigned int my_type = ptr->type (); |
605
|
1585 |
1792
|
1586 glob_match pattern (pat); |
1755
|
1587 |
605
|
1588 if ((type & my_type) && (scope & my_scope) |
1792
|
1589 && pattern.match (ptr->name ())) |
605
|
1590 { |
3355
|
1591 symbols(count++) = ptr; |
605
|
1592 } |
|
1593 |
|
1594 ptr = ptr->next (); |
|
1595 } |
|
1596 } |
3355
|
1597 |
5659
|
1598 symbols.resize (dim_vector (count, 1)); |
605
|
1599 |
|
1600 return symbols; |
|
1601 } |
|
1602 |
220
|
1603 void |
|
1604 symbol_table::push_context (void) |
|
1605 { |
3013
|
1606 for (unsigned int i = 0; i < table_size; i++) |
220
|
1607 { |
|
1608 symbol_record *ptr = table[i].next (); |
|
1609 |
530
|
1610 while (ptr) |
220
|
1611 { |
|
1612 ptr->push_context (); |
|
1613 ptr = ptr->next (); |
|
1614 } |
|
1615 } |
|
1616 } |
|
1617 |
|
1618 void |
|
1619 symbol_table::pop_context (void) |
|
1620 { |
3013
|
1621 for (unsigned int i = 0; i < table_size; i++) |
220
|
1622 { |
|
1623 symbol_record *ptr = table[i].next (); |
|
1624 |
530
|
1625 while (ptr) |
220
|
1626 { |
|
1627 ptr->pop_context (); |
|
1628 ptr = ptr->next (); |
|
1629 } |
|
1630 } |
|
1631 } |
|
1632 |
3013
|
1633 void |
3944
|
1634 symbol_table::print_info (std::ostream& os) const |
3013
|
1635 { |
|
1636 int count = 0; |
|
1637 int empty_chains = 0; |
|
1638 int max_chain_length = 0; |
|
1639 int min_chain_length = INT_MAX; |
|
1640 |
|
1641 for (unsigned int i = 0; i < table_size; i++) |
|
1642 { |
|
1643 int num_this_chain = 0; |
|
1644 |
|
1645 symbol_record *ptr = table[i].next (); |
|
1646 |
|
1647 if (ptr) |
3933
|
1648 os << "chain number " << i << ":\n"; |
3013
|
1649 else |
|
1650 { |
|
1651 empty_chains++; |
|
1652 min_chain_length = 0; |
|
1653 } |
|
1654 |
|
1655 while (ptr) |
|
1656 { |
|
1657 num_this_chain++; |
|
1658 |
3933
|
1659 os << " " << ptr->name () << "\n"; |
|
1660 |
|
1661 ptr->print_info (os, " "); |
3013
|
1662 |
|
1663 ptr = ptr->next (); |
|
1664 } |
|
1665 |
|
1666 count += num_this_chain; |
|
1667 |
|
1668 if (num_this_chain > max_chain_length) |
|
1669 max_chain_length = num_this_chain; |
|
1670 |
|
1671 if (num_this_chain < min_chain_length) |
|
1672 min_chain_length = num_this_chain; |
|
1673 |
|
1674 if (num_this_chain > 0) |
3933
|
1675 os << "\n"; |
3013
|
1676 } |
|
1677 |
3933
|
1678 os << "max chain length: " << max_chain_length << "\n"; |
|
1679 os << "min chain length: " << min_chain_length << "\n"; |
|
1680 os << "empty chains: " << empty_chains << "\n"; |
|
1681 os << "total chains: " << table_size << "\n"; |
|
1682 os << "total symbols: " << count << "\n"; |
3013
|
1683 } |
|
1684 |
1
|
1685 // Chris Torek's fave hash function. |
|
1686 |
|
1687 unsigned int |
3523
|
1688 symbol_table::hash (const std::string& str) |
1
|
1689 { |
2893
|
1690 unsigned int h = 0; |
3013
|
1691 |
2893
|
1692 for (unsigned int i = 0; i < str.length (); i++) |
1755
|
1693 h = h * 33 + str[i]; |
1
|
1694 |
3013
|
1695 return h & (table_size - 1); |
2790
|
1696 } |
|
1697 |
5794
|
1698 DEFUN (debug_symtab_lookups, args, nargout, |
|
1699 "-*- texinfo -*-\n\ |
|
1700 @deftypefn {Built-in Function} {@var{val} =} debug_symtab_lookups ()\n\ |
|
1701 @deftypefnx {Built-in Function} {@var{old_val} =} debug_symtab_lookups (@var{new_val})\n\ |
|
1702 Query or set the internal variable that controls whether debugging\n\ |
|
1703 information is printed when searching for symbols in the symbol tables.\n\ |
|
1704 @end deftypefn") |
3308
|
1705 { |
5794
|
1706 return SET_INTERNAL_VARIABLE (debug_symtab_lookups); |
4238
|
1707 } |
|
1708 |
5794
|
1709 DEFUN (whos_line_format, args, nargout, |
|
1710 "-*- texinfo -*-\n\ |
|
1711 @deftypefn {Built-in Function} {@var{val} =} whos_line_format ()\n\ |
|
1712 @deftypefnx {Built-in Function} {@var{old_val} =} whos_line_format (@var{new_val})\n\ |
|
1713 Query or set the format string used by the @code{whos}.\n\ |
|
1714 \n\ |
|
1715 The following escape sequences may be used in the format:\n\ |
4913
|
1716 @table @code\n\ |
|
1717 @item %b\n\ |
|
1718 Prints number of bytes occupied by variables.\n\ |
|
1719 @item %e\n\ |
|
1720 Prints elements held by variables.\n\ |
|
1721 @item %n\n\ |
|
1722 Prints variable names.\n\ |
|
1723 @item %p\n\ |
|
1724 Prints protection attributtes of variables.\n\ |
|
1725 @item %s\n\ |
|
1726 Prints dimensions of variables.\n\ |
|
1727 @item %t\n\ |
|
1728 Prints type names of variables.\n\ |
|
1729 @end table\n\ |
|
1730 \n\ |
|
1731 Every command may also have a modifier:\n\ |
|
1732 @table @code\n\ |
|
1733 @item l\n\ |
|
1734 Left alignment.\n\ |
|
1735 @item r\n\ |
|
1736 Right alignment (this is the default).\n\ |
|
1737 @item c\n\ |
|
1738 Centered (may only be applied to command %s).\n\ |
|
1739 @end table\n\ |
|
1740 \n\ |
|
1741 A command is composed like this:\n\ |
|
1742 %[modifier]<command>[:size_of_parameter[:center-specific[:print_dims[:balance]]]];\n\ |
|
1743 \n\ |
|
1744 Command and modifier is already explained. Size_of_parameter\n\ |
|
1745 tells how many columns the parameter will need for printing.\n\ |
|
1746 print_dims tells how many dimensions to print. If number of\n\ |
|
1747 dimensions exceeds print_dims, dimensions will be printed like\n\ |
|
1748 x-D.\n\ |
|
1749 center-specific and print_dims may only be applied to command\n\ |
|
1750 %s. A negative value for print_dims will cause Octave to print all\n\ |
|
1751 dimensions whatsoever.\n\ |
|
1752 balance specifies the offset for printing of the dimensions string.\n\ |
|
1753 \n\ |
5794
|
1754 The default format is \" %p:4; %ln:6; %cs:16:6:8:1; %rb:12; %lt:-1;\\n\".\n\ |
|
1755 @end deftypefn") |
|
1756 { |
|
1757 return SET_INTERNAL_VARIABLE (whos_line_format); |
|
1758 } |
|
1759 |
|
1760 DEFUN (variables_can_hide_functions, args, nargout, |
|
1761 "-*- texinfo -*-\n\ |
|
1762 @deftypefn {Built-in Function} {@var{val} =} variables_can_hide_functions ()\n\ |
|
1763 @deftypefnx {Built-in Function} {@var{old_val} =} variables_can_hide_functions (@var{new_val})\n\ |
|
1764 Query or set the internal variable that controls whether assignments\n\ |
|
1765 to variables may hide previously defined functions of the same name.\n\ |
|
1766 If set to a nonzero value allows hiding, zero causes Octave to\n\ |
|
1767 generate an error, and a negative value cause Octave to print a\n\ |
|
1768 warning, but allow the operation.\n\ |
|
1769 @end deftypefn") |
|
1770 { |
|
1771 return SET_INTERNAL_VARIABLE (variables_can_hide_functions); |
3308
|
1772 } |
|
1773 |
1
|
1774 /* |
|
1775 ;;; Local Variables: *** |
|
1776 ;;; mode: C++ *** |
|
1777 ;;; End: *** |
|
1778 */ |