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