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