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