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