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