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