1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
1297
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
1962
|
31 #include <cctype> |
|
32 |
1792
|
33 #include "oct-glob.h" |
1755
|
34 #include "str-vec.h" |
|
35 |
1352
|
36 #include "error.h" |
1742
|
37 #include "pt-const.h" |
|
38 #include "pt-fcn.h" |
|
39 #include "pt-fvc.h" |
1755
|
40 #include "symtab.h" |
1352
|
41 #include "user-prefs.h" |
1
|
42 #include "utils.h" |
1352
|
43 #include "variables.h" |
1
|
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 definition = 0; |
|
81 next_elem = 0; |
195
|
82 count = 0; |
1
|
83 } |
|
84 |
|
85 symbol_def::~symbol_def (void) |
|
86 { |
|
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 |
1755
|
185 string |
164
|
186 symbol_def::help (void) const |
1
|
187 { |
|
188 return help_string; |
|
189 } |
|
190 |
|
191 void |
1755
|
192 symbol_def::document (const string& h) |
1
|
193 { |
1755
|
194 help_string = h; |
1
|
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 |
1755
|
218 symbol_record::symbol_record (const string& n, symbol_record *nxt) |
1
|
219 { |
195
|
220 init_state (); |
1755
|
221 nm = 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 sv_fcn = 0; |
|
231 definition = 0; |
|
232 next_elem = 0; |
1
|
233 } |
|
234 |
1755
|
235 string |
164
|
236 symbol_record::name (void) const |
1
|
237 { |
|
238 return nm; |
|
239 } |
|
240 |
1755
|
241 string |
164
|
242 symbol_record::help (void) const |
1
|
243 { |
1755
|
244 string retval; |
|
245 if (definition) |
|
246 retval = definition->help (); |
|
247 return retval; |
1
|
248 } |
|
249 |
489
|
250 tree_fvc * |
164
|
251 symbol_record::def (void) const |
1
|
252 { |
530
|
253 return definition ? definition->def () : 0; |
1
|
254 } |
|
255 |
572
|
256 void |
1755
|
257 symbol_record::rename (const string& new_name) |
572
|
258 { |
1755
|
259 nm = new_name; |
572
|
260 } |
|
261 |
1
|
262 int |
164
|
263 symbol_record::is_function (void) const |
1
|
264 { |
530
|
265 return definition ? definition->is_function () : 0; |
|
266 } |
|
267 |
|
268 int |
|
269 symbol_record::is_text_function (void) const |
|
270 { |
|
271 return definition ? definition->is_text_function () : 0; |
|
272 } |
|
273 |
|
274 int |
|
275 symbol_record::is_mapper_function (void) const |
|
276 { |
|
277 return definition ? definition->is_mapper_function () : 0; |
195
|
278 } |
|
279 |
|
280 int |
|
281 symbol_record::is_user_function (void) const |
|
282 { |
530
|
283 return definition ? definition->is_user_function () : 0; |
195
|
284 } |
|
285 |
|
286 int |
|
287 symbol_record::is_builtin_function (void) const |
|
288 { |
530
|
289 return definition ? definition->is_builtin_function () : 0; |
1
|
290 } |
|
291 |
|
292 int |
164
|
293 symbol_record::is_variable (void) const |
1
|
294 { |
530
|
295 return definition ? definition->is_variable () : 0; |
195
|
296 } |
|
297 |
|
298 int |
|
299 symbol_record::is_user_variable (void) const |
|
300 { |
530
|
301 return definition ? definition->is_user_variable () : 0; |
195
|
302 } |
|
303 |
|
304 int |
|
305 symbol_record::is_builtin_variable (void) const |
|
306 { |
530
|
307 return definition ? definition->is_builtin_variable () : 0; |
195
|
308 } |
|
309 |
|
310 unsigned |
|
311 symbol_record::type (void) const |
|
312 { |
530
|
313 return definition ? definition->type : 0; |
1
|
314 } |
|
315 |
|
316 int |
164
|
317 symbol_record::is_defined (void) const |
1
|
318 { |
530
|
319 return definition ? (definition->def () != 0) : 0; |
195
|
320 } |
|
321 |
|
322 int |
|
323 symbol_record::is_read_only (void) const |
|
324 { |
530
|
325 return definition ? definition->read_only : 0; |
195
|
326 } |
|
327 |
|
328 int |
|
329 symbol_record::is_eternal (void) const |
|
330 { |
530
|
331 return definition ? definition->eternal : 0; |
195
|
332 } |
|
333 |
|
334 void |
|
335 symbol_record::protect (void) |
|
336 { |
530
|
337 if (definition) |
195
|
338 { |
|
339 definition->protect (); |
|
340 |
|
341 if (! is_defined ()) |
1755
|
342 warning ("protecting undefined variable `%s'", nm.c_str ()); |
195
|
343 } |
|
344 } |
|
345 |
|
346 void |
|
347 symbol_record::unprotect (void) |
|
348 { |
530
|
349 if (definition) |
195
|
350 definition->unprotect (); |
|
351 } |
|
352 |
|
353 void |
|
354 symbol_record::make_eternal (void) |
|
355 { |
530
|
356 if (definition) |
195
|
357 { |
|
358 definition->make_eternal (); |
|
359 |
|
360 if (! is_defined ()) |
1755
|
361 warning ("giving eternal life to undefined variable `%s'", |
|
362 nm.c_str ()); |
195
|
363 } |
1
|
364 } |
|
365 |
|
366 void |
|
367 symbol_record::set_sv_function (sv_Function f) |
|
368 { |
|
369 sv_fcn = f; |
|
370 } |
|
371 |
|
372 int |
|
373 symbol_record::define (tree_constant *t) |
|
374 { |
195
|
375 if (is_variable () && read_only_error ()) |
1
|
376 return 0; |
|
377 |
530
|
378 tree_fvc *saved_def = 0; |
|
379 if (! definition) |
195
|
380 { |
|
381 definition = new symbol_def (); |
|
382 definition->count = 1; |
|
383 } |
|
384 else if (is_function ()) |
1
|
385 { |
195
|
386 symbol_def *new_def = new symbol_def (); |
|
387 push_def (new_def); |
|
388 definition->count = 1; |
1
|
389 } |
195
|
390 else if (is_variable ()) |
1
|
391 { |
195
|
392 saved_def = definition->def (); |
1
|
393 } |
|
394 |
195
|
395 definition->define (t); |
|
396 |
530
|
397 if (sv_fcn && sv_fcn () < 0) |
1
|
398 { |
1358
|
399 // Would be nice to be able to avoid this cast. XXX FIXME XXX |
|
400 |
195
|
401 definition->define ((tree_constant *) saved_def); |
1
|
402 return 0; |
|
403 } |
|
404 |
|
405 delete saved_def; |
|
406 |
|
407 return 1; |
|
408 } |
|
409 |
|
410 int |
530
|
411 symbol_record::define (tree_builtin *t, int text_fcn) |
1
|
412 { |
195
|
413 if (read_only_error ()) |
1
|
414 return 0; |
|
415 |
195
|
416 if (is_variable ()) |
1
|
417 { |
195
|
418 symbol_def *old_def = pop_def (); |
|
419 maybe_delete (old_def); |
1
|
420 } |
|
421 |
195
|
422 if (is_function ()) |
1
|
423 { |
195
|
424 symbol_def *old_def = pop_def (); |
|
425 maybe_delete (old_def); |
1
|
426 } |
|
427 |
530
|
428 unsigned fcn_type = text_fcn ? symbol_def::TEXT_FUNCTION |
|
429 : ((t && t->is_mapper_function ()) ? symbol_def::MAPPER_FUNCTION |
|
430 : symbol_def::UNKNOWN); |
|
431 |
|
432 symbol_def *new_def = new symbol_def (t, fcn_type); |
195
|
433 push_def (new_def); |
|
434 definition->count = 1; |
|
435 |
1
|
436 return 1; |
|
437 } |
|
438 |
|
439 int |
530
|
440 symbol_record::define (tree_function *t, int text_fcn) |
1
|
441 { |
195
|
442 if (read_only_error ()) |
1
|
443 return 0; |
|
444 |
195
|
445 if (is_variable ()) |
1
|
446 { |
195
|
447 symbol_def *old_def = pop_def (); |
|
448 maybe_delete (old_def); |
1
|
449 } |
|
450 |
195
|
451 if (is_function ()) |
1
|
452 { |
195
|
453 symbol_def *old_def = pop_def (); |
|
454 maybe_delete (old_def); |
1
|
455 } |
|
456 |
530
|
457 unsigned fcn_type = text_fcn ? symbol_def::TEXT_FUNCTION |
|
458 : symbol_def::UNKNOWN; |
|
459 |
|
460 symbol_def *new_def = new symbol_def (t, fcn_type); |
195
|
461 push_def (new_def); |
|
462 definition->count = 1; |
|
463 |
1
|
464 return 1; |
|
465 } |
|
466 |
|
467 int |
|
468 symbol_record::define_as_fcn (tree_constant *t) |
|
469 { |
195
|
470 if (is_variable () && read_only_error ()) |
1
|
471 return 0; |
|
472 |
195
|
473 if (is_variable ()) |
1
|
474 { |
195
|
475 symbol_def *old_def = pop_def (); |
|
476 maybe_delete (old_def); |
|
477 } |
|
478 |
|
479 if (is_function ()) |
|
480 { |
|
481 symbol_def *old_def = pop_def (); |
|
482 maybe_delete (old_def); |
1
|
483 } |
|
484 |
195
|
485 symbol_def *new_def = new symbol_def (t); |
|
486 push_def (new_def); |
|
487 definition->count = 1; |
|
488 definition->type = symbol_def::BUILTIN_FUNCTION; |
1
|
489 |
|
490 return 1; |
|
491 } |
|
492 |
195
|
493 int |
|
494 symbol_record::define_builtin_var (tree_constant *t) |
|
495 { |
|
496 define (t); |
|
497 if (is_variable ()) |
|
498 definition->type = symbol_def::BUILTIN_VARIABLE; |
530
|
499 return 1; |
195
|
500 } |
|
501 |
1
|
502 void |
1755
|
503 symbol_record::document (const string& h) |
1
|
504 { |
530
|
505 if (definition) |
195
|
506 { |
|
507 definition->document (h); |
1
|
508 |
195
|
509 if (! is_defined ()) |
1755
|
510 warning ("documenting undefined variable `%s'", nm.c_str ()); |
195
|
511 } |
1
|
512 } |
|
513 |
|
514 int |
195
|
515 symbol_record::clear (void) |
1
|
516 { |
195
|
517 int count = 0; |
|
518 if (linked_to_global) |
1
|
519 { |
195
|
520 count = maybe_delete (definition); |
530
|
521 definition = 0; |
195
|
522 linked_to_global = 0; |
1
|
523 } |
195
|
524 else |
|
525 { |
|
526 symbol_def *old_def = pop_def (); |
|
527 count = maybe_delete (old_def); |
|
528 } |
|
529 return count; |
1
|
530 } |
|
531 |
|
532 void |
530
|
533 symbol_record::alias (symbol_record *s, int force) |
1
|
534 { |
195
|
535 sv_fcn = s->sv_fcn; |
|
536 |
530
|
537 if (force && ! s->definition) |
1
|
538 { |
195
|
539 s->definition = new symbol_def (); |
|
540 definition = s->definition; |
|
541 definition->count = 2; // Yes, this is correct. |
1
|
542 } |
530
|
543 else if (s->definition) |
1
|
544 { |
195
|
545 definition = s->definition; |
|
546 definition->count++; |
1
|
547 } |
|
548 } |
|
549 |
|
550 void |
|
551 symbol_record::mark_as_formal_parameter (void) |
|
552 { |
|
553 formal_param = 1; |
|
554 } |
|
555 |
|
556 int |
164
|
557 symbol_record::is_formal_parameter (void) const |
1
|
558 { |
|
559 return formal_param; |
|
560 } |
|
561 |
|
562 void |
195
|
563 symbol_record::mark_as_linked_to_global (void) |
122
|
564 { |
195
|
565 linked_to_global = 1; |
122
|
566 } |
|
567 |
|
568 int |
195
|
569 symbol_record::is_linked_to_global (void) const |
1
|
570 { |
195
|
571 return linked_to_global; |
1
|
572 } |
|
573 |
|
574 symbol_record * |
164
|
575 symbol_record::next (void) const |
1
|
576 { |
|
577 return next_elem; |
|
578 } |
|
579 |
195
|
580 void |
|
581 symbol_record::chain (symbol_record *s) |
|
582 { |
|
583 next_elem = s; |
|
584 } |
|
585 |
220
|
586 void |
|
587 symbol_record::push_context (void) |
|
588 { |
|
589 context.push (definition); |
530
|
590 definition = 0; |
395
|
591 |
|
592 global_link_context.push ((unsigned) linked_to_global); |
|
593 linked_to_global = 0; |
220
|
594 } |
|
595 |
|
596 void |
|
597 symbol_record::pop_context (void) |
|
598 { |
1653
|
599 // It is possible for context to be empty if new symbols have been |
|
600 // inserted in the symbol table during recursive calls. This can |
|
601 // happen as a result of calls to eval() and feval(). |
220
|
602 |
1653
|
603 if (! context.empty ()) |
220
|
604 { |
1653
|
605 if (is_variable ()) |
|
606 { |
|
607 symbol_def *old_def = pop_def (); |
|
608 maybe_delete (old_def); |
|
609 } |
|
610 |
|
611 if (is_function ()) |
|
612 { |
|
613 symbol_def *old_def = pop_def (); |
|
614 maybe_delete (old_def); |
|
615 } |
|
616 |
|
617 definition = context.pop (); |
|
618 linked_to_global = global_link_context.pop (); |
220
|
619 } |
|
620 } |
|
621 |
195
|
622 int |
|
623 symbol_record::read_only_error (void) |
|
624 { |
|
625 if (is_read_only ()) |
|
626 { |
|
627 if (is_variable ()) |
1045
|
628 { |
|
629 if (user_pref.read_only_constants) |
|
630 { |
|
631 if (user_pref.read_only_constants < 0) |
|
632 { |
1755
|
633 ::warning ("redefinition of constant `%s'", |
|
634 nm.c_str ()); |
1045
|
635 return 0; |
|
636 } |
|
637 else |
1755
|
638 ::error ("can't redefine read-only constant `%s'", |
|
639 nm.c_str ()); |
1045
|
640 } |
|
641 } |
195
|
642 else if (is_function ()) |
1045
|
643 { |
1755
|
644 ::error ("can't redefine read-only function `%s'", nm.c_str ()); |
1045
|
645 } |
|
646 else |
|
647 { |
1755
|
648 ::error ("can't redefine read-only symbol `%s'", nm.c_str ()); |
1045
|
649 } |
195
|
650 |
|
651 return 1; |
|
652 } |
|
653 else |
|
654 return 0; |
|
655 } |
|
656 |
|
657 void |
|
658 symbol_record::push_def (symbol_def *sd) |
|
659 { |
530
|
660 if (! sd) |
195
|
661 return; |
|
662 |
|
663 sd->next_elem = definition; |
|
664 definition = sd; |
|
665 } |
|
666 |
|
667 symbol_def * |
|
668 symbol_record::pop_def (void) |
|
669 { |
|
670 symbol_def *top = definition; |
530
|
671 if (definition) |
195
|
672 definition = definition->next_elem; |
|
673 return top; |
|
674 } |
|
675 |
767
|
676 // A structure for handling verbose information about a symbol_record. |
195
|
677 |
|
678 symbol_record_info::symbol_record_info (void) |
|
679 { |
|
680 init_state (); |
|
681 } |
|
682 |
|
683 symbol_record_info::symbol_record_info (const symbol_record& sr) |
|
684 { |
|
685 init_state (); |
|
686 |
|
687 type = sr.type (); |
|
688 |
|
689 if (sr.is_variable () && sr.is_defined ()) |
|
690 { |
1358
|
691 // Would be nice to avoid this cast. XXX FIXME XXX |
|
692 |
195
|
693 tree_constant *tmp = (tree_constant *) sr.def (); |
620
|
694 if (tmp->is_real_scalar ()) |
|
695 const_type = SR_INFO_SCALAR; |
|
696 else if (tmp->is_complex_scalar ()) |
|
697 const_type = SR_INFO_COMPLEX_SCALAR; |
|
698 else if (tmp->is_real_matrix ()) |
|
699 const_type = SR_INFO_MATRIX; |
|
700 else if (tmp->is_complex_matrix ()) |
|
701 const_type = SR_INFO_COMPLEX_MATRIX; |
|
702 else if (tmp->is_range ()) |
|
703 const_type = SR_INFO_RANGE; |
|
704 else if (tmp->is_string ()) |
|
705 const_type = SR_INFO_STRING; |
195
|
706 |
|
707 nr = tmp->rows (); |
|
708 nc = tmp->columns (); |
|
709 |
|
710 symbol_def *sr_def = sr.definition; |
|
711 symbol_def *hidden_def = sr_def->next_elem; |
530
|
712 if (hidden_def) |
195
|
713 { |
|
714 if (hidden_def->is_user_function ()) |
|
715 hides = SR_INFO_USER_FUNCTION; |
|
716 else if (hidden_def->is_builtin_function ()) |
|
717 hides = SR_INFO_BUILTIN_FUNCTION; |
|
718 } |
|
719 } |
|
720 |
|
721 eternal = sr.is_eternal (); |
|
722 read_only = sr.is_read_only (); |
|
723 |
1755
|
724 nm = sr.name (); |
195
|
725 |
|
726 initialized = 1; |
|
727 } |
|
728 |
|
729 symbol_record_info::symbol_record_info (const symbol_record_info& s) |
|
730 { |
|
731 type = s.type; |
|
732 const_type = s.const_type; |
|
733 hides = s.hides; |
|
734 eternal = s.eternal; |
|
735 read_only = s.read_only; |
|
736 nr = s.nr; |
|
737 nc = s.nc; |
1755
|
738 nm = s.nm; |
195
|
739 initialized = s.initialized; |
|
740 } |
|
741 |
|
742 symbol_record_info& |
|
743 symbol_record_info::operator = (const symbol_record_info& s) |
|
744 { |
|
745 if (this != &s) |
|
746 { |
|
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; |
1755
|
754 nm = s.nm; |
195
|
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 |
1755
|
790 string |
195
|
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 |
1755
|
835 string |
195
|
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; |
|
852 } |
|
853 |
767
|
854 // A symbol table. |
1
|
855 |
|
856 symbol_table::symbol_table (void) |
|
857 { |
|
858 } |
|
859 |
|
860 symbol_record * |
1755
|
861 symbol_table::lookup (const string& nm, int insert, int warn) |
1
|
862 { |
|
863 int index = hash (nm) & HASH_MASK; |
|
864 |
|
865 symbol_record *ptr = table[index].next (); |
|
866 |
530
|
867 while (ptr) |
1
|
868 { |
1755
|
869 if (ptr->name () == nm) |
1
|
870 return ptr; |
|
871 ptr = ptr->next (); |
|
872 } |
|
873 |
|
874 if (insert) |
|
875 { |
|
876 symbol_record *new_sym; |
|
877 new_sym = new symbol_record (nm, table[index].next ()); |
195
|
878 table[index].chain (new_sym); |
1
|
879 return new_sym; |
|
880 } |
|
881 else if (warn) |
1755
|
882 warning ("lookup: symbol`%s' not found", nm.c_str ()); |
1
|
883 |
530
|
884 return 0; |
1
|
885 } |
|
886 |
|
887 void |
1755
|
888 symbol_table::rename (const string& old_name, const string& new_name) |
572
|
889 { |
|
890 int index = hash (old_name) & HASH_MASK; |
|
891 |
|
892 symbol_record *prev = &table[index]; |
|
893 symbol_record *ptr = prev->next (); |
|
894 |
|
895 while (ptr) |
|
896 { |
1755
|
897 if (ptr->name () == old_name) |
572
|
898 { |
|
899 prev->chain (ptr->next ()); |
|
900 |
|
901 index = hash (new_name) & HASH_MASK; |
|
902 table[index].chain (ptr); |
|
903 ptr->rename (new_name); |
|
904 |
|
905 return; |
|
906 } |
|
907 |
|
908 prev = ptr; |
|
909 ptr = ptr->next (); |
|
910 } |
|
911 |
1755
|
912 error ("unable to rename `%s' to `%s'", old_name.c_str (), |
|
913 new_name.c_str ()); |
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 |
1755
|
937 symbol_table::clear (const string& 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 { |
1755
|
945 if (ptr->name () == nm |
195
|
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 { |
1755
|
983 return (a->name () == b->name ()); |
1
|
984 } |
|
985 |
867
|
986 static int |
1755
|
987 matches_patterns (const string& name, const string_vector& pats, int npats) |
867
|
988 { |
1755
|
989 for (int i = 0; i < npats; i++) |
867
|
990 { |
1792
|
991 glob_match pattern (pats[i]); |
|
992 if (pattern.match (name)) |
867
|
993 return 1; |
|
994 } |
|
995 |
|
996 return 0; |
|
997 } |
|
998 |
195
|
999 // This function should probably share code with symbol_table::list. |
|
1000 // XXX FIXME XXX |
1
|
1001 |
195
|
1002 symbol_record_info * |
1755
|
1003 symbol_table::long_list (int& count, const string_vector& pats, |
|
1004 int npats, int sort, unsigned type, |
|
1005 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 |
1755
|
1024 string my_name = ptr->name (); |
867
|
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 |
1755
|
1042 string_vector |
|
1043 symbol_table::list (int& count, const string_vector& pats, int npats, |
|
1044 int sort, 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 |
1755
|
1051 string_vector symbols (n); |
|
1052 |
1
|
1053 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1054 { |
|
1055 symbol_record *ptr = table[i].next (); |
530
|
1056 while (ptr) |
1
|
1057 { |
|
1058 assert (count < n); |
605
|
1059 |
195
|
1060 unsigned my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
1061 |
195
|
1062 unsigned my_type = ptr->type (); |
605
|
1063 |
1755
|
1064 string my_name = ptr->name (); |
867
|
1065 |
|
1066 if ((type & my_type) && (scope & my_scope) |
|
1067 && (npats == 0 || matches_patterns (my_name, pats, npats))) |
1755
|
1068 symbols[count++] = ptr->name (); |
605
|
1069 |
1
|
1070 ptr = ptr->next (); |
|
1071 } |
|
1072 } |
1755
|
1073 |
|
1074 symbols.resize (count); |
1
|
1075 |
1755
|
1076 if (sort && ! symbols.empty ()) |
|
1077 symbols.qsort (); |
1
|
1078 |
|
1079 return symbols; |
|
1080 } |
|
1081 |
605
|
1082 symbol_record ** |
1755
|
1083 symbol_table::glob (int& count, const string& pat, unsigned type, |
605
|
1084 unsigned scope) const |
|
1085 { |
|
1086 count = 0; |
|
1087 int n = size (); |
|
1088 if (n == 0) |
|
1089 return 0; |
|
1090 |
|
1091 symbol_record **symbols = new symbol_record * [n+1]; |
|
1092 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1093 { |
|
1094 symbol_record *ptr = table[i].next (); |
|
1095 while (ptr) |
|
1096 { |
|
1097 assert (count < n); |
|
1098 |
|
1099 unsigned my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
|
1100 |
|
1101 unsigned my_type = ptr->type (); |
|
1102 |
1792
|
1103 glob_match pattern (pat); |
1755
|
1104 |
605
|
1105 if ((type & my_type) && (scope & my_scope) |
1792
|
1106 && pattern.match (ptr->name ())) |
605
|
1107 { |
|
1108 symbols[count++] = ptr; |
|
1109 } |
|
1110 |
|
1111 ptr = ptr->next (); |
|
1112 } |
|
1113 } |
|
1114 symbols[count] = 0; |
|
1115 |
|
1116 return symbols; |
|
1117 } |
|
1118 |
220
|
1119 void |
|
1120 symbol_table::push_context (void) |
|
1121 { |
|
1122 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1123 { |
|
1124 symbol_record *ptr = table[i].next (); |
|
1125 |
530
|
1126 while (ptr) |
220
|
1127 { |
|
1128 ptr->push_context (); |
|
1129 ptr = ptr->next (); |
|
1130 } |
|
1131 } |
|
1132 } |
|
1133 |
|
1134 void |
|
1135 symbol_table::pop_context (void) |
|
1136 { |
|
1137 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1138 { |
|
1139 symbol_record *ptr = table[i].next (); |
|
1140 |
530
|
1141 while (ptr) |
220
|
1142 { |
|
1143 ptr->pop_context (); |
|
1144 ptr = ptr->next (); |
|
1145 } |
|
1146 } |
|
1147 } |
|
1148 |
1
|
1149 // Chris Torek's fave hash function. |
|
1150 |
|
1151 unsigned int |
1755
|
1152 symbol_table::hash (const string& str) |
1
|
1153 { |
|
1154 unsigned h = 0; |
1755
|
1155 for (unsigned i = 0; i < str.length (); i++) |
|
1156 h = h * 33 + str[i]; |
1
|
1157 return h; |
|
1158 } |
|
1159 |
1962
|
1160 // Return nonzero if S is a valid identifier. |
|
1161 |
|
1162 int |
|
1163 valid_identifier (const char *s) |
|
1164 { |
|
1165 if (! s || ! (isalnum (*s) || *s == '_')) |
|
1166 return 0; |
|
1167 |
|
1168 while (*++s != '\0') |
|
1169 if (! (isalnum (*s) || *s == '_')) |
|
1170 return 0; |
|
1171 |
|
1172 return 1; |
|
1173 } |
|
1174 |
1
|
1175 /* |
|
1176 ;;; Local Variables: *** |
|
1177 ;;; mode: C++ *** |
|
1178 ;;; End: *** |
|
1179 */ |