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