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 |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
|
28 #include "symtab.h" |
|
29 #include "error.h" |
|
30 #include "variables.h" |
|
31 #include "utils.h" |
1045
|
32 #include "user-prefs.h" |
584
|
33 #include "tree-base.h" |
|
34 #include "tree-expr.h" |
1
|
35 #include "tree-const.h" |
|
36 |
605
|
37 extern "C" |
|
38 { |
|
39 #include "fnmatch.h" |
|
40 } |
|
41 |
767
|
42 // Variables and functions. |
|
43 |
1
|
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 |
195
|
198 maybe_delete (symbol_def *def) |
|
199 { |
|
200 int count = 0; |
530
|
201 if (def && def->count > 0) |
195
|
202 { |
530
|
203 def->count--; |
|
204 count = def->count; |
|
205 if (def->count == 0) |
|
206 delete def; |
195
|
207 } |
|
208 return count; |
|
209 } |
|
210 |
767
|
211 // Individual records in a symbol table. |
|
212 |
1
|
213 symbol_record::symbol_record (void) |
|
214 { |
195
|
215 init_state (); |
1
|
216 } |
|
217 |
530
|
218 symbol_record::symbol_record (const char *n, symbol_record *nxt) |
1
|
219 { |
195
|
220 init_state (); |
1
|
221 nm = strsave (n); |
195
|
222 next_elem = nxt; |
1
|
223 } |
|
224 |
195
|
225 void |
|
226 symbol_record::init_state (void) |
1
|
227 { |
|
228 formal_param = 0; |
195
|
229 linked_to_global = 0; |
530
|
230 nm = 0; |
|
231 sv_fcn = 0; |
|
232 definition = 0; |
|
233 next_elem = 0; |
1
|
234 } |
|
235 |
|
236 symbol_record::~symbol_record (void) |
|
237 { |
|
238 delete [] nm; |
|
239 } |
|
240 |
|
241 char * |
164
|
242 symbol_record::name (void) const |
1
|
243 { |
|
244 return nm; |
|
245 } |
|
246 |
|
247 char * |
164
|
248 symbol_record::help (void) const |
1
|
249 { |
530
|
250 return definition ? definition->help () : 0; |
1
|
251 } |
|
252 |
489
|
253 tree_fvc * |
164
|
254 symbol_record::def (void) const |
1
|
255 { |
530
|
256 return definition ? definition->def () : 0; |
1
|
257 } |
|
258 |
572
|
259 void |
|
260 symbol_record::rename (const char *new_name) |
|
261 { |
|
262 delete [] nm; |
|
263 nm = strsave (new_name); |
|
264 } |
|
265 |
1
|
266 int |
164
|
267 symbol_record::is_function (void) const |
1
|
268 { |
530
|
269 return definition ? definition->is_function () : 0; |
|
270 } |
|
271 |
|
272 int |
|
273 symbol_record::is_text_function (void) const |
|
274 { |
|
275 return definition ? definition->is_text_function () : 0; |
|
276 } |
|
277 |
|
278 int |
|
279 symbol_record::is_mapper_function (void) const |
|
280 { |
|
281 return definition ? definition->is_mapper_function () : 0; |
195
|
282 } |
|
283 |
|
284 int |
|
285 symbol_record::is_user_function (void) const |
|
286 { |
530
|
287 return definition ? definition->is_user_function () : 0; |
195
|
288 } |
|
289 |
|
290 int |
|
291 symbol_record::is_builtin_function (void) const |
|
292 { |
530
|
293 return definition ? definition->is_builtin_function () : 0; |
1
|
294 } |
|
295 |
|
296 int |
164
|
297 symbol_record::is_variable (void) const |
1
|
298 { |
530
|
299 return definition ? definition->is_variable () : 0; |
195
|
300 } |
|
301 |
|
302 int |
|
303 symbol_record::is_user_variable (void) const |
|
304 { |
530
|
305 return definition ? definition->is_user_variable () : 0; |
195
|
306 } |
|
307 |
|
308 int |
|
309 symbol_record::is_builtin_variable (void) const |
|
310 { |
530
|
311 return definition ? definition->is_builtin_variable () : 0; |
195
|
312 } |
|
313 |
|
314 unsigned |
|
315 symbol_record::type (void) const |
|
316 { |
530
|
317 return definition ? definition->type : 0; |
1
|
318 } |
|
319 |
|
320 int |
164
|
321 symbol_record::is_defined (void) const |
1
|
322 { |
530
|
323 return definition ? (definition->def () != 0) : 0; |
195
|
324 } |
|
325 |
|
326 int |
|
327 symbol_record::is_read_only (void) const |
|
328 { |
530
|
329 return definition ? definition->read_only : 0; |
195
|
330 } |
|
331 |
|
332 int |
|
333 symbol_record::is_eternal (void) const |
|
334 { |
530
|
335 return definition ? definition->eternal : 0; |
195
|
336 } |
|
337 |
|
338 void |
|
339 symbol_record::protect (void) |
|
340 { |
530
|
341 if (definition) |
195
|
342 { |
|
343 definition->protect (); |
|
344 |
|
345 if (! is_defined ()) |
|
346 warning ("protecting undefined variable `%s'", nm); |
|
347 } |
|
348 } |
|
349 |
|
350 void |
|
351 symbol_record::unprotect (void) |
|
352 { |
530
|
353 if (definition) |
195
|
354 definition->unprotect (); |
|
355 } |
|
356 |
|
357 void |
|
358 symbol_record::make_eternal (void) |
|
359 { |
530
|
360 if (definition) |
195
|
361 { |
|
362 definition->make_eternal (); |
|
363 |
|
364 if (! is_defined ()) |
|
365 warning ("giving eternal life to undefined variable `%s'", nm); |
|
366 } |
1
|
367 } |
|
368 |
|
369 void |
|
370 symbol_record::set_sv_function (sv_Function f) |
|
371 { |
|
372 sv_fcn = f; |
|
373 } |
|
374 |
|
375 int |
|
376 symbol_record::define (tree_constant *t) |
|
377 { |
195
|
378 if (is_variable () && read_only_error ()) |
1
|
379 return 0; |
|
380 |
530
|
381 tree_fvc *saved_def = 0; |
|
382 if (! definition) |
195
|
383 { |
|
384 definition = new symbol_def (); |
|
385 definition->count = 1; |
|
386 } |
|
387 else if (is_function ()) |
1
|
388 { |
195
|
389 symbol_def *new_def = new symbol_def (); |
|
390 push_def (new_def); |
|
391 definition->count = 1; |
1
|
392 } |
195
|
393 else if (is_variable ()) |
1
|
394 { |
195
|
395 saved_def = definition->def (); |
1
|
396 } |
|
397 |
195
|
398 definition->define (t); |
|
399 |
530
|
400 if (sv_fcn && sv_fcn () < 0) |
1
|
401 { |
195
|
402 // Would be nice to be able to avoid this cast. XXX FIXME XXX |
|
403 definition->define ((tree_constant *) saved_def); |
1
|
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 if (is_variable ()) |
1045
|
625 { |
|
626 if (user_pref.read_only_constants) |
|
627 { |
|
628 if (user_pref.read_only_constants < 0) |
|
629 { |
|
630 ::warning ("redefinition of constant `%s'", nm); |
|
631 return 0; |
|
632 } |
|
633 else |
|
634 ::error ("can't redefine read-only constant `%s'", nm); |
|
635 } |
|
636 } |
195
|
637 else if (is_function ()) |
1045
|
638 { |
|
639 ::error ("can't redefine read-only function `%s'", nm); |
|
640 } |
|
641 else |
|
642 { |
|
643 ::error ("can't redefine read-only symbol `%s'", nm); |
|
644 } |
195
|
645 |
|
646 return 1; |
|
647 } |
|
648 else |
|
649 return 0; |
|
650 } |
|
651 |
|
652 void |
|
653 symbol_record::push_def (symbol_def *sd) |
|
654 { |
530
|
655 if (! sd) |
195
|
656 return; |
|
657 |
|
658 sd->next_elem = definition; |
|
659 definition = sd; |
|
660 } |
|
661 |
|
662 symbol_def * |
|
663 symbol_record::pop_def (void) |
|
664 { |
|
665 symbol_def *top = definition; |
530
|
666 if (definition) |
195
|
667 definition = definition->next_elem; |
|
668 return top; |
|
669 } |
|
670 |
767
|
671 // A structure for handling verbose information about a symbol_record. |
195
|
672 |
|
673 symbol_record_info::symbol_record_info (void) |
|
674 { |
|
675 init_state (); |
|
676 } |
|
677 |
|
678 symbol_record_info::symbol_record_info (const symbol_record& sr) |
|
679 { |
|
680 init_state (); |
|
681 |
|
682 type = sr.type (); |
|
683 |
|
684 if (sr.is_variable () && sr.is_defined ()) |
|
685 { |
|
686 // Would be nice to avoid this cast. XXX FIXME XXX |
|
687 tree_constant *tmp = (tree_constant *) sr.def (); |
620
|
688 if (tmp->is_real_scalar ()) |
|
689 const_type = SR_INFO_SCALAR; |
|
690 else if (tmp->is_complex_scalar ()) |
|
691 const_type = SR_INFO_COMPLEX_SCALAR; |
|
692 else if (tmp->is_real_matrix ()) |
|
693 const_type = SR_INFO_MATRIX; |
|
694 else if (tmp->is_complex_matrix ()) |
|
695 const_type = SR_INFO_COMPLEX_MATRIX; |
|
696 else if (tmp->is_range ()) |
|
697 const_type = SR_INFO_RANGE; |
|
698 else if (tmp->is_string ()) |
|
699 const_type = SR_INFO_STRING; |
195
|
700 |
|
701 nr = tmp->rows (); |
|
702 nc = tmp->columns (); |
|
703 |
|
704 symbol_def *sr_def = sr.definition; |
|
705 symbol_def *hidden_def = sr_def->next_elem; |
530
|
706 if (hidden_def) |
195
|
707 { |
|
708 if (hidden_def->is_user_function ()) |
|
709 hides = SR_INFO_USER_FUNCTION; |
|
710 else if (hidden_def->is_builtin_function ()) |
|
711 hides = SR_INFO_BUILTIN_FUNCTION; |
|
712 } |
|
713 } |
|
714 |
|
715 eternal = sr.is_eternal (); |
|
716 read_only = sr.is_read_only (); |
|
717 |
|
718 nm = strsave (sr.name ()); |
|
719 |
|
720 initialized = 1; |
|
721 } |
|
722 |
|
723 symbol_record_info::symbol_record_info (const symbol_record_info& s) |
|
724 { |
|
725 type = s.type; |
|
726 const_type = s.const_type; |
|
727 hides = s.hides; |
|
728 eternal = s.eternal; |
|
729 read_only = s.read_only; |
|
730 nr = s.nr; |
|
731 nc = s.nc; |
|
732 nm = strsave (s.nm); |
|
733 initialized = s.initialized; |
|
734 } |
|
735 |
|
736 symbol_record_info::~symbol_record_info (void) |
|
737 { |
|
738 delete nm; |
|
739 } |
|
740 |
|
741 symbol_record_info& |
|
742 symbol_record_info::operator = (const symbol_record_info& s) |
|
743 { |
|
744 if (this != &s) |
|
745 { |
|
746 delete nm; |
|
747 type = s.type; |
|
748 const_type = s.const_type; |
|
749 hides = s.hides; |
|
750 eternal = s.eternal; |
|
751 read_only = s.read_only; |
|
752 nr = s.nr; |
|
753 nc = s.nc; |
|
754 nm = strsave (s.nm); |
|
755 initialized = s.initialized; |
|
756 } |
|
757 return *this; |
|
758 } |
|
759 |
|
760 int |
|
761 symbol_record_info::is_defined (void) const |
|
762 { |
|
763 return initialized; |
|
764 } |
|
765 |
|
766 int |
|
767 symbol_record_info::is_read_only (void) const |
|
768 { |
|
769 return read_only; |
|
770 } |
|
771 |
|
772 int |
|
773 symbol_record_info::is_eternal (void) const |
|
774 { |
|
775 return eternal; |
|
776 } |
|
777 |
|
778 int |
|
779 symbol_record_info::hides_fcn (void) const |
|
780 { |
|
781 return (hides & SR_INFO_USER_FUNCTION); |
|
782 } |
|
783 |
|
784 int |
|
785 symbol_record_info::hides_builtin (void) const |
|
786 { |
|
787 return (hides & SR_INFO_BUILTIN_FUNCTION); |
|
788 } |
|
789 |
|
790 char * |
|
791 symbol_record_info::type_as_string (void) const |
|
792 { |
|
793 if (type == symbol_def::USER_FUNCTION) |
|
794 return "user function"; |
|
795 else if (type == symbol_def::BUILTIN_FUNCTION) |
|
796 return "builtin function"; |
|
797 else |
|
798 { |
|
799 if (const_type == SR_INFO_SCALAR) |
|
800 return "real scalar"; |
|
801 else if (const_type == SR_INFO_COMPLEX_SCALAR) |
|
802 return "complex scalar"; |
|
803 else if (const_type == SR_INFO_MATRIX) |
|
804 return "real matrix"; |
|
805 else if (const_type == SR_INFO_COMPLEX_MATRIX) |
|
806 return "complex matrix"; |
|
807 else if (const_type == SR_INFO_RANGE) |
|
808 return "range"; |
|
809 else if (const_type == SR_INFO_STRING) |
|
810 return "string"; |
|
811 else |
|
812 return ""; |
|
813 } |
|
814 } |
|
815 |
|
816 int |
|
817 symbol_record_info::is_function (void) const |
|
818 { |
|
819 return (type == symbol_def::USER_FUNCTION |
|
820 || type == symbol_def::BUILTIN_FUNCTION); |
|
821 } |
|
822 |
|
823 int |
|
824 symbol_record_info::rows (void) const |
|
825 { |
|
826 return nr; |
|
827 } |
|
828 |
|
829 int |
|
830 symbol_record_info::columns (void) const |
|
831 { |
|
832 return nc; |
|
833 } |
|
834 |
|
835 char * |
|
836 symbol_record_info::name (void) const |
|
837 { |
|
838 return nm; |
|
839 } |
|
840 |
|
841 void |
|
842 symbol_record_info::init_state (void) |
|
843 { |
|
844 initialized = 0; |
|
845 type = symbol_def::UNKNOWN; |
|
846 const_type = SR_INFO_UNKNOWN; |
|
847 hides = SR_INFO_NONE; |
|
848 eternal = 0; |
|
849 read_only = 0; |
|
850 nr = -1; |
|
851 nc = -1; |
530
|
852 nm = 0; |
195
|
853 } |
|
854 |
767
|
855 // A symbol table. |
1
|
856 |
|
857 symbol_table::symbol_table (void) |
|
858 { |
|
859 } |
|
860 |
|
861 symbol_record * |
530
|
862 symbol_table::lookup (const char *nm, int insert, int warn) |
1
|
863 { |
|
864 int index = hash (nm) & HASH_MASK; |
|
865 |
|
866 symbol_record *ptr = table[index].next (); |
|
867 |
530
|
868 while (ptr) |
1
|
869 { |
|
870 if (strcmp (ptr->name (), nm) == 0) |
|
871 return ptr; |
|
872 ptr = ptr->next (); |
|
873 } |
|
874 |
|
875 if (insert) |
|
876 { |
|
877 symbol_record *new_sym; |
|
878 new_sym = new symbol_record (nm, table[index].next ()); |
195
|
879 table[index].chain (new_sym); |
1
|
880 return new_sym; |
|
881 } |
|
882 else if (warn) |
217
|
883 warning ("lookup: symbol`%s' not found", nm); |
1
|
884 |
530
|
885 return 0; |
1
|
886 } |
|
887 |
|
888 void |
572
|
889 symbol_table::rename (const char *old_name, const char *new_name) |
|
890 { |
|
891 int index = hash (old_name) & HASH_MASK; |
|
892 |
|
893 symbol_record *prev = &table[index]; |
|
894 symbol_record *ptr = prev->next (); |
|
895 |
|
896 while (ptr) |
|
897 { |
|
898 if (strcmp (ptr->name (), old_name) == 0) |
|
899 { |
|
900 prev->chain (ptr->next ()); |
|
901 |
|
902 index = hash (new_name) & HASH_MASK; |
|
903 table[index].chain (ptr); |
|
904 ptr->rename (new_name); |
|
905 |
|
906 return; |
|
907 } |
|
908 |
|
909 prev = ptr; |
|
910 ptr = ptr->next (); |
|
911 } |
|
912 |
773
|
913 error ("unable to rename `%s' to `%s', old_name, new_name"); |
572
|
914 } |
|
915 |
|
916 void |
530
|
917 symbol_table::clear (int clear_user_functions) |
1
|
918 { |
|
919 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
920 { |
169
|
921 symbol_record *ptr = table[i].next (); |
1
|
922 |
530
|
923 while (ptr) |
169
|
924 { |
195
|
925 if (ptr->is_user_variable () |
|
926 || (clear_user_functions && ptr->is_user_function ())) |
|
927 { |
|
928 ptr->clear (); |
|
929 } |
|
930 |
169
|
931 ptr = ptr->next (); |
1
|
932 } |
|
933 } |
|
934 } |
|
935 |
|
936 int |
530
|
937 symbol_table::clear (const char *nm, int clear_user_functions) |
1
|
938 { |
|
939 int index = hash (nm) & HASH_MASK; |
|
940 |
195
|
941 symbol_record *ptr = table[index].next (); |
1
|
942 |
530
|
943 while (ptr) |
1
|
944 { |
195
|
945 if (strcmp (ptr->name (), nm) == 0 |
|
946 && (ptr->is_user_variable () |
|
947 || (clear_user_functions && ptr->is_user_function ()))) |
1
|
948 { |
195
|
949 ptr->clear (); |
|
950 return 1; |
1
|
951 } |
195
|
952 ptr = ptr->next (); |
1
|
953 } |
|
954 |
|
955 return 0; |
|
956 } |
|
957 |
|
958 int |
164
|
959 symbol_table::size (void) const |
1
|
960 { |
|
961 int count = 0; |
|
962 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
963 { |
|
964 symbol_record *ptr = table[i].next (); |
530
|
965 while (ptr) |
1
|
966 { |
|
967 count++; |
|
968 ptr = ptr->next (); |
|
969 } |
|
970 } |
|
971 return count; |
|
972 } |
|
973 |
195
|
974 static inline int |
|
975 pstrcmp (char **a, char **b) |
1
|
976 { |
195
|
977 return strcmp (*a, *b); |
1
|
978 } |
|
979 |
195
|
980 static inline int |
|
981 symbol_record_info_cmp (symbol_record_info *a, symbol_record_info *b) |
1
|
982 { |
195
|
983 return strcmp (a->name (), b->name ()); |
1
|
984 } |
|
985 |
867
|
986 static int |
|
987 matches_patterns (const char *name, char **pats, int npats) |
|
988 { |
|
989 while (npats-- > 0) |
|
990 { |
|
991 if (fnmatch (*pats, name, __FNM_FLAGS) == 0) |
|
992 return 1; |
|
993 |
|
994 pats++; |
|
995 } |
|
996 |
|
997 return 0; |
|
998 } |
|
999 |
195
|
1000 // This function should probably share code with symbol_table::list. |
|
1001 // XXX FIXME XXX |
1
|
1002 |
195
|
1003 symbol_record_info * |
867
|
1004 symbol_table::long_list (int& count, char **pats, int npats, int sort, |
|
1005 unsigned type, unsigned scope) const |
1
|
1006 { |
115
|
1007 count = 0; |
1
|
1008 int n = size (); |
|
1009 if (n == 0) |
530
|
1010 return 0; |
1
|
1011 |
195
|
1012 symbol_record_info *symbols = new symbol_record_info [n+1]; |
1
|
1013 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1014 { |
|
1015 symbol_record *ptr = table[i].next (); |
530
|
1016 while (ptr) |
1
|
1017 { |
|
1018 assert (count < n); |
867
|
1019 |
195
|
1020 unsigned my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
1021 |
195
|
1022 unsigned my_type = ptr->type (); |
867
|
1023 |
|
1024 char *my_name = ptr->name (); |
|
1025 |
|
1026 if ((type & my_type) && (scope & my_scope) |
|
1027 && (npats == 0 || matches_patterns (my_name, pats, npats))) |
|
1028 symbols[count++] = symbol_record_info (*ptr); |
|
1029 |
1
|
1030 ptr = ptr->next (); |
|
1031 } |
|
1032 } |
195
|
1033 symbols[count] = symbol_record_info (); |
|
1034 |
530
|
1035 if (sort && symbols) |
195
|
1036 qsort ((void *) symbols, count, sizeof (symbol_record_info), |
530
|
1037 (int (*)(const void*, const void*)) symbol_record_info_cmp); |
195
|
1038 |
1
|
1039 return symbols; |
|
1040 } |
|
1041 |
|
1042 char ** |
867
|
1043 symbol_table::list (int& count, char **pats, int npats, int sort, |
|
1044 unsigned type, unsigned scope) const |
1
|
1045 { |
115
|
1046 count = 0; |
1
|
1047 int n = size (); |
|
1048 if (n == 0) |
530
|
1049 return 0; |
1
|
1050 |
|
1051 char **symbols = new char * [n+1]; |
|
1052 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1053 { |
|
1054 symbol_record *ptr = table[i].next (); |
530
|
1055 while (ptr) |
1
|
1056 { |
|
1057 assert (count < n); |
605
|
1058 |
195
|
1059 unsigned my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
1060 |
195
|
1061 unsigned my_type = ptr->type (); |
605
|
1062 |
867
|
1063 char *my_name = ptr->name (); |
|
1064 |
|
1065 if ((type & my_type) && (scope & my_scope) |
|
1066 && (npats == 0 || matches_patterns (my_name, pats, npats))) |
1
|
1067 symbols[count++] = strsave (ptr->name ()); |
605
|
1068 |
1
|
1069 ptr = ptr->next (); |
|
1070 } |
|
1071 } |
530
|
1072 symbols[count] = 0; |
1
|
1073 |
530
|
1074 if (sort && symbols) |
1
|
1075 qsort ((void **) symbols, count, sizeof (char *), |
530
|
1076 (int (*)(const void*, const void*)) pstrcmp); |
1
|
1077 |
|
1078 return symbols; |
|
1079 } |
|
1080 |
605
|
1081 symbol_record ** |
|
1082 symbol_table::glob (int& count, char *pat, unsigned type, |
|
1083 unsigned scope) const |
|
1084 { |
|
1085 count = 0; |
|
1086 int n = size (); |
|
1087 if (n == 0) |
|
1088 return 0; |
|
1089 |
|
1090 symbol_record **symbols = new symbol_record * [n+1]; |
|
1091 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1092 { |
|
1093 symbol_record *ptr = table[i].next (); |
|
1094 while (ptr) |
|
1095 { |
|
1096 assert (count < n); |
|
1097 |
|
1098 unsigned my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
|
1099 |
|
1100 unsigned my_type = ptr->type (); |
|
1101 |
|
1102 if ((type & my_type) && (scope & my_scope) |
|
1103 && fnmatch (pat, ptr->name (), __FNM_FLAGS) == 0) |
|
1104 { |
|
1105 symbols[count++] = ptr; |
|
1106 } |
|
1107 |
|
1108 ptr = ptr->next (); |
|
1109 } |
|
1110 } |
|
1111 symbols[count] = 0; |
|
1112 |
|
1113 return symbols; |
|
1114 } |
|
1115 |
220
|
1116 void |
|
1117 symbol_table::push_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->push_context (); |
|
1126 ptr = ptr->next (); |
|
1127 } |
|
1128 } |
|
1129 } |
|
1130 |
|
1131 void |
|
1132 symbol_table::pop_context (void) |
|
1133 { |
|
1134 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1135 { |
|
1136 symbol_record *ptr = table[i].next (); |
|
1137 |
530
|
1138 while (ptr) |
220
|
1139 { |
|
1140 ptr->pop_context (); |
|
1141 ptr = ptr->next (); |
|
1142 } |
|
1143 } |
|
1144 } |
|
1145 |
1
|
1146 // Chris Torek's fave hash function. |
|
1147 |
|
1148 unsigned int |
|
1149 symbol_table::hash (const char *str) |
|
1150 { |
|
1151 unsigned h = 0; |
|
1152 while (*str) |
|
1153 h = h * 33 + *str++; |
|
1154 return h; |
|
1155 } |
|
1156 |
|
1157 /* |
|
1158 ;;; Local Variables: *** |
|
1159 ;;; mode: C++ *** |
|
1160 ;;; page-delimiter: "^/\\*" *** |
|
1161 ;;; End: *** |
|
1162 */ |