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