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