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