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