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