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