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 if (! definition) |
|
360 { |
|
361 definition = new symbol_def (); |
|
362 definition->count = 1; |
|
363 } |
|
364 else if (is_function ()) |
|
365 { |
|
366 push_def (new symbol_def ()); |
|
367 definition->count = 1; |
|
368 } |
|
369 |
2949
|
370 if (definition->symbol_type () == symbol_def::BUILTIN_VARIABLE) |
|
371 sym_type = symbol_def::BUILTIN_VARIABLE; |
2893
|
372 |
|
373 definition->define (new octave_value (v), sym_type); |
1
|
374 } |
|
375 |
2893
|
376 return retval; |
2390
|
377 } |
|
378 |
|
379 int |
2893
|
380 symbol_record::define_builtin_var (const octave_value& v) |
1
|
381 { |
2949
|
382 int retval = define (v, symbol_def::BUILTIN_VARIABLE); |
|
383 |
|
384 if (sv_fcn) |
|
385 sv_fcn (); |
|
386 |
|
387 return retval; |
1
|
388 } |
|
389 |
|
390 int |
2390
|
391 symbol_record::define_as_fcn (const octave_value& v) |
1
|
392 { |
2791
|
393 if (is_variable () && read_only_error ("redefine")) |
1
|
394 return 0; |
|
395 |
195
|
396 if (is_variable ()) |
1
|
397 { |
195
|
398 symbol_def *old_def = pop_def (); |
|
399 maybe_delete (old_def); |
|
400 } |
|
401 |
|
402 if (is_function ()) |
|
403 { |
|
404 symbol_def *old_def = pop_def (); |
|
405 maybe_delete (old_def); |
1
|
406 } |
|
407 |
2893
|
408 push_def (new symbol_def (new octave_value (v), |
|
409 symbol_def::BUILTIN_FUNCTION)); |
|
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 |
|
434 push_def (new symbol_def (f, sym_type)); |
|
435 |
|
436 definition->count = 1; |
|
437 |
530
|
438 return 1; |
195
|
439 } |
|
440 |
1
|
441 void |
1755
|
442 symbol_record::document (const string& h) |
1
|
443 { |
530
|
444 if (definition) |
195
|
445 { |
|
446 definition->document (h); |
1
|
447 |
195
|
448 if (! is_defined ()) |
1755
|
449 warning ("documenting undefined variable `%s'", nm.c_str ()); |
195
|
450 } |
1
|
451 } |
|
452 |
|
453 int |
195
|
454 symbol_record::clear (void) |
1
|
455 { |
195
|
456 int count = 0; |
|
457 if (linked_to_global) |
1
|
458 { |
195
|
459 count = maybe_delete (definition); |
530
|
460 definition = 0; |
195
|
461 linked_to_global = 0; |
1
|
462 } |
2846
|
463 else if (! tagged_static) |
195
|
464 { |
|
465 symbol_def *old_def = pop_def (); |
|
466 count = maybe_delete (old_def); |
|
467 } |
|
468 return count; |
1
|
469 } |
|
470 |
|
471 void |
2856
|
472 symbol_record::alias (symbol_record *s, bool force) |
1
|
473 { |
195
|
474 sv_fcn = s->sv_fcn; |
|
475 |
530
|
476 if (force && ! s->definition) |
1
|
477 { |
195
|
478 s->definition = new symbol_def (); |
|
479 definition = s->definition; |
|
480 definition->count = 2; // Yes, this is correct. |
1
|
481 } |
530
|
482 else if (s->definition) |
1
|
483 { |
195
|
484 definition = s->definition; |
|
485 definition->count++; |
1
|
486 } |
|
487 } |
|
488 |
|
489 void |
|
490 symbol_record::mark_as_formal_parameter (void) |
|
491 { |
|
492 formal_param = 1; |
|
493 } |
|
494 |
2856
|
495 bool |
164
|
496 symbol_record::is_formal_parameter (void) const |
1
|
497 { |
|
498 return formal_param; |
|
499 } |
|
500 |
|
501 void |
195
|
502 symbol_record::mark_as_linked_to_global (void) |
122
|
503 { |
195
|
504 linked_to_global = 1; |
122
|
505 } |
|
506 |
2856
|
507 bool |
195
|
508 symbol_record::is_linked_to_global (void) const |
1
|
509 { |
195
|
510 return linked_to_global; |
1
|
511 } |
|
512 |
2846
|
513 void |
|
514 symbol_record::mark_as_static (void) |
|
515 { |
|
516 if (is_linked_to_global ()) |
|
517 error ("can't make global variable static"); |
|
518 else if (is_formal_parameter ()) |
|
519 error ("can't make formal parameter static"); |
|
520 else |
|
521 tagged_static = 1; |
|
522 } |
|
523 |
2856
|
524 bool |
2846
|
525 symbol_record::is_static (void) const |
|
526 { |
|
527 return tagged_static; |
|
528 } |
|
529 |
2390
|
530 octave_value |
|
531 symbol_record::variable_value (void) const |
|
532 { |
|
533 octave_value retval; |
|
534 |
|
535 if (is_variable ()) |
|
536 { |
2893
|
537 octave_symbol *tmp = def (); |
|
538 retval = tmp->eval (); |
2390
|
539 } |
|
540 |
|
541 return retval; |
|
542 } |
|
543 |
2949
|
544 octave_variable_reference |
2390
|
545 symbol_record::variable_reference (void) |
|
546 { |
|
547 if (is_function ()) |
|
548 clear (); |
|
549 |
|
550 if (! is_defined ()) |
|
551 { |
|
552 if (! (is_formal_parameter () || is_linked_to_global ())) |
|
553 link_to_builtin_variable (this); |
|
554 |
|
555 if (! is_defined ()) |
2893
|
556 define (octave_value ()); |
2390
|
557 } |
|
558 |
2949
|
559 return octave_variable_reference |
|
560 (static_cast<octave_value *> (def ()), sv_fcn); |
2390
|
561 } |
|
562 |
1
|
563 symbol_record * |
164
|
564 symbol_record::next (void) const |
1
|
565 { |
|
566 return next_elem; |
|
567 } |
|
568 |
195
|
569 void |
|
570 symbol_record::chain (symbol_record *s) |
|
571 { |
|
572 next_elem = s; |
|
573 } |
|
574 |
220
|
575 void |
|
576 symbol_record::push_context (void) |
|
577 { |
2846
|
578 if (! is_static ()) |
|
579 { |
|
580 context.push (definition); |
|
581 definition = 0; |
395
|
582 |
2893
|
583 global_link_context.push (static_cast<unsigned int> (linked_to_global)); |
2846
|
584 linked_to_global = 0; |
|
585 } |
220
|
586 } |
|
587 |
|
588 void |
|
589 symbol_record::pop_context (void) |
|
590 { |
1653
|
591 // It is possible for context to be empty if new symbols have been |
|
592 // inserted in the symbol table during recursive calls. This can |
|
593 // happen as a result of calls to eval() and feval(). |
220
|
594 |
1653
|
595 if (! context.empty ()) |
220
|
596 { |
1653
|
597 if (is_variable ()) |
|
598 { |
|
599 symbol_def *old_def = pop_def (); |
|
600 maybe_delete (old_def); |
|
601 } |
|
602 |
|
603 if (is_function ()) |
|
604 { |
|
605 symbol_def *old_def = pop_def (); |
|
606 maybe_delete (old_def); |
|
607 } |
|
608 |
|
609 definition = context.pop (); |
|
610 linked_to_global = global_link_context.pop (); |
220
|
611 } |
|
612 } |
|
613 |
195
|
614 int |
2791
|
615 symbol_record::read_only_error (const char *action) |
195
|
616 { |
|
617 if (is_read_only ()) |
|
618 { |
|
619 if (is_variable ()) |
1045
|
620 { |
2791
|
621 ::error ("can't %s read-only constant `%s'", action, nm.c_str ()); |
1045
|
622 } |
195
|
623 else if (is_function ()) |
1045
|
624 { |
2791
|
625 ::error ("can't %s read-only function `%s'", action, nm.c_str ()); |
1045
|
626 } |
|
627 else |
|
628 { |
2791
|
629 ::error ("can't %s read-only symbol `%s'", action, nm.c_str ()); |
1045
|
630 } |
195
|
631 |
|
632 return 1; |
|
633 } |
|
634 else |
|
635 return 0; |
|
636 } |
|
637 |
|
638 void |
|
639 symbol_record::push_def (symbol_def *sd) |
|
640 { |
530
|
641 if (! sd) |
195
|
642 return; |
|
643 |
|
644 sd->next_elem = definition; |
|
645 definition = sd; |
|
646 } |
|
647 |
|
648 symbol_def * |
|
649 symbol_record::pop_def (void) |
|
650 { |
|
651 symbol_def *top = definition; |
530
|
652 if (definition) |
195
|
653 definition = definition->next_elem; |
|
654 return top; |
|
655 } |
|
656 |
767
|
657 // A structure for handling verbose information about a symbol_record. |
195
|
658 |
|
659 symbol_record_info::symbol_record_info (void) |
2404
|
660 : initialized (0), nr (-1), nc (-1), type (symbol_def::UNKNOWN), |
|
661 hides (SR_INFO_NONE), eternal (0), read_only (0), nm (), |
|
662 const_type () { } |
195
|
663 |
|
664 symbol_record_info::symbol_record_info (const symbol_record& sr) |
2404
|
665 : initialized (0), nr (-1), nc (-1), type (sr.type ()), |
|
666 hides (SR_INFO_NONE), eternal (0), read_only (0), nm (), |
|
667 const_type () |
195
|
668 { |
|
669 if (sr.is_variable () && sr.is_defined ()) |
|
670 { |
1358
|
671 // Would be nice to avoid this cast. XXX FIXME XXX |
|
672 |
2893
|
673 octave_symbol *tmp = sr.def (); |
2404
|
674 |
2893
|
675 octave_value vtmp = tmp->eval (); |
195
|
676 |
2878
|
677 const_type = vtmp.type_name (); |
|
678 |
|
679 nr = vtmp.rows (); |
|
680 nc = vtmp.columns (); |
195
|
681 |
|
682 symbol_def *sr_def = sr.definition; |
|
683 symbol_def *hidden_def = sr_def->next_elem; |
2404
|
684 |
530
|
685 if (hidden_def) |
195
|
686 { |
|
687 if (hidden_def->is_user_function ()) |
|
688 hides = SR_INFO_USER_FUNCTION; |
|
689 else if (hidden_def->is_builtin_function ()) |
|
690 hides = SR_INFO_BUILTIN_FUNCTION; |
|
691 } |
|
692 } |
|
693 |
|
694 eternal = sr.is_eternal (); |
|
695 read_only = sr.is_read_only (); |
|
696 |
1755
|
697 nm = sr.name (); |
195
|
698 |
|
699 initialized = 1; |
|
700 } |
|
701 |
|
702 symbol_record_info::symbol_record_info (const symbol_record_info& s) |
2404
|
703 : initialized (s.initialized), nr (s.nr), nc (s.nc), type (s.type), |
|
704 hides (s.hides), eternal (s.eternal), read_only (s.read_only), |
|
705 nm (s.nm), const_type (s.const_type) { } |
195
|
706 |
|
707 symbol_record_info& |
|
708 symbol_record_info::operator = (const symbol_record_info& s) |
|
709 { |
|
710 if (this != &s) |
|
711 { |
2404
|
712 initialized = s.initialized; |
|
713 nr = s.nr; |
|
714 nc = s.nc; |
195
|
715 type = s.type; |
|
716 hides = s.hides; |
|
717 eternal = s.eternal; |
|
718 read_only = s.read_only; |
1755
|
719 nm = s.nm; |
2404
|
720 const_type = s.const_type; |
195
|
721 } |
|
722 return *this; |
|
723 } |
|
724 |
2856
|
725 bool |
195
|
726 symbol_record_info::is_defined (void) const |
|
727 { |
|
728 return initialized; |
|
729 } |
|
730 |
2856
|
731 bool |
195
|
732 symbol_record_info::is_read_only (void) const |
|
733 { |
|
734 return read_only; |
|
735 } |
|
736 |
2856
|
737 bool |
195
|
738 symbol_record_info::is_eternal (void) const |
|
739 { |
|
740 return eternal; |
|
741 } |
|
742 |
2856
|
743 bool |
195
|
744 symbol_record_info::hides_fcn (void) const |
|
745 { |
|
746 return (hides & SR_INFO_USER_FUNCTION); |
|
747 } |
|
748 |
2856
|
749 bool |
195
|
750 symbol_record_info::hides_builtin (void) const |
|
751 { |
|
752 return (hides & SR_INFO_BUILTIN_FUNCTION); |
|
753 } |
|
754 |
1755
|
755 string |
2390
|
756 symbol_record_info::type_name (void) const |
195
|
757 { |
2404
|
758 string retval; |
|
759 |
195
|
760 if (type == symbol_def::USER_FUNCTION) |
2404
|
761 retval = "user function"; |
|
762 else if (type & symbol_def::BUILTIN_FUNCTION) |
195
|
763 { |
2404
|
764 if (type & symbol_def::TEXT_FUNCTION) |
|
765 retval = "text function"; |
|
766 else if (type & symbol_def::MAPPER_FUNCTION) |
|
767 retval = "mapper function"; |
195
|
768 else |
2404
|
769 retval = "builtin function"; |
195
|
770 } |
2404
|
771 else |
|
772 retval = const_type; |
|
773 |
|
774 return retval; |
195
|
775 } |
|
776 |
2856
|
777 bool |
195
|
778 symbol_record_info::is_function (void) const |
|
779 { |
|
780 return (type == symbol_def::USER_FUNCTION |
2404
|
781 || type == symbol_def::BUILTIN_FUNCTION |
|
782 || symbol_def::TEXT_FUNCTION |
|
783 || symbol_def::MAPPER_FUNCTION); |
195
|
784 } |
|
785 |
|
786 int |
|
787 symbol_record_info::rows (void) const |
|
788 { |
|
789 return nr; |
|
790 } |
|
791 |
|
792 int |
|
793 symbol_record_info::columns (void) const |
|
794 { |
|
795 return nc; |
|
796 } |
|
797 |
1755
|
798 string |
195
|
799 symbol_record_info::name (void) const |
|
800 { |
|
801 return nm; |
|
802 } |
|
803 |
767
|
804 // A symbol table. |
1
|
805 |
|
806 symbol_table::symbol_table (void) |
|
807 { |
|
808 } |
|
809 |
|
810 symbol_record * |
2856
|
811 symbol_table::lookup (const string& nm, bool insert, bool warn) |
1
|
812 { |
|
813 int index = hash (nm) & HASH_MASK; |
|
814 |
|
815 symbol_record *ptr = table[index].next (); |
|
816 |
530
|
817 while (ptr) |
1
|
818 { |
1755
|
819 if (ptr->name () == nm) |
1
|
820 return ptr; |
|
821 ptr = ptr->next (); |
|
822 } |
|
823 |
|
824 if (insert) |
|
825 { |
|
826 symbol_record *new_sym; |
|
827 new_sym = new symbol_record (nm, table[index].next ()); |
195
|
828 table[index].chain (new_sym); |
1
|
829 return new_sym; |
|
830 } |
|
831 else if (warn) |
1755
|
832 warning ("lookup: symbol`%s' not found", nm.c_str ()); |
1
|
833 |
530
|
834 return 0; |
1
|
835 } |
|
836 |
|
837 void |
1755
|
838 symbol_table::rename (const string& old_name, const string& new_name) |
572
|
839 { |
|
840 int index = hash (old_name) & HASH_MASK; |
|
841 |
|
842 symbol_record *prev = &table[index]; |
|
843 symbol_record *ptr = prev->next (); |
|
844 |
|
845 while (ptr) |
|
846 { |
1755
|
847 if (ptr->name () == old_name) |
572
|
848 { |
|
849 ptr->rename (new_name); |
|
850 |
2791
|
851 if (! error_state) |
|
852 { |
|
853 prev->chain (ptr->next ()); |
|
854 |
|
855 index = hash (new_name) & HASH_MASK; |
|
856 table[index].chain (ptr); |
|
857 |
|
858 return; |
|
859 } |
|
860 |
|
861 break; |
572
|
862 } |
|
863 |
|
864 prev = ptr; |
|
865 ptr = ptr->next (); |
|
866 } |
|
867 |
1755
|
868 error ("unable to rename `%s' to `%s'", old_name.c_str (), |
|
869 new_name.c_str ()); |
572
|
870 } |
|
871 |
|
872 void |
2856
|
873 symbol_table::clear (bool clear_user_functions) |
1
|
874 { |
|
875 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
876 { |
169
|
877 symbol_record *ptr = table[i].next (); |
1
|
878 |
530
|
879 while (ptr) |
169
|
880 { |
195
|
881 if (ptr->is_user_variable () |
|
882 || (clear_user_functions && ptr->is_user_function ())) |
|
883 { |
|
884 ptr->clear (); |
|
885 } |
|
886 |
169
|
887 ptr = ptr->next (); |
1
|
888 } |
|
889 } |
|
890 } |
|
891 |
|
892 int |
2856
|
893 symbol_table::clear (const string& nm, bool clear_user_functions) |
1
|
894 { |
|
895 int index = hash (nm) & HASH_MASK; |
|
896 |
195
|
897 symbol_record *ptr = table[index].next (); |
1
|
898 |
530
|
899 while (ptr) |
1
|
900 { |
1755
|
901 if (ptr->name () == nm |
195
|
902 && (ptr->is_user_variable () |
|
903 || (clear_user_functions && ptr->is_user_function ()))) |
1
|
904 { |
195
|
905 ptr->clear (); |
|
906 return 1; |
1
|
907 } |
195
|
908 ptr = ptr->next (); |
1
|
909 } |
|
910 |
|
911 return 0; |
|
912 } |
|
913 |
|
914 int |
164
|
915 symbol_table::size (void) const |
1
|
916 { |
|
917 int count = 0; |
|
918 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
919 { |
|
920 symbol_record *ptr = table[i].next (); |
530
|
921 while (ptr) |
1
|
922 { |
|
923 count++; |
|
924 ptr = ptr->next (); |
|
925 } |
|
926 } |
|
927 return count; |
|
928 } |
|
929 |
195
|
930 static inline int |
|
931 pstrcmp (char **a, char **b) |
1
|
932 { |
195
|
933 return strcmp (*a, *b); |
1
|
934 } |
|
935 |
195
|
936 static inline int |
|
937 symbol_record_info_cmp (symbol_record_info *a, symbol_record_info *b) |
1
|
938 { |
1755
|
939 return (a->name () == b->name ()); |
1
|
940 } |
|
941 |
867
|
942 static int |
1755
|
943 matches_patterns (const string& name, const string_vector& pats, int npats) |
867
|
944 { |
1755
|
945 for (int i = 0; i < npats; i++) |
867
|
946 { |
1792
|
947 glob_match pattern (pats[i]); |
|
948 if (pattern.match (name)) |
867
|
949 return 1; |
|
950 } |
|
951 |
|
952 return 0; |
|
953 } |
|
954 |
195
|
955 // This function should probably share code with symbol_table::list. |
|
956 // XXX FIXME XXX |
1
|
957 |
195
|
958 symbol_record_info * |
1755
|
959 symbol_table::long_list (int& count, const string_vector& pats, |
2893
|
960 int npats, bool sort, unsigned int type, |
|
961 unsigned int scope) const |
1
|
962 { |
115
|
963 count = 0; |
1
|
964 int n = size (); |
|
965 if (n == 0) |
530
|
966 return 0; |
1
|
967 |
195
|
968 symbol_record_info *symbols = new symbol_record_info [n+1]; |
1
|
969 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
970 { |
|
971 symbol_record *ptr = table[i].next (); |
530
|
972 while (ptr) |
1
|
973 { |
|
974 assert (count < n); |
867
|
975 |
2893
|
976 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
977 |
2893
|
978 unsigned int my_type = ptr->type (); |
867
|
979 |
1755
|
980 string my_name = ptr->name (); |
867
|
981 |
|
982 if ((type & my_type) && (scope & my_scope) |
|
983 && (npats == 0 || matches_patterns (my_name, pats, npats))) |
|
984 symbols[count++] = symbol_record_info (*ptr); |
|
985 |
1
|
986 ptr = ptr->next (); |
|
987 } |
|
988 } |
195
|
989 symbols[count] = symbol_record_info (); |
|
990 |
530
|
991 if (sort && symbols) |
2800
|
992 qsort (symbols, count, sizeof (symbol_record_info), |
|
993 symbol_record_info_cmp); |
195
|
994 |
1
|
995 return symbols; |
|
996 } |
|
997 |
1755
|
998 string_vector |
|
999 symbol_table::list (int& count, const string_vector& pats, int npats, |
2893
|
1000 bool sort, unsigned int type, unsigned int scope) const |
1
|
1001 { |
115
|
1002 count = 0; |
1
|
1003 int n = size (); |
|
1004 if (n == 0) |
530
|
1005 return 0; |
1
|
1006 |
1755
|
1007 string_vector symbols (n); |
|
1008 |
1
|
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 assert (count < n); |
605
|
1015 |
2893
|
1016 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
1017 |
2893
|
1018 unsigned int my_type = ptr->type (); |
605
|
1019 |
1755
|
1020 string my_name = ptr->name (); |
867
|
1021 |
|
1022 if ((type & my_type) && (scope & my_scope) |
|
1023 && (npats == 0 || matches_patterns (my_name, pats, npats))) |
1755
|
1024 symbols[count++] = ptr->name (); |
605
|
1025 |
1
|
1026 ptr = ptr->next (); |
|
1027 } |
|
1028 } |
1755
|
1029 |
|
1030 symbols.resize (count); |
1
|
1031 |
1755
|
1032 if (sort && ! symbols.empty ()) |
|
1033 symbols.qsort (); |
1
|
1034 |
|
1035 return symbols; |
|
1036 } |
|
1037 |
605
|
1038 symbol_record ** |
2893
|
1039 symbol_table::glob (int& count, const string& pat, unsigned int type, |
|
1040 unsigned int scope) const |
605
|
1041 { |
|
1042 count = 0; |
|
1043 int n = size (); |
|
1044 if (n == 0) |
|
1045 return 0; |
|
1046 |
|
1047 symbol_record **symbols = new symbol_record * [n+1]; |
|
1048 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1049 { |
|
1050 symbol_record *ptr = table[i].next (); |
|
1051 while (ptr) |
|
1052 { |
|
1053 assert (count < n); |
|
1054 |
2893
|
1055 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
1056 |
2893
|
1057 unsigned int my_type = ptr->type (); |
605
|
1058 |
1792
|
1059 glob_match pattern (pat); |
1755
|
1060 |
605
|
1061 if ((type & my_type) && (scope & my_scope) |
1792
|
1062 && pattern.match (ptr->name ())) |
605
|
1063 { |
|
1064 symbols[count++] = ptr; |
|
1065 } |
|
1066 |
|
1067 ptr = ptr->next (); |
|
1068 } |
|
1069 } |
|
1070 symbols[count] = 0; |
|
1071 |
|
1072 return symbols; |
|
1073 } |
|
1074 |
220
|
1075 void |
|
1076 symbol_table::push_context (void) |
|
1077 { |
|
1078 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
1079 { |
|
1080 symbol_record *ptr = table[i].next (); |
|
1081 |
530
|
1082 while (ptr) |
220
|
1083 { |
|
1084 ptr->push_context (); |
|
1085 ptr = ptr->next (); |
|
1086 } |
|
1087 } |
|
1088 } |
|
1089 |
|
1090 void |
|
1091 symbol_table::pop_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->pop_context (); |
|
1100 ptr = ptr->next (); |
|
1101 } |
|
1102 } |
|
1103 } |
|
1104 |
1
|
1105 // Chris Torek's fave hash function. |
|
1106 |
|
1107 unsigned int |
1755
|
1108 symbol_table::hash (const string& str) |
1
|
1109 { |
2893
|
1110 unsigned int h = 0; |
|
1111 for (unsigned int i = 0; i < str.length (); i++) |
1755
|
1112 h = h * 33 + str[i]; |
1
|
1113 return h; |
|
1114 } |
|
1115 |
1962
|
1116 // Return nonzero if S is a valid identifier. |
|
1117 |
2856
|
1118 bool |
1962
|
1119 valid_identifier (const char *s) |
|
1120 { |
|
1121 if (! s || ! (isalnum (*s) || *s == '_')) |
2856
|
1122 return false; |
1962
|
1123 |
|
1124 while (*++s != '\0') |
|
1125 if (! (isalnum (*s) || *s == '_')) |
2856
|
1126 return false; |
1962
|
1127 |
2856
|
1128 return true; |
1962
|
1129 } |
|
1130 |
2856
|
1131 bool |
2790
|
1132 valid_identifier (const string& s) |
|
1133 { |
|
1134 return valid_identifier (s.c_str ()); |
|
1135 } |
|
1136 |
1
|
1137 /* |
|
1138 ;;; Local Variables: *** |
|
1139 ;;; mode: C++ *** |
|
1140 ;;; End: *** |
|
1141 */ |