1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
1297
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
1962
|
31 #include <cctype> |
|
32 |
1792
|
33 #include "oct-glob.h" |
1755
|
34 #include "str-vec.h" |
|
35 |
1352
|
36 #include "error.h" |
1742
|
37 #include "pt-const.h" |
|
38 #include "pt-fcn.h" |
|
39 #include "pt-fvc.h" |
1755
|
40 #include "symtab.h" |
1
|
41 #include "utils.h" |
1352
|
42 #include "variables.h" |
1
|
43 |
767
|
44 // Variables and functions. |
|
45 |
1
|
46 symbol_def::symbol_def (void) |
|
47 { |
195
|
48 init_state (); |
1
|
49 } |
|
50 |
2390
|
51 symbol_def::symbol_def (tree_constant *t) |
1
|
52 { |
195
|
53 init_state (); |
1
|
54 definition = t; |
195
|
55 type = USER_VARIABLE; |
1
|
56 } |
|
57 |
530
|
58 symbol_def::symbol_def (tree_builtin *t, unsigned fcn_type) |
1
|
59 { |
195
|
60 init_state (); |
1
|
61 definition = t; |
530
|
62 type = BUILTIN_FUNCTION | fcn_type; |
1
|
63 } |
|
64 |
530
|
65 symbol_def::symbol_def (tree_function *t, unsigned fcn_type) |
1
|
66 { |
195
|
67 init_state (); |
|
68 definition = t; |
530
|
69 type = USER_FUNCTION | fcn_type; |
195
|
70 } |
|
71 |
|
72 void |
|
73 symbol_def::init_state (void) |
|
74 { |
|
75 type = UNKNOWN; |
|
76 eternal = 0; |
|
77 read_only = 0; |
|
78 |
530
|
79 definition = 0; |
|
80 next_elem = 0; |
195
|
81 count = 0; |
1
|
82 } |
|
83 |
|
84 symbol_def::~symbol_def (void) |
|
85 { |
|
86 delete definition; |
|
87 } |
|
88 |
2856
|
89 bool |
195
|
90 symbol_def::is_variable (void) const |
|
91 { |
530
|
92 return (type & USER_VARIABLE || type & BUILTIN_VARIABLE); |
195
|
93 } |
|
94 |
2856
|
95 bool |
195
|
96 symbol_def::is_function (void) const |
|
97 { |
530
|
98 return (type & USER_FUNCTION || type & BUILTIN_FUNCTION); |
195
|
99 } |
|
100 |
2856
|
101 bool |
195
|
102 symbol_def::is_user_variable (void) const |
|
103 { |
530
|
104 return (type & USER_VARIABLE); |
|
105 } |
|
106 |
2856
|
107 bool |
530
|
108 symbol_def::is_text_function (void) const |
|
109 { |
|
110 return (type & TEXT_FUNCTION); |
|
111 } |
|
112 |
2856
|
113 bool |
530
|
114 symbol_def::is_mapper_function (void) const |
|
115 { |
|
116 return (type & MAPPER_FUNCTION); |
195
|
117 } |
|
118 |
2856
|
119 bool |
195
|
120 symbol_def::is_user_function (void) const |
|
121 { |
530
|
122 return (type & USER_FUNCTION); |
195
|
123 } |
|
124 |
2856
|
125 bool |
195
|
126 symbol_def::is_builtin_variable (void) const |
|
127 { |
530
|
128 return (type & BUILTIN_VARIABLE); |
195
|
129 } |
|
130 |
2856
|
131 bool |
195
|
132 symbol_def::is_builtin_function (void) const |
|
133 { |
530
|
134 return (type & BUILTIN_FUNCTION); |
195
|
135 } |
|
136 |
2390
|
137 // XXX FIXME XXX |
2856
|
138 bool |
2390
|
139 symbol_def::is_map_element (const string& /* elts */) const |
|
140 { |
|
141 return 0; |
|
142 } |
|
143 |
1
|
144 void |
2390
|
145 symbol_def::define (tree_constant *t) |
1
|
146 { |
2408
|
147 if (t) |
|
148 t->maybe_mutate (); |
|
149 |
1
|
150 definition = t; |
2408
|
151 |
195
|
152 if (! is_builtin_variable ()) |
|
153 type = USER_VARIABLE; |
1
|
154 } |
|
155 |
|
156 void |
530
|
157 symbol_def::define (tree_builtin *t, unsigned fcn_type) |
1
|
158 { |
|
159 definition = t; |
530
|
160 type = BUILTIN_FUNCTION | fcn_type; |
1
|
161 } |
|
162 |
|
163 void |
530
|
164 symbol_def::define (tree_function *t, unsigned fcn_type) |
1
|
165 { |
|
166 definition = t; |
530
|
167 type = USER_FUNCTION | fcn_type; |
195
|
168 } |
|
169 |
|
170 void |
|
171 symbol_def::protect (void) |
|
172 { |
|
173 read_only = 1; |
|
174 } |
|
175 |
|
176 void |
|
177 symbol_def::unprotect (void) |
|
178 { |
|
179 read_only = 0; |
|
180 |
|
181 } |
|
182 |
|
183 void |
|
184 symbol_def::make_eternal (void) |
|
185 { |
|
186 eternal = 1; |
1
|
187 } |
|
188 |
489
|
189 tree_fvc * |
164
|
190 symbol_def::def (void) const |
1
|
191 { |
|
192 return definition; |
|
193 } |
|
194 |
1755
|
195 string |
164
|
196 symbol_def::help (void) const |
1
|
197 { |
|
198 return help_string; |
|
199 } |
|
200 |
|
201 void |
1755
|
202 symbol_def::document (const string& h) |
1
|
203 { |
1755
|
204 help_string = h; |
1
|
205 } |
|
206 |
|
207 int |
195
|
208 maybe_delete (symbol_def *def) |
|
209 { |
|
210 int count = 0; |
530
|
211 if (def && def->count > 0) |
195
|
212 { |
530
|
213 def->count--; |
|
214 count = def->count; |
|
215 if (def->count == 0) |
|
216 delete def; |
195
|
217 } |
|
218 return count; |
|
219 } |
|
220 |
767
|
221 // Individual records in a symbol table. |
|
222 |
1
|
223 symbol_record::symbol_record (void) |
|
224 { |
195
|
225 init_state (); |
1
|
226 } |
|
227 |
1755
|
228 symbol_record::symbol_record (const string& n, symbol_record *nxt) |
1
|
229 { |
195
|
230 init_state (); |
1755
|
231 nm = n; |
195
|
232 next_elem = nxt; |
1
|
233 } |
|
234 |
195
|
235 void |
|
236 symbol_record::init_state (void) |
1
|
237 { |
|
238 formal_param = 0; |
195
|
239 linked_to_global = 0; |
2846
|
240 tagged_static = 0; |
530
|
241 sv_fcn = 0; |
|
242 definition = 0; |
|
243 next_elem = 0; |
1
|
244 } |
|
245 |
1755
|
246 string |
164
|
247 symbol_record::name (void) const |
1
|
248 { |
|
249 return nm; |
|
250 } |
|
251 |
1755
|
252 string |
164
|
253 symbol_record::help (void) const |
1
|
254 { |
1755
|
255 string retval; |
|
256 if (definition) |
|
257 retval = definition->help (); |
|
258 return retval; |
1
|
259 } |
|
260 |
489
|
261 tree_fvc * |
164
|
262 symbol_record::def (void) const |
1
|
263 { |
2856
|
264 return definition ? definition->def () : false; |
1
|
265 } |
|
266 |
572
|
267 void |
1755
|
268 symbol_record::rename (const string& new_name) |
572
|
269 { |
2791
|
270 if (! read_only_error ("rename")) |
|
271 nm = new_name; |
572
|
272 } |
|
273 |
2856
|
274 bool |
164
|
275 symbol_record::is_function (void) const |
1
|
276 { |
2856
|
277 return definition ? definition->is_function () : false; |
530
|
278 } |
|
279 |
2856
|
280 bool |
530
|
281 symbol_record::is_text_function (void) const |
|
282 { |
2856
|
283 return definition ? definition->is_text_function () : false; |
530
|
284 } |
|
285 |
2856
|
286 bool |
530
|
287 symbol_record::is_mapper_function (void) const |
|
288 { |
2856
|
289 return definition ? definition->is_mapper_function () : false; |
195
|
290 } |
|
291 |
2856
|
292 bool |
195
|
293 symbol_record::is_user_function (void) const |
|
294 { |
2856
|
295 return definition ? definition->is_user_function () : false; |
195
|
296 } |
|
297 |
2856
|
298 bool |
195
|
299 symbol_record::is_builtin_function (void) const |
|
300 { |
2856
|
301 return definition ? definition->is_builtin_function () : false; |
1
|
302 } |
|
303 |
2856
|
304 bool |
164
|
305 symbol_record::is_variable (void) const |
1
|
306 { |
2856
|
307 return definition ? definition->is_variable () : false; |
195
|
308 } |
|
309 |
2856
|
310 bool |
195
|
311 symbol_record::is_user_variable (void) const |
|
312 { |
2856
|
313 return definition ? definition->is_user_variable () : false; |
195
|
314 } |
|
315 |
2856
|
316 bool |
195
|
317 symbol_record::is_builtin_variable (void) const |
|
318 { |
2856
|
319 return definition ? definition->is_builtin_variable () : false; |
195
|
320 } |
|
321 |
2856
|
322 bool |
2390
|
323 symbol_record::is_map_element (const string& elts) const |
|
324 { |
2856
|
325 return definition ? definition->is_map_element (elts) : false; |
2390
|
326 } |
|
327 |
195
|
328 unsigned |
|
329 symbol_record::type (void) const |
|
330 { |
2856
|
331 return definition ? definition->type : false; |
1
|
332 } |
|
333 |
2856
|
334 bool |
164
|
335 symbol_record::is_defined (void) const |
1
|
336 { |
2856
|
337 return definition ? (definition->def () != 0) : false; |
195
|
338 } |
|
339 |
2856
|
340 bool |
195
|
341 symbol_record::is_read_only (void) const |
|
342 { |
2856
|
343 return definition ? definition->read_only : false; |
195
|
344 } |
|
345 |
2856
|
346 bool |
195
|
347 symbol_record::is_eternal (void) const |
|
348 { |
2856
|
349 return definition ? definition->eternal : false; |
195
|
350 } |
|
351 |
|
352 void |
|
353 symbol_record::protect (void) |
|
354 { |
530
|
355 if (definition) |
195
|
356 { |
|
357 definition->protect (); |
|
358 |
|
359 if (! is_defined ()) |
1755
|
360 warning ("protecting undefined variable `%s'", nm.c_str ()); |
195
|
361 } |
|
362 } |
|
363 |
|
364 void |
|
365 symbol_record::unprotect (void) |
|
366 { |
530
|
367 if (definition) |
195
|
368 definition->unprotect (); |
|
369 } |
|
370 |
|
371 void |
|
372 symbol_record::make_eternal (void) |
|
373 { |
530
|
374 if (definition) |
195
|
375 { |
|
376 definition->make_eternal (); |
|
377 |
|
378 if (! is_defined ()) |
1755
|
379 warning ("giving eternal life to undefined variable `%s'", |
|
380 nm.c_str ()); |
195
|
381 } |
1
|
382 } |
|
383 |
|
384 void |
|
385 symbol_record::set_sv_function (sv_Function f) |
|
386 { |
|
387 sv_fcn = f; |
|
388 } |
|
389 |
|
390 int |
2390
|
391 symbol_record::define (tree_constant *t) |
1
|
392 { |
2791
|
393 if (is_variable () && read_only_error ("redefine")) |
1
|
394 return 0; |
|
395 |
530
|
396 tree_fvc *saved_def = 0; |
|
397 if (! definition) |
195
|
398 { |
|
399 definition = new symbol_def (); |
|
400 definition->count = 1; |
|
401 } |
|
402 else if (is_function ()) |
1
|
403 { |
195
|
404 symbol_def *new_def = new symbol_def (); |
|
405 push_def (new_def); |
|
406 definition->count = 1; |
1
|
407 } |
195
|
408 else if (is_variable ()) |
1
|
409 { |
195
|
410 saved_def = definition->def (); |
1
|
411 } |
|
412 |
195
|
413 definition->define (t); |
|
414 |
530
|
415 if (sv_fcn && sv_fcn () < 0) |
1
|
416 { |
1358
|
417 // Would be nice to be able to avoid this cast. XXX FIXME XXX |
|
418 |
2800
|
419 definition->define (static_cast<tree_constant *> (saved_def)); |
1
|
420 return 0; |
|
421 } |
|
422 |
|
423 delete saved_def; |
|
424 |
|
425 return 1; |
|
426 } |
|
427 |
|
428 int |
2390
|
429 symbol_record::define (const octave_value& v) |
|
430 { |
|
431 tree_constant *t = new tree_constant (v); |
|
432 return define (t); |
|
433 } |
|
434 |
|
435 int |
2856
|
436 symbol_record::define (tree_builtin *t, bool text_fcn) |
1
|
437 { |
2791
|
438 if (read_only_error ("redefine")) |
1
|
439 return 0; |
|
440 |
195
|
441 if (is_variable ()) |
1
|
442 { |
195
|
443 symbol_def *old_def = pop_def (); |
|
444 maybe_delete (old_def); |
1
|
445 } |
|
446 |
195
|
447 if (is_function ()) |
1
|
448 { |
195
|
449 symbol_def *old_def = pop_def (); |
|
450 maybe_delete (old_def); |
1
|
451 } |
|
452 |
530
|
453 unsigned fcn_type = text_fcn ? symbol_def::TEXT_FUNCTION |
|
454 : ((t && t->is_mapper_function ()) ? symbol_def::MAPPER_FUNCTION |
|
455 : symbol_def::UNKNOWN); |
|
456 |
|
457 symbol_def *new_def = new symbol_def (t, fcn_type); |
195
|
458 push_def (new_def); |
|
459 definition->count = 1; |
|
460 |
1
|
461 return 1; |
|
462 } |
|
463 |
|
464 int |
2856
|
465 symbol_record::define (tree_function *t, bool text_fcn) |
1
|
466 { |
2791
|
467 if (read_only_error ("redefine")) |
1
|
468 return 0; |
|
469 |
195
|
470 if (is_variable ()) |
1
|
471 { |
195
|
472 symbol_def *old_def = pop_def (); |
|
473 maybe_delete (old_def); |
1
|
474 } |
|
475 |
195
|
476 if (is_function ()) |
1
|
477 { |
195
|
478 symbol_def *old_def = pop_def (); |
|
479 maybe_delete (old_def); |
1
|
480 } |
|
481 |
530
|
482 unsigned fcn_type = text_fcn ? symbol_def::TEXT_FUNCTION |
|
483 : symbol_def::UNKNOWN; |
|
484 |
|
485 symbol_def *new_def = new symbol_def (t, fcn_type); |
195
|
486 push_def (new_def); |
|
487 definition->count = 1; |
|
488 |
1
|
489 return 1; |
|
490 } |
|
491 |
|
492 int |
2390
|
493 symbol_record::define_as_fcn (const octave_value& v) |
1
|
494 { |
2791
|
495 if (is_variable () && read_only_error ("redefine")) |
1
|
496 return 0; |
|
497 |
195
|
498 if (is_variable ()) |
1
|
499 { |
195
|
500 symbol_def *old_def = pop_def (); |
|
501 maybe_delete (old_def); |
|
502 } |
|
503 |
|
504 if (is_function ()) |
|
505 { |
|
506 symbol_def *old_def = pop_def (); |
|
507 maybe_delete (old_def); |
1
|
508 } |
|
509 |
2390
|
510 tree_constant *t = new tree_constant (v); |
195
|
511 symbol_def *new_def = new symbol_def (t); |
|
512 push_def (new_def); |
|
513 definition->count = 1; |
|
514 definition->type = symbol_def::BUILTIN_FUNCTION; |
1
|
515 |
|
516 return 1; |
|
517 } |
|
518 |
195
|
519 int |
2390
|
520 symbol_record::define_builtin_var (const octave_value& v) |
195
|
521 { |
2390
|
522 tree_constant *t = new tree_constant (v); |
195
|
523 define (t); |
|
524 if (is_variable ()) |
|
525 definition->type = symbol_def::BUILTIN_VARIABLE; |
530
|
526 return 1; |
195
|
527 } |
|
528 |
1
|
529 void |
1755
|
530 symbol_record::document (const string& h) |
1
|
531 { |
530
|
532 if (definition) |
195
|
533 { |
|
534 definition->document (h); |
1
|
535 |
195
|
536 if (! is_defined ()) |
1755
|
537 warning ("documenting undefined variable `%s'", nm.c_str ()); |
195
|
538 } |
1
|
539 } |
|
540 |
|
541 int |
195
|
542 symbol_record::clear (void) |
1
|
543 { |
195
|
544 int count = 0; |
|
545 if (linked_to_global) |
1
|
546 { |
195
|
547 count = maybe_delete (definition); |
530
|
548 definition = 0; |
195
|
549 linked_to_global = 0; |
1
|
550 } |
2846
|
551 else if (! tagged_static) |
195
|
552 { |
|
553 symbol_def *old_def = pop_def (); |
|
554 count = maybe_delete (old_def); |
|
555 } |
|
556 return count; |
1
|
557 } |
|
558 |
|
559 void |
2856
|
560 symbol_record::alias (symbol_record *s, bool force) |
1
|
561 { |
195
|
562 sv_fcn = s->sv_fcn; |
|
563 |
530
|
564 if (force && ! s->definition) |
1
|
565 { |
195
|
566 s->definition = new symbol_def (); |
|
567 definition = s->definition; |
|
568 definition->count = 2; // Yes, this is correct. |
1
|
569 } |
530
|
570 else if (s->definition) |
1
|
571 { |
195
|
572 definition = s->definition; |
|
573 definition->count++; |
1
|
574 } |
|
575 } |
|
576 |
|
577 void |
|
578 symbol_record::mark_as_formal_parameter (void) |
|
579 { |
|
580 formal_param = 1; |
|
581 } |
|
582 |
2856
|
583 bool |
164
|
584 symbol_record::is_formal_parameter (void) const |
1
|
585 { |
|
586 return formal_param; |
|
587 } |
|
588 |
|
589 void |
195
|
590 symbol_record::mark_as_linked_to_global (void) |
122
|
591 { |
195
|
592 linked_to_global = 1; |
122
|
593 } |
|
594 |
2856
|
595 bool |
195
|
596 symbol_record::is_linked_to_global (void) const |
1
|
597 { |
195
|
598 return linked_to_global; |
1
|
599 } |
|
600 |
2846
|
601 void |
|
602 symbol_record::mark_as_static (void) |
|
603 { |
|
604 if (is_linked_to_global ()) |
|
605 error ("can't make global variable static"); |
|
606 else if (is_formal_parameter ()) |
|
607 error ("can't make formal parameter static"); |
|
608 else |
|
609 tagged_static = 1; |
|
610 } |
|
611 |
2856
|
612 bool |
2846
|
613 symbol_record::is_static (void) const |
|
614 { |
|
615 return tagged_static; |
|
616 } |
|
617 |
2390
|
618 octave_value |
|
619 symbol_record::variable_value (void) const |
|
620 { |
|
621 octave_value retval; |
|
622 |
|
623 if (is_variable ()) |
|
624 { |
2800
|
625 tree_constant *tmp = static_cast<tree_constant *> (def ()); |
2390
|
626 retval = tmp->value (); |
|
627 } |
|
628 |
|
629 return retval; |
|
630 } |
|
631 |
|
632 octave_value& |
|
633 symbol_record::variable_reference (void) |
|
634 { |
|
635 if (is_function ()) |
|
636 clear (); |
|
637 |
|
638 if (! is_defined ()) |
|
639 { |
|
640 if (! (is_formal_parameter () || is_linked_to_global ())) |
|
641 link_to_builtin_variable (this); |
|
642 |
|
643 if (! is_defined ()) |
|
644 { |
|
645 tree_constant *tmp = new tree_constant (); |
|
646 define (tmp); |
|
647 } |
|
648 } |
|
649 |
2800
|
650 tree_constant *tmp = static_cast<tree_constant *> (def ()); |
2390
|
651 |
|
652 return tmp->reference (); |
|
653 } |
|
654 |
1
|
655 symbol_record * |
164
|
656 symbol_record::next (void) const |
1
|
657 { |
|
658 return next_elem; |
|
659 } |
|
660 |
195
|
661 void |
|
662 symbol_record::chain (symbol_record *s) |
|
663 { |
|
664 next_elem = s; |
|
665 } |
|
666 |
220
|
667 void |
|
668 symbol_record::push_context (void) |
|
669 { |
2846
|
670 if (! is_static ()) |
|
671 { |
|
672 context.push (definition); |
|
673 definition = 0; |
395
|
674 |
2846
|
675 global_link_context.push (static_cast<unsigned> (linked_to_global)); |
|
676 linked_to_global = 0; |
|
677 } |
220
|
678 } |
|
679 |
|
680 void |
|
681 symbol_record::pop_context (void) |
|
682 { |
1653
|
683 // It is possible for context to be empty if new symbols have been |
|
684 // inserted in the symbol table during recursive calls. This can |
|
685 // happen as a result of calls to eval() and feval(). |
220
|
686 |
1653
|
687 if (! context.empty ()) |
220
|
688 { |
1653
|
689 if (is_variable ()) |
|
690 { |
|
691 symbol_def *old_def = pop_def (); |
|
692 maybe_delete (old_def); |
|
693 } |
|
694 |
|
695 if (is_function ()) |
|
696 { |
|
697 symbol_def *old_def = pop_def (); |
|
698 maybe_delete (old_def); |
|
699 } |
|
700 |
|
701 definition = context.pop (); |
|
702 linked_to_global = global_link_context.pop (); |
220
|
703 } |
|
704 } |
|
705 |
195
|
706 int |
2791
|
707 symbol_record::read_only_error (const char *action) |
195
|
708 { |
|
709 if (is_read_only ()) |
|
710 { |
|
711 if (is_variable ()) |
1045
|
712 { |
2791
|
713 ::error ("can't %s read-only constant `%s'", action, nm.c_str ()); |
1045
|
714 } |
195
|
715 else if (is_function ()) |
1045
|
716 { |
2791
|
717 ::error ("can't %s read-only function `%s'", action, nm.c_str ()); |
1045
|
718 } |
|
719 else |
|
720 { |
2791
|
721 ::error ("can't %s read-only symbol `%s'", action, nm.c_str ()); |
1045
|
722 } |
195
|
723 |
|
724 return 1; |
|
725 } |
|
726 else |
|
727 return 0; |
|
728 } |
|
729 |
|
730 void |
|
731 symbol_record::push_def (symbol_def *sd) |
|
732 { |
530
|
733 if (! sd) |
195
|
734 return; |
|
735 |
|
736 sd->next_elem = definition; |
|
737 definition = sd; |
|
738 } |
|
739 |
|
740 symbol_def * |
|
741 symbol_record::pop_def (void) |
|
742 { |
|
743 symbol_def *top = definition; |
530
|
744 if (definition) |
195
|
745 definition = definition->next_elem; |
|
746 return top; |
|
747 } |
|
748 |
767
|
749 // A structure for handling verbose information about a symbol_record. |
195
|
750 |
|
751 symbol_record_info::symbol_record_info (void) |
2404
|
752 : initialized (0), nr (-1), nc (-1), type (symbol_def::UNKNOWN), |
|
753 hides (SR_INFO_NONE), eternal (0), read_only (0), nm (), |
|
754 const_type () { } |
195
|
755 |
|
756 symbol_record_info::symbol_record_info (const symbol_record& sr) |
2404
|
757 : initialized (0), nr (-1), nc (-1), type (sr.type ()), |
|
758 hides (SR_INFO_NONE), eternal (0), read_only (0), nm (), |
|
759 const_type () |
195
|
760 { |
|
761 if (sr.is_variable () && sr.is_defined ()) |
|
762 { |
1358
|
763 // Would be nice to avoid this cast. XXX FIXME XXX |
|
764 |
2800
|
765 tree_constant *tmp = static_cast<tree_constant *> (sr.def ()); |
2404
|
766 |
2878
|
767 octave_value vtmp = tmp->value (); |
195
|
768 |
2878
|
769 const_type = vtmp.type_name (); |
|
770 |
|
771 nr = vtmp.rows (); |
|
772 nc = vtmp.columns (); |
195
|
773 |
|
774 symbol_def *sr_def = sr.definition; |
|
775 symbol_def *hidden_def = sr_def->next_elem; |
2404
|
776 |
530
|
777 if (hidden_def) |
195
|
778 { |
|
779 if (hidden_def->is_user_function ()) |
|
780 hides = SR_INFO_USER_FUNCTION; |
|
781 else if (hidden_def->is_builtin_function ()) |
|
782 hides = SR_INFO_BUILTIN_FUNCTION; |
|
783 } |
|
784 } |
|
785 |
|
786 eternal = sr.is_eternal (); |
|
787 read_only = sr.is_read_only (); |
|
788 |
1755
|
789 nm = sr.name (); |
195
|
790 |
|
791 initialized = 1; |
|
792 } |
|
793 |
|
794 symbol_record_info::symbol_record_info (const symbol_record_info& s) |
2404
|
795 : initialized (s.initialized), nr (s.nr), nc (s.nc), type (s.type), |
|
796 hides (s.hides), eternal (s.eternal), read_only (s.read_only), |
|
797 nm (s.nm), const_type (s.const_type) { } |
195
|
798 |
|
799 symbol_record_info& |
|
800 symbol_record_info::operator = (const symbol_record_info& s) |
|
801 { |
|
802 if (this != &s) |
|
803 { |
2404
|
804 initialized = s.initialized; |
|
805 nr = s.nr; |
|
806 nc = s.nc; |
195
|
807 type = s.type; |
|
808 hides = s.hides; |
|
809 eternal = s.eternal; |
|
810 read_only = s.read_only; |
1755
|
811 nm = s.nm; |
2404
|
812 const_type = s.const_type; |
195
|
813 } |
|
814 return *this; |
|
815 } |
|
816 |
2856
|
817 bool |
195
|
818 symbol_record_info::is_defined (void) const |
|
819 { |
|
820 return initialized; |
|
821 } |
|
822 |
2856
|
823 bool |
195
|
824 symbol_record_info::is_read_only (void) const |
|
825 { |
|
826 return read_only; |
|
827 } |
|
828 |
2856
|
829 bool |
195
|
830 symbol_record_info::is_eternal (void) const |
|
831 { |
|
832 return eternal; |
|
833 } |
|
834 |
2856
|
835 bool |
195
|
836 symbol_record_info::hides_fcn (void) const |
|
837 { |
|
838 return (hides & SR_INFO_USER_FUNCTION); |
|
839 } |
|
840 |
2856
|
841 bool |
195
|
842 symbol_record_info::hides_builtin (void) const |
|
843 { |
|
844 return (hides & SR_INFO_BUILTIN_FUNCTION); |
|
845 } |
|
846 |
1755
|
847 string |
2390
|
848 symbol_record_info::type_name (void) const |
195
|
849 { |
2404
|
850 string retval; |
|
851 |
195
|
852 if (type == symbol_def::USER_FUNCTION) |
2404
|
853 retval = "user function"; |
|
854 else if (type & symbol_def::BUILTIN_FUNCTION) |
195
|
855 { |
2404
|
856 if (type & symbol_def::TEXT_FUNCTION) |
|
857 retval = "text function"; |
|
858 else if (type & symbol_def::MAPPER_FUNCTION) |
|
859 retval = "mapper function"; |
195
|
860 else |
2404
|
861 retval = "builtin function"; |
195
|
862 } |
2404
|
863 else |
|
864 retval = const_type; |
|
865 |
|
866 return retval; |
195
|
867 } |
|
868 |
2856
|
869 bool |
195
|
870 symbol_record_info::is_function (void) const |
|
871 { |
|
872 return (type == symbol_def::USER_FUNCTION |
2404
|
873 || type == symbol_def::BUILTIN_FUNCTION |
|
874 || symbol_def::TEXT_FUNCTION |
|
875 || symbol_def::MAPPER_FUNCTION); |
195
|
876 } |
|
877 |
|
878 int |
|
879 symbol_record_info::rows (void) const |
|
880 { |
|
881 return nr; |
|
882 } |
|
883 |
|
884 int |
|
885 symbol_record_info::columns (void) const |
|
886 { |
|
887 return nc; |
|
888 } |
|
889 |
1755
|
890 string |
195
|
891 symbol_record_info::name (void) const |
|
892 { |
|
893 return nm; |
|
894 } |
|
895 |
767
|
896 // A symbol table. |
1
|
897 |
|
898 symbol_table::symbol_table (void) |
|
899 { |
|
900 } |
|
901 |
|
902 symbol_record * |
2856
|
903 symbol_table::lookup (const string& nm, bool insert, bool warn) |
1
|
904 { |
|
905 int index = hash (nm) & HASH_MASK; |
|
906 |
|
907 symbol_record *ptr = table[index].next (); |
|
908 |
530
|
909 while (ptr) |
1
|
910 { |
1755
|
911 if (ptr->name () == nm) |
1
|
912 return ptr; |
|
913 ptr = ptr->next (); |
|
914 } |
|
915 |
|
916 if (insert) |
|
917 { |
|
918 symbol_record *new_sym; |
|
919 new_sym = new symbol_record (nm, table[index].next ()); |
195
|
920 table[index].chain (new_sym); |
1
|
921 return new_sym; |
|
922 } |
|
923 else if (warn) |
1755
|
924 warning ("lookup: symbol`%s' not found", nm.c_str ()); |
1
|
925 |
530
|
926 return 0; |
1
|
927 } |
|
928 |
|
929 void |
1755
|
930 symbol_table::rename (const string& old_name, const string& new_name) |
572
|
931 { |
|
932 int index = hash (old_name) & HASH_MASK; |
|
933 |
|
934 symbol_record *prev = &table[index]; |
|
935 symbol_record *ptr = prev->next (); |
|
936 |
|
937 while (ptr) |
|
938 { |
1755
|
939 if (ptr->name () == old_name) |
572
|
940 { |
|
941 ptr->rename (new_name); |
|
942 |
2791
|
943 if (! error_state) |
|
944 { |
|
945 prev->chain (ptr->next ()); |
|
946 |
|
947 index = hash (new_name) & HASH_MASK; |
|
948 table[index].chain (ptr); |
|
949 |
|
950 return; |
|
951 } |
|
952 |
|
953 break; |
572
|
954 } |
|
955 |
|
956 prev = ptr; |
|
957 ptr = ptr->next (); |
|
958 } |
|
959 |
1755
|
960 error ("unable to rename `%s' to `%s'", old_name.c_str (), |
|
961 new_name.c_str ()); |
572
|
962 } |
|
963 |
|
964 void |
2856
|
965 symbol_table::clear (bool clear_user_functions) |
1
|
966 { |
|
967 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
968 { |
169
|
969 symbol_record *ptr = table[i].next (); |
1
|
970 |
530
|
971 while (ptr) |
169
|
972 { |
195
|
973 if (ptr->is_user_variable () |
|
974 || (clear_user_functions && ptr->is_user_function ())) |
|
975 { |
|
976 ptr->clear (); |
|
977 } |
|
978 |
169
|
979 ptr = ptr->next (); |
1
|
980 } |
|
981 } |
|
982 } |
|
983 |
|
984 int |
2856
|
985 symbol_table::clear (const string& nm, bool clear_user_functions) |
1
|
986 { |
|
987 int index = hash (nm) & HASH_MASK; |
|
988 |
195
|
989 symbol_record *ptr = table[index].next (); |
1
|
990 |
530
|
991 while (ptr) |
1
|
992 { |
1755
|
993 if (ptr->name () == nm |
195
|
994 && (ptr->is_user_variable () |
|
995 || (clear_user_functions && ptr->is_user_function ()))) |
1
|
996 { |
195
|
997 ptr->clear (); |
|
998 return 1; |
1
|
999 } |
195
|
1000 ptr = ptr->next (); |
1
|
1001 } |
|
1002 |
|
1003 return 0; |
|
1004 } |
|
1005 |
|
1006 int |
164
|
1007 symbol_table::size (void) const |
1
|
1008 { |
|
1009 int count = 0; |
|
1010 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1011 { |
|
1012 symbol_record *ptr = table[i].next (); |
530
|
1013 while (ptr) |
1
|
1014 { |
|
1015 count++; |
|
1016 ptr = ptr->next (); |
|
1017 } |
|
1018 } |
|
1019 return count; |
|
1020 } |
|
1021 |
195
|
1022 static inline int |
|
1023 pstrcmp (char **a, char **b) |
1
|
1024 { |
195
|
1025 return strcmp (*a, *b); |
1
|
1026 } |
|
1027 |
195
|
1028 static inline int |
|
1029 symbol_record_info_cmp (symbol_record_info *a, symbol_record_info *b) |
1
|
1030 { |
1755
|
1031 return (a->name () == b->name ()); |
1
|
1032 } |
|
1033 |
867
|
1034 static int |
1755
|
1035 matches_patterns (const string& name, const string_vector& pats, int npats) |
867
|
1036 { |
1755
|
1037 for (int i = 0; i < npats; i++) |
867
|
1038 { |
1792
|
1039 glob_match pattern (pats[i]); |
|
1040 if (pattern.match (name)) |
867
|
1041 return 1; |
|
1042 } |
|
1043 |
|
1044 return 0; |
|
1045 } |
|
1046 |
195
|
1047 // This function should probably share code with symbol_table::list. |
|
1048 // XXX FIXME XXX |
1
|
1049 |
195
|
1050 symbol_record_info * |
1755
|
1051 symbol_table::long_list (int& count, const string_vector& pats, |
2856
|
1052 int npats, bool sort, unsigned type, |
1755
|
1053 unsigned scope) const |
1
|
1054 { |
115
|
1055 count = 0; |
1
|
1056 int n = size (); |
|
1057 if (n == 0) |
530
|
1058 return 0; |
1
|
1059 |
195
|
1060 symbol_record_info *symbols = new symbol_record_info [n+1]; |
1
|
1061 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1062 { |
|
1063 symbol_record *ptr = table[i].next (); |
530
|
1064 while (ptr) |
1
|
1065 { |
|
1066 assert (count < n); |
867
|
1067 |
195
|
1068 unsigned my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
1069 |
195
|
1070 unsigned my_type = ptr->type (); |
867
|
1071 |
1755
|
1072 string my_name = ptr->name (); |
867
|
1073 |
|
1074 if ((type & my_type) && (scope & my_scope) |
|
1075 && (npats == 0 || matches_patterns (my_name, pats, npats))) |
|
1076 symbols[count++] = symbol_record_info (*ptr); |
|
1077 |
1
|
1078 ptr = ptr->next (); |
|
1079 } |
|
1080 } |
195
|
1081 symbols[count] = symbol_record_info (); |
|
1082 |
530
|
1083 if (sort && symbols) |
2800
|
1084 qsort (symbols, count, sizeof (symbol_record_info), |
|
1085 symbol_record_info_cmp); |
195
|
1086 |
1
|
1087 return symbols; |
|
1088 } |
|
1089 |
1755
|
1090 string_vector |
|
1091 symbol_table::list (int& count, const string_vector& pats, int npats, |
2856
|
1092 bool sort, unsigned type, unsigned scope) const |
1
|
1093 { |
115
|
1094 count = 0; |
1
|
1095 int n = size (); |
|
1096 if (n == 0) |
530
|
1097 return 0; |
1
|
1098 |
1755
|
1099 string_vector symbols (n); |
|
1100 |
1
|
1101 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1102 { |
|
1103 symbol_record *ptr = table[i].next (); |
530
|
1104 while (ptr) |
1
|
1105 { |
|
1106 assert (count < n); |
605
|
1107 |
195
|
1108 unsigned my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
1109 |
195
|
1110 unsigned my_type = ptr->type (); |
605
|
1111 |
1755
|
1112 string my_name = ptr->name (); |
867
|
1113 |
|
1114 if ((type & my_type) && (scope & my_scope) |
|
1115 && (npats == 0 || matches_patterns (my_name, pats, npats))) |
1755
|
1116 symbols[count++] = ptr->name (); |
605
|
1117 |
1
|
1118 ptr = ptr->next (); |
|
1119 } |
|
1120 } |
1755
|
1121 |
|
1122 symbols.resize (count); |
1
|
1123 |
1755
|
1124 if (sort && ! symbols.empty ()) |
|
1125 symbols.qsort (); |
1
|
1126 |
|
1127 return symbols; |
|
1128 } |
|
1129 |
605
|
1130 symbol_record ** |
1755
|
1131 symbol_table::glob (int& count, const string& pat, unsigned type, |
605
|
1132 unsigned scope) const |
|
1133 { |
|
1134 count = 0; |
|
1135 int n = size (); |
|
1136 if (n == 0) |
|
1137 return 0; |
|
1138 |
|
1139 symbol_record **symbols = new symbol_record * [n+1]; |
|
1140 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1141 { |
|
1142 symbol_record *ptr = table[i].next (); |
|
1143 while (ptr) |
|
1144 { |
|
1145 assert (count < n); |
|
1146 |
|
1147 unsigned my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
|
1148 |
|
1149 unsigned my_type = ptr->type (); |
|
1150 |
1792
|
1151 glob_match pattern (pat); |
1755
|
1152 |
605
|
1153 if ((type & my_type) && (scope & my_scope) |
1792
|
1154 && pattern.match (ptr->name ())) |
605
|
1155 { |
|
1156 symbols[count++] = ptr; |
|
1157 } |
|
1158 |
|
1159 ptr = ptr->next (); |
|
1160 } |
|
1161 } |
|
1162 symbols[count] = 0; |
|
1163 |
|
1164 return symbols; |
|
1165 } |
|
1166 |
220
|
1167 void |
|
1168 symbol_table::push_context (void) |
|
1169 { |
|
1170 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1171 { |
|
1172 symbol_record *ptr = table[i].next (); |
|
1173 |
530
|
1174 while (ptr) |
220
|
1175 { |
|
1176 ptr->push_context (); |
|
1177 ptr = ptr->next (); |
|
1178 } |
|
1179 } |
|
1180 } |
|
1181 |
|
1182 void |
|
1183 symbol_table::pop_context (void) |
|
1184 { |
|
1185 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1186 { |
|
1187 symbol_record *ptr = table[i].next (); |
|
1188 |
530
|
1189 while (ptr) |
220
|
1190 { |
|
1191 ptr->pop_context (); |
|
1192 ptr = ptr->next (); |
|
1193 } |
|
1194 } |
|
1195 } |
|
1196 |
1
|
1197 // Chris Torek's fave hash function. |
|
1198 |
|
1199 unsigned int |
1755
|
1200 symbol_table::hash (const string& str) |
1
|
1201 { |
|
1202 unsigned h = 0; |
1755
|
1203 for (unsigned i = 0; i < str.length (); i++) |
|
1204 h = h * 33 + str[i]; |
1
|
1205 return h; |
|
1206 } |
|
1207 |
1962
|
1208 // Return nonzero if S is a valid identifier. |
|
1209 |
2856
|
1210 bool |
1962
|
1211 valid_identifier (const char *s) |
|
1212 { |
|
1213 if (! s || ! (isalnum (*s) || *s == '_')) |
2856
|
1214 return false; |
1962
|
1215 |
|
1216 while (*++s != '\0') |
|
1217 if (! (isalnum (*s) || *s == '_')) |
2856
|
1218 return false; |
1962
|
1219 |
2856
|
1220 return true; |
1962
|
1221 } |
|
1222 |
2856
|
1223 bool |
2790
|
1224 valid_identifier (const string& s) |
|
1225 { |
|
1226 return valid_identifier (s.c_str ()); |
|
1227 } |
|
1228 |
1
|
1229 /* |
|
1230 ;;; Local Variables: *** |
|
1231 ;;; mode: C++ *** |
|
1232 ;;; End: *** |
|
1233 */ |