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 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 // Don't even think about moving the tree.h include to symtab.h... |
|
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 { |
|
184 return definition->save (os, mark_as_global); |
|
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 { |
|
531 int status = 0; |
|
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... |
|
536 |
|
537 if (is_read_only ()) |
1
|
538 { |
195
|
539 warning ("save: sorry, can't save read-only variable `%s'", nm); |
|
540 status = -1; |
1
|
541 } |
195
|
542 else if (is_function ()) |
|
543 { |
|
544 warning ("save: sorry, can't save function `%s'", nm); |
|
545 status = -1; |
|
546 } |
1
|
547 else |
111
|
548 { |
195
|
549 // Should we also save the help string? Maybe someday. |
|
550 |
|
551 os << "# name: " << nm << "\n"; |
|
552 |
|
553 status = definition->save (os, mark_as_global); |
111
|
554 } |
1
|
555 |
|
556 return status; |
|
557 } |
|
558 |
186
|
559 int |
195
|
560 symbol_record::clear (void) |
1
|
561 { |
195
|
562 int count = 0; |
|
563 if (linked_to_global) |
1
|
564 { |
195
|
565 count = maybe_delete (definition); |
|
566 definition = (symbol_def *) NULL; |
|
567 linked_to_global = 0; |
1
|
568 } |
195
|
569 else |
|
570 { |
|
571 symbol_def *old_def = pop_def (); |
|
572 count = maybe_delete (old_def); |
|
573 } |
|
574 return count; |
1
|
575 } |
|
576 |
|
577 void |
195
|
578 symbol_record::alias (symbol_record *s, int force = 0) |
1
|
579 { |
195
|
580 sv_fcn = s->sv_fcn; |
|
581 |
|
582 if (force && s->definition == (symbol_def *) NULL) |
1
|
583 { |
195
|
584 s->definition = new symbol_def (); |
|
585 definition = s->definition; |
|
586 definition->count = 2; // Yes, this is correct. |
1
|
587 } |
195
|
588 else if (s->definition != (symbol_def *) NULL) |
1
|
589 { |
195
|
590 definition = s->definition; |
|
591 definition->count++; |
1
|
592 } |
|
593 } |
|
594 |
|
595 void |
|
596 symbol_record::mark_as_formal_parameter (void) |
|
597 { |
|
598 formal_param = 1; |
|
599 } |
|
600 |
|
601 int |
164
|
602 symbol_record::is_formal_parameter (void) const |
1
|
603 { |
|
604 return formal_param; |
|
605 } |
|
606 |
|
607 void |
195
|
608 symbol_record::mark_as_linked_to_global (void) |
122
|
609 { |
195
|
610 linked_to_global = 1; |
122
|
611 } |
|
612 |
|
613 int |
195
|
614 symbol_record::is_linked_to_global (void) const |
1
|
615 { |
195
|
616 return linked_to_global; |
1
|
617 } |
|
618 |
|
619 symbol_record * |
164
|
620 symbol_record::next (void) const |
1
|
621 { |
|
622 return next_elem; |
|
623 } |
|
624 |
195
|
625 void |
|
626 symbol_record::chain (symbol_record *s) |
|
627 { |
|
628 next_elem = s; |
|
629 } |
|
630 |
220
|
631 void |
|
632 symbol_record::push_context (void) |
|
633 { |
|
634 context.push (definition); |
|
635 definition = (symbol_def *) NULL; |
|
636 } |
|
637 |
|
638 void |
|
639 symbol_record::pop_context (void) |
|
640 { |
|
641 assert (! context.empty ()); |
|
642 |
|
643 if (is_variable ()) |
|
644 { |
|
645 symbol_def *old_def = pop_def (); |
|
646 maybe_delete (old_def); |
|
647 } |
|
648 |
|
649 if (is_function ()) |
|
650 { |
|
651 symbol_def *old_def = pop_def (); |
|
652 maybe_delete (old_def); |
|
653 } |
|
654 |
|
655 definition = context.pop (); |
|
656 } |
|
657 |
195
|
658 int |
|
659 symbol_record::read_only_error (void) |
|
660 { |
|
661 if (is_read_only ()) |
|
662 { |
|
663 char *tag = "symbol"; |
|
664 if (is_variable ()) |
|
665 tag = "variable"; |
|
666 else if (is_function ()) |
|
667 tag = "function"; |
|
668 |
|
669 error ("can't redefined read-only %s `%s'", tag, nm); |
|
670 |
|
671 return 1; |
|
672 } |
|
673 else |
|
674 return 0; |
|
675 } |
|
676 |
|
677 void |
|
678 symbol_record::push_def (symbol_def *sd) |
|
679 { |
|
680 if (sd == (symbol_def *) NULL) |
|
681 return; |
|
682 |
|
683 sd->next_elem = definition; |
|
684 definition = sd; |
|
685 } |
|
686 |
|
687 symbol_def * |
|
688 symbol_record::pop_def (void) |
|
689 { |
|
690 symbol_def *top = definition; |
|
691 if (definition != (symbol_def *) NULL) |
|
692 definition = definition->next_elem; |
|
693 return top; |
|
694 } |
|
695 |
|
696 /* |
|
697 * A structure for handling verbose information about a symbol_record. |
|
698 */ |
|
699 |
|
700 symbol_record_info::symbol_record_info (void) |
|
701 { |
|
702 init_state (); |
|
703 } |
|
704 |
|
705 symbol_record_info::symbol_record_info (const symbol_record& sr) |
|
706 { |
|
707 init_state (); |
|
708 |
|
709 type = sr.type (); |
|
710 |
|
711 if (sr.is_variable () && sr.is_defined ()) |
|
712 { |
|
713 // Would be nice to avoid this cast. XXX FIXME XXX |
|
714 tree_constant *tmp = (tree_constant *) sr.def (); |
|
715 switch (tmp->const_type ()) |
|
716 { |
|
717 case tree_constant_rep::scalar_constant: |
|
718 const_type = SR_INFO_SCALAR; |
|
719 break; |
|
720 case tree_constant_rep::complex_scalar_constant: |
|
721 const_type = SR_INFO_COMPLEX_SCALAR; |
|
722 break; |
|
723 case tree_constant_rep::matrix_constant: |
|
724 const_type = SR_INFO_MATRIX; |
|
725 break; |
|
726 case tree_constant_rep::complex_matrix_constant: |
|
727 const_type = SR_INFO_COMPLEX_MATRIX; |
|
728 break; |
|
729 case tree_constant_rep::range_constant: |
|
730 const_type = SR_INFO_RANGE; |
|
731 break; |
|
732 case tree_constant_rep::string_constant: |
|
733 const_type = SR_INFO_STRING; |
|
734 break; |
|
735 default: |
|
736 break; |
|
737 } |
|
738 |
|
739 nr = tmp->rows (); |
|
740 nc = tmp->columns (); |
|
741 |
|
742 symbol_def *sr_def = sr.definition; |
|
743 symbol_def *hidden_def = sr_def->next_elem; |
|
744 if (hidden_def != (symbol_def *) NULL) |
|
745 { |
|
746 if (hidden_def->is_user_function ()) |
|
747 hides = SR_INFO_USER_FUNCTION; |
|
748 else if (hidden_def->is_builtin_function ()) |
|
749 hides = SR_INFO_BUILTIN_FUNCTION; |
|
750 } |
|
751 } |
|
752 |
|
753 eternal = sr.is_eternal (); |
|
754 read_only = sr.is_read_only (); |
|
755 |
|
756 nm = strsave (sr.name ()); |
|
757 |
|
758 initialized = 1; |
|
759 } |
|
760 |
|
761 symbol_record_info::symbol_record_info (const symbol_record_info& s) |
|
762 { |
|
763 type = s.type; |
|
764 const_type = s.const_type; |
|
765 hides = s.hides; |
|
766 eternal = s.eternal; |
|
767 read_only = s.read_only; |
|
768 nr = s.nr; |
|
769 nc = s.nc; |
|
770 nm = strsave (s.nm); |
|
771 initialized = s.initialized; |
|
772 } |
|
773 |
|
774 symbol_record_info::~symbol_record_info (void) |
|
775 { |
|
776 delete nm; |
|
777 } |
|
778 |
|
779 symbol_record_info& |
|
780 symbol_record_info::operator = (const symbol_record_info& s) |
|
781 { |
|
782 if (this != &s) |
|
783 { |
|
784 delete nm; |
|
785 type = s.type; |
|
786 const_type = s.const_type; |
|
787 hides = s.hides; |
|
788 eternal = s.eternal; |
|
789 read_only = s.read_only; |
|
790 nr = s.nr; |
|
791 nc = s.nc; |
|
792 nm = strsave (s.nm); |
|
793 initialized = s.initialized; |
|
794 } |
|
795 return *this; |
|
796 } |
|
797 |
|
798 int |
|
799 symbol_record_info::is_defined (void) const |
|
800 { |
|
801 return initialized; |
|
802 } |
|
803 |
|
804 int |
|
805 symbol_record_info::is_read_only (void) const |
|
806 { |
|
807 return read_only; |
|
808 } |
|
809 |
|
810 int |
|
811 symbol_record_info::is_eternal (void) const |
|
812 { |
|
813 return eternal; |
|
814 } |
|
815 |
|
816 int |
|
817 symbol_record_info::hides_fcn (void) const |
|
818 { |
|
819 return (hides & SR_INFO_USER_FUNCTION); |
|
820 } |
|
821 |
|
822 int |
|
823 symbol_record_info::hides_builtin (void) const |
|
824 { |
|
825 return (hides & SR_INFO_BUILTIN_FUNCTION); |
|
826 } |
|
827 |
|
828 char * |
|
829 symbol_record_info::type_as_string (void) const |
|
830 { |
|
831 if (type == symbol_def::USER_FUNCTION) |
|
832 return "user function"; |
|
833 else if (type == symbol_def::BUILTIN_FUNCTION) |
|
834 return "builtin function"; |
|
835 else |
|
836 { |
|
837 if (const_type == SR_INFO_SCALAR) |
|
838 return "real scalar"; |
|
839 else if (const_type == SR_INFO_COMPLEX_SCALAR) |
|
840 return "complex scalar"; |
|
841 else if (const_type == SR_INFO_MATRIX) |
|
842 return "real matrix"; |
|
843 else if (const_type == SR_INFO_COMPLEX_MATRIX) |
|
844 return "complex matrix"; |
|
845 else if (const_type == SR_INFO_RANGE) |
|
846 return "range"; |
|
847 else if (const_type == SR_INFO_STRING) |
|
848 return "string"; |
|
849 else |
|
850 return ""; |
|
851 } |
|
852 } |
|
853 |
|
854 int |
|
855 symbol_record_info::is_function (void) const |
|
856 { |
|
857 return (type == symbol_def::USER_FUNCTION |
|
858 || type == symbol_def::BUILTIN_FUNCTION); |
|
859 } |
|
860 |
|
861 int |
|
862 symbol_record_info::rows (void) const |
|
863 { |
|
864 return nr; |
|
865 } |
|
866 |
|
867 int |
|
868 symbol_record_info::columns (void) const |
|
869 { |
|
870 return nc; |
|
871 } |
|
872 |
|
873 char * |
|
874 symbol_record_info::name (void) const |
|
875 { |
|
876 return nm; |
|
877 } |
|
878 |
|
879 void |
|
880 symbol_record_info::init_state (void) |
|
881 { |
|
882 initialized = 0; |
|
883 type = symbol_def::UNKNOWN; |
|
884 const_type = SR_INFO_UNKNOWN; |
|
885 hides = SR_INFO_NONE; |
|
886 eternal = 0; |
|
887 read_only = 0; |
|
888 nr = -1; |
|
889 nc = -1; |
|
890 nm = (char *) NULL; |
|
891 } |
|
892 |
1
|
893 /* |
|
894 * A symbol table. |
|
895 */ |
|
896 |
|
897 symbol_table::symbol_table (void) |
|
898 { |
|
899 } |
|
900 |
|
901 symbol_record * |
164
|
902 symbol_table::lookup (const char *nm, int insert = 0, int warn = 0) |
1
|
903 { |
|
904 int index = hash (nm) & HASH_MASK; |
|
905 |
|
906 symbol_record *ptr = table[index].next (); |
|
907 |
|
908 while (ptr != (symbol_record *) NULL) |
|
909 { |
|
910 if (strcmp (ptr->name (), nm) == 0) |
|
911 return ptr; |
|
912 ptr = ptr->next (); |
|
913 } |
|
914 |
|
915 if (insert) |
|
916 { |
|
917 symbol_record *new_sym; |
|
918 new_sym = new symbol_record (nm, table[index].next ()); |
195
|
919 table[index].chain (new_sym); |
1
|
920 return new_sym; |
|
921 } |
|
922 else if (warn) |
217
|
923 warning ("lookup: symbol`%s' not found", nm); |
1
|
924 |
|
925 return (symbol_record *) NULL; |
|
926 } |
|
927 |
|
928 void |
195
|
929 symbol_table::clear (int clear_user_functions = 1) |
1
|
930 { |
|
931 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
932 { |
169
|
933 symbol_record *ptr = table[i].next (); |
1
|
934 |
169
|
935 while (ptr != (symbol_record *) NULL) |
|
936 { |
195
|
937 if (ptr->is_user_variable () |
|
938 || (clear_user_functions && ptr->is_user_function ())) |
|
939 { |
|
940 ptr->clear (); |
|
941 } |
|
942 |
169
|
943 ptr = ptr->next (); |
1
|
944 } |
|
945 } |
|
946 } |
|
947 |
|
948 int |
195
|
949 symbol_table::clear (const char *nm, int clear_user_functions = 1) |
1
|
950 { |
|
951 int index = hash (nm) & HASH_MASK; |
|
952 |
195
|
953 symbol_record *ptr = table[index].next (); |
1
|
954 |
195
|
955 while (ptr != (symbol_record *) NULL) |
1
|
956 { |
195
|
957 if (strcmp (ptr->name (), nm) == 0 |
|
958 && (ptr->is_user_variable () |
|
959 || (clear_user_functions && ptr->is_user_function ()))) |
1
|
960 { |
195
|
961 ptr->clear (); |
|
962 return 1; |
1
|
963 } |
195
|
964 ptr = ptr->next (); |
1
|
965 } |
|
966 |
|
967 return 0; |
|
968 } |
|
969 |
|
970 int |
|
971 symbol_table::save (ostream& os, int mark_as_global = 0) |
|
972 { |
|
973 int status = 0; |
195
|
974 int count; |
|
975 char **names = list (count, 1, symbol_def::USER_VARIABLE); |
200
|
976 char **ptr = names; |
|
977 if (ptr != (char **) NULL) |
1
|
978 { |
200
|
979 while (*ptr != (char *) NULL) |
111
|
980 { |
200
|
981 if (save (os, *ptr, mark_as_global)) |
111
|
982 status++; |
|
983 |
200
|
984 ptr++; |
111
|
985 } |
200
|
986 delete [] names; |
1
|
987 } |
|
988 return status; |
|
989 } |
|
990 |
|
991 int |
164
|
992 symbol_table::save (ostream& os, const char *name, int mark_as_global = 0) |
1
|
993 { |
|
994 int status = 0; |
|
995 symbol_record *sr = lookup (name, 0, 0); |
|
996 if (sr != (symbol_record *) NULL) |
|
997 status = sr->save (os, mark_as_global); |
|
998 return status; |
|
999 } |
|
1000 |
|
1001 int |
164
|
1002 symbol_table::size (void) const |
1
|
1003 { |
|
1004 int count = 0; |
|
1005 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1006 { |
|
1007 symbol_record *ptr = table[i].next (); |
|
1008 while (ptr != (symbol_record *) NULL) |
|
1009 { |
|
1010 count++; |
|
1011 ptr = ptr->next (); |
|
1012 } |
|
1013 } |
|
1014 return count; |
|
1015 } |
|
1016 |
195
|
1017 static inline int |
|
1018 pstrcmp (char **a, char **b) |
1
|
1019 { |
195
|
1020 return strcmp (*a, *b); |
1
|
1021 } |
|
1022 |
195
|
1023 static inline int |
|
1024 symbol_record_info_cmp (symbol_record_info *a, symbol_record_info *b) |
1
|
1025 { |
195
|
1026 return strcmp (a->name (), b->name ()); |
1
|
1027 } |
|
1028 |
195
|
1029 // This function should probably share code with symbol_table::list. |
|
1030 // XXX FIXME XXX |
1
|
1031 |
195
|
1032 symbol_record_info * |
|
1033 symbol_table::long_list (int& count, int sort = 0, |
|
1034 unsigned type = SYMTAB_ALL_TYPES, |
|
1035 unsigned scope = SYMTAB_ALL_SCOPES) const |
1
|
1036 { |
115
|
1037 count = 0; |
1
|
1038 int n = size (); |
|
1039 if (n == 0) |
195
|
1040 return (symbol_record_info *) NULL; |
1
|
1041 |
195
|
1042 symbol_record_info *symbols = new symbol_record_info [n+1]; |
1
|
1043 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1044 { |
|
1045 symbol_record *ptr = table[i].next (); |
|
1046 while (ptr != (symbol_record *) NULL) |
|
1047 { |
|
1048 assert (count < n); |
195
|
1049 unsigned my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
|
1050 unsigned my_type = ptr->type (); |
|
1051 if ((type & my_type) && (scope & my_scope)) |
|
1052 { |
|
1053 symbols[count++] = symbol_record_info (*ptr); |
|
1054 } |
1
|
1055 ptr = ptr->next (); |
|
1056 } |
|
1057 } |
195
|
1058 symbols[count] = symbol_record_info (); |
|
1059 |
|
1060 if (sort && symbols != (symbol_record_info *) NULL) |
|
1061 qsort ((void *) symbols, count, sizeof (symbol_record_info), |
|
1062 (int (*)(void*, void*)) symbol_record_info_cmp); |
|
1063 |
1
|
1064 return symbols; |
|
1065 } |
|
1066 |
|
1067 char ** |
195
|
1068 symbol_table::list (int& count, int sort = 0, |
|
1069 unsigned type = SYMTAB_ALL_TYPES, |
|
1070 unsigned scope = SYMTAB_ALL_SCOPES) const |
1
|
1071 { |
115
|
1072 count = 0; |
1
|
1073 int n = size (); |
|
1074 if (n == 0) |
|
1075 return (char **) NULL; |
|
1076 |
|
1077 char **symbols = new char * [n+1]; |
|
1078 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1079 { |
|
1080 symbol_record *ptr = table[i].next (); |
|
1081 while (ptr != (symbol_record *) NULL) |
|
1082 { |
|
1083 assert (count < n); |
195
|
1084 unsigned my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
|
1085 unsigned my_type = ptr->type (); |
|
1086 if ((type & my_type) && (scope & my_scope)) |
1
|
1087 symbols[count++] = strsave (ptr->name ()); |
|
1088 ptr = ptr->next (); |
|
1089 } |
|
1090 } |
|
1091 symbols[count] = (char *) NULL; |
|
1092 |
195
|
1093 if (sort && symbols != (char **) NULL) |
1
|
1094 qsort ((void **) symbols, count, sizeof (char *), |
|
1095 (int (*)(void*, void*)) pstrcmp); |
|
1096 |
|
1097 return symbols; |
|
1098 } |
|
1099 |
220
|
1100 void |
|
1101 symbol_table::push_context (void) |
|
1102 { |
|
1103 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1104 { |
|
1105 symbol_record *ptr = table[i].next (); |
|
1106 |
|
1107 while (ptr != (symbol_record *) NULL) |
|
1108 { |
|
1109 ptr->push_context (); |
|
1110 ptr = ptr->next (); |
|
1111 } |
|
1112 } |
|
1113 } |
|
1114 |
|
1115 void |
|
1116 symbol_table::pop_context (void) |
|
1117 { |
|
1118 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1119 { |
|
1120 symbol_record *ptr = table[i].next (); |
|
1121 |
|
1122 while (ptr != (symbol_record *) NULL) |
|
1123 { |
|
1124 ptr->pop_context (); |
|
1125 ptr = ptr->next (); |
|
1126 } |
|
1127 } |
|
1128 } |
|
1129 |
1
|
1130 // Chris Torek's fave hash function. |
|
1131 |
|
1132 unsigned int |
|
1133 symbol_table::hash (const char *str) |
|
1134 { |
|
1135 unsigned h = 0; |
|
1136 while (*str) |
|
1137 h = h * 33 + *str++; |
|
1138 return h; |
|
1139 } |
|
1140 |
|
1141 /* |
|
1142 ;;; Local Variables: *** |
|
1143 ;;; mode: C++ *** |
|
1144 ;;; page-delimiter: "^/\\*" *** |
|
1145 ;;; End: *** |
|
1146 */ |