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