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