8
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, |
|
4 2004, 2005, 2006, 2007 John W. Eaton |
7336
|
5 |
8
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
8
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
8
|
21 |
|
22 */ |
|
23 |
383
|
24 #if !defined (octave_symtab_h) |
|
25 #define octave_symtab_h 1 |
8
|
26 |
7336
|
27 #include <deque> |
|
28 #include <list> |
|
29 #include <map> |
|
30 #include <set> |
4214
|
31 #include <stack> |
7336
|
32 #include <string> |
2953
|
33 |
7336
|
34 #include "glob-match.h" |
2846
|
35 |
7336
|
36 class tree_argument_list; |
3013
|
37 |
7336
|
38 #include "oct-obj.h" |
|
39 #include "ov.h" |
1412
|
40 |
8
|
41 class |
6109
|
42 OCTINTERP_API |
8
|
43 symbol_table |
|
44 { |
|
45 public: |
|
46 |
7336
|
47 typedef int scope_id; |
|
48 |
|
49 class |
|
50 symbol_record |
|
51 { |
|
52 public: |
|
53 |
|
54 // generic variable |
|
55 static const unsigned int local = 1; |
|
56 |
|
57 // varargin, argn, .nargin., .nargout. |
|
58 // (FIXME -- is this really used now?) |
|
59 static const unsigned int automatic = 2; |
|
60 |
|
61 // formal parameter |
|
62 static const unsigned int formal = 4; |
|
63 |
|
64 // not listed or cleared (.nargin., .nargout.) |
|
65 static const unsigned int hidden = 8; |
|
66 |
|
67 // inherited from parent scope; not cleared at function exit |
|
68 static const unsigned int inherited = 16; |
|
69 |
|
70 // global (redirects to global scope) |
|
71 static const unsigned int global = 32; |
|
72 |
|
73 // not cleared at function exit |
|
74 static const unsigned int persistent = 64; |
|
75 |
|
76 private: |
|
77 |
|
78 class |
|
79 symbol_record_rep |
4009
|
80 { |
7336
|
81 public: |
|
82 |
|
83 symbol_record_rep (const std::string& nm, const octave_value& v, |
|
84 unsigned int sc) |
|
85 : name (nm), value_stack (), storage_class (sc), count (1) |
|
86 { |
|
87 value_stack.push (v); |
|
88 } |
|
89 |
|
90 octave_value& varref (void) { return value_stack.top (); } |
|
91 |
|
92 octave_value varval (void) const { return value_stack.top (); } |
|
93 |
|
94 void push_context (void) { value_stack.push (octave_value ()); } |
|
95 |
|
96 void pop_context (void) { value_stack.pop (); } |
|
97 |
|
98 void clear (void) |
|
99 { |
|
100 if (! (is_hidden () || is_inherited ())) |
|
101 { |
|
102 if (is_global ()) |
|
103 unmark_global (); |
|
104 |
|
105 if (is_persistent ()) |
|
106 { |
|
107 symbol_table::persistent_varref (name) = varval (); |
|
108 unmark_persistent (); |
|
109 } |
|
110 |
|
111 varref () = octave_value (); |
|
112 } |
|
113 } |
|
114 |
|
115 bool is_defined (void) const { return varval ().is_defined (); } |
|
116 |
|
117 bool is_variable (void) const |
|
118 { |
|
119 return (storage_class != local || is_defined ()); |
|
120 } |
|
121 |
|
122 bool is_local (void) const { return storage_class & local; } |
|
123 bool is_automatic (void) const { return storage_class & automatic; } |
|
124 bool is_formal (void) const { return storage_class & formal; } |
|
125 bool is_hidden (void) const { return storage_class & hidden; } |
|
126 bool is_inherited (void) const { return storage_class & inherited; } |
|
127 bool is_global (void) const { return storage_class & global; } |
|
128 bool is_persistent (void) const { return storage_class & persistent; } |
|
129 |
|
130 void mark_local (void) { storage_class |= local; } |
|
131 void mark_automatic (void) { storage_class |= automatic; } |
|
132 void mark_formal (void) { storage_class |= formal; } |
|
133 void mark_hidden (void) { storage_class |= hidden; } |
|
134 void mark_inherited (void) { storage_class |= inherited; } |
|
135 void mark_global (void) |
|
136 { |
|
137 if (is_persistent ()) |
|
138 error ("can't make persistent variable %s global", name.c_str ()); |
|
139 else |
|
140 storage_class |= global; |
|
141 } |
|
142 void mark_persistent (void) |
|
143 { |
|
144 if (is_global ()) |
|
145 error ("can't make global variable %s persistent", name.c_str ()); |
|
146 else |
|
147 storage_class |= persistent; |
|
148 } |
|
149 |
|
150 void unmark_local (void) { storage_class &= ~local; } |
|
151 void unmark_automatic (void) { storage_class &= ~automatic; } |
|
152 void unmark_formal (void) { storage_class &= ~formal; } |
|
153 void unmark_hidden (void) { storage_class &= ~hidden; } |
|
154 void unmark_inherited (void) { storage_class &= ~inherited; } |
|
155 void unmark_global (void) { storage_class &= ~global; } |
|
156 void unmark_persistent (void) { storage_class &= ~persistent; } |
|
157 |
|
158 void init_persistent (void) |
|
159 { |
|
160 if (! is_defined ()) |
|
161 { |
|
162 mark_persistent (); |
|
163 |
|
164 varref () = symbol_table::persistent_varval (name); |
|
165 } |
|
166 // FIXME -- this causes trouble with recursive calls. |
|
167 // else |
|
168 // error ("unable to declare existing variable persistent"); |
|
169 } |
|
170 |
|
171 void erase_persistent (void) |
|
172 { |
|
173 unmark_persistent (); |
|
174 symbol_table::erase_persistent (name); |
|
175 } |
|
176 |
|
177 symbol_record_rep *dup (void) |
|
178 { |
|
179 return new symbol_record_rep (name, varval (), storage_class); |
|
180 } |
|
181 |
|
182 std::string name; |
|
183 |
|
184 std::stack<octave_value> value_stack; |
|
185 |
|
186 unsigned int storage_class; |
|
187 |
|
188 size_t count; |
|
189 |
|
190 private: |
|
191 |
|
192 // No copying! |
|
193 |
|
194 symbol_record_rep (const symbol_record_rep& ov); |
|
195 |
|
196 symbol_record_rep& operator = (const symbol_record_rep&); |
|
197 }; |
|
198 |
|
199 public: |
|
200 |
|
201 symbol_record (const std::string& nm = std::string (), |
|
202 const octave_value& v = octave_value (), |
|
203 unsigned int sc = local) |
|
204 : rep (new symbol_record_rep (nm, v, sc)) { } |
|
205 |
|
206 symbol_record (const symbol_record& sr) |
|
207 : rep (sr.rep) |
|
208 { |
|
209 rep->count++; |
|
210 } |
|
211 |
|
212 symbol_record& operator = (const symbol_record& sr) |
|
213 { |
|
214 if (this != &sr) |
|
215 { |
|
216 rep = sr.rep; |
|
217 rep->count++; |
|
218 } |
|
219 |
|
220 return *this; |
|
221 } |
|
222 |
|
223 ~symbol_record (void) |
|
224 { |
|
225 if (--rep->count == 0) |
|
226 delete rep; |
|
227 } |
|
228 |
|
229 symbol_record dup (void) const { return symbol_record (rep->dup ()); } |
|
230 |
|
231 std::string name (void) const { return rep->name; } |
|
232 |
|
233 octave_value |
|
234 find (tree_argument_list *args, const string_vector& arg_names, |
|
235 octave_value_list& evaluated_args, bool& args_evaluated) const; |
|
236 |
|
237 octave_value& varref (void) |
|
238 { |
|
239 return is_global () |
|
240 ? symbol_table::varref (name (), symbol_table::global_scope ()) |
|
241 : rep->varref (); |
|
242 } |
|
243 |
|
244 octave_value varval (void) const |
|
245 { |
|
246 return is_global () |
|
247 ? symbol_table::varval (name (), symbol_table::global_scope ()) |
|
248 : rep->varval (); |
|
249 } |
|
250 |
|
251 void push_context (void) |
|
252 { |
|
253 if (! (is_persistent () || is_global ())) |
|
254 rep->push_context (); |
|
255 } |
|
256 |
|
257 void pop_context (void) |
|
258 { |
|
259 if (! (is_persistent () || is_global ())) |
|
260 rep->pop_context (); |
|
261 } |
|
262 |
|
263 void clear (void) { rep->clear (); } |
|
264 |
|
265 bool is_defined (void) const { return rep->is_defined (); } |
|
266 bool is_variable (void) const { return rep->is_variable (); } |
|
267 |
|
268 bool is_local (void) const { return rep->is_local (); } |
|
269 bool is_automatic (void) const { return rep->is_automatic (); } |
|
270 bool is_formal (void) const { return rep->is_formal (); } |
|
271 bool is_global (void) const { return rep->is_global (); } |
|
272 bool is_hidden (void) const { return rep->is_hidden (); } |
|
273 bool is_inherited (void) const { return rep->is_inherited (); } |
|
274 bool is_persistent (void) const { return rep->is_persistent (); } |
|
275 |
|
276 void mark_local (void) { rep->mark_local (); } |
|
277 void mark_automatic (void) { rep->mark_automatic (); } |
|
278 void mark_formal (void) { rep->mark_formal (); } |
|
279 void mark_hidden (void) { rep->mark_hidden (); } |
|
280 void mark_inherited (void) { rep->mark_inherited (); } |
|
281 void mark_global (void) { rep->mark_global (); } |
|
282 void mark_persistent (void) { rep->mark_persistent (); } |
|
283 |
|
284 void unmark_local (void) { rep->unmark_local (); } |
|
285 void unmark_automatic (void) { rep->unmark_automatic (); } |
|
286 void unmark_formal (void) { rep->unmark_formal (); } |
|
287 void unmark_hidden (void) { rep->unmark_hidden (); } |
|
288 void unmark_inherited (void) { rep->unmark_inherited (); } |
|
289 void unmark_global (void) { rep->unmark_global (); } |
|
290 void unmark_persistent (void) { rep->unmark_persistent (); } |
|
291 |
|
292 void init_persistent (void) { rep->init_persistent (); } |
|
293 |
|
294 void erase_persistent (void) { rep->erase_persistent (); } |
|
295 |
|
296 unsigned int xstorage_class (void) const { return rep->storage_class; } |
|
297 |
|
298 private: |
|
299 |
|
300 symbol_record_rep *rep; |
|
301 |
|
302 symbol_record (symbol_record_rep *new_rep) : rep (new_rep) { } |
|
303 }; |
|
304 |
|
305 class |
|
306 fcn_info |
|
307 { |
|
308 public: |
|
309 |
|
310 typedef std::map<std::string, std::string> dispatch_map_type; |
|
311 |
|
312 typedef std::map<scope_id, octave_value>::const_iterator const_scope_val_iterator; |
|
313 typedef std::map<scope_id, octave_value>::iterator scope_val_iterator; |
4238
|
314 |
7336
|
315 typedef std::map<std::string, octave_value>::const_iterator const_str_val_iterator; |
|
316 typedef std::map<std::string, octave_value>::iterator str_val_iterator; |
|
317 |
|
318 typedef dispatch_map_type::const_iterator const_dispatch_map_iterator; |
|
319 typedef dispatch_map_type::iterator dispatch_map_iterator; |
|
320 |
|
321 private: |
|
322 |
|
323 class |
|
324 fcn_info_rep |
|
325 { |
|
326 public: |
|
327 |
|
328 fcn_info_rep (const std::string& nm) |
|
329 : name (nm), subfunctions (), private_functions (), |
|
330 class_constructors (), class_methods (), cmdline_function (), |
|
331 autoload_function (), function_on_path (), built_in_function (), |
|
332 count (1) { } |
|
333 |
|
334 octave_value load_private_function (const std::string& dir_name); |
|
335 |
|
336 octave_value load_class_constructor (void); |
|
337 |
|
338 octave_value load_class_method (const std::string& dispatch_type); |
|
339 |
|
340 octave_value |
|
341 find (tree_argument_list *args, const string_vector& arg_names, |
|
342 octave_value_list& evaluated_args, bool& args_evaluated, |
|
343 scope_id scope); |
|
344 |
|
345 octave_value find_method (const std::string& dispatch_type); |
|
346 |
|
347 octave_value find_autoload (void); |
|
348 |
|
349 octave_value find_user_function (void); |
|
350 |
|
351 bool is_user_function_defined (void) const |
|
352 { |
|
353 return function_on_path.is_defined (); |
|
354 } |
|
355 |
|
356 octave_value find_function (scope_id scope) |
|
357 { |
|
358 octave_value_list args; |
|
359 |
|
360 return find_function (args, scope); |
|
361 } |
|
362 |
|
363 octave_value find_function (const octave_value_list& args, |
|
364 scope_id scope) |
|
365 { |
|
366 string_vector arg_names; |
|
367 octave_value_list evaluated_args = args; |
|
368 bool args_evaluated; |
|
369 |
|
370 return find (0, arg_names, evaluated_args, args_evaluated, scope); |
|
371 } |
|
372 |
|
373 void install_cmdline_function (const octave_value& f) |
|
374 { |
|
375 cmdline_function = f; |
|
376 } |
|
377 |
|
378 void install_subfunction (const octave_value& f, scope_id scope) |
|
379 { |
|
380 subfunctions[scope] = f; |
|
381 } |
|
382 |
|
383 void install_user_function (const octave_value& f) |
|
384 { |
|
385 function_on_path = f; |
|
386 } |
|
387 |
|
388 void install_built_in_function (const octave_value& f) |
|
389 { |
|
390 built_in_function = f; |
|
391 } |
|
392 |
|
393 void clear (void) |
|
394 { |
|
395 subfunctions.clear (); |
|
396 private_functions.clear (); |
|
397 class_constructors.clear (); |
|
398 class_methods.clear (); |
|
399 cmdline_function = octave_value (); |
|
400 autoload_function = octave_value (); |
|
401 function_on_path = octave_value (); |
|
402 } |
|
403 |
|
404 // FIXME -- should this also clear the cmdline and other "user |
|
405 // defined" functions? |
|
406 void clear_user_function (void) |
|
407 { |
|
408 function_on_path = octave_value (); |
|
409 } |
|
410 |
|
411 void clear_mex_function (void) |
|
412 { |
|
413 if (function_on_path.is_mex_function ()) |
|
414 function_on_path = octave_value (); |
|
415 } |
|
416 |
|
417 void add_dispatch (const std::string& type, const std::string& fname) |
|
418 { |
|
419 dispatch_map[type] = fname; |
|
420 } |
|
421 |
|
422 void clear_dispatch (const std::string& type) |
|
423 { |
|
424 dispatch_map_iterator p = dispatch_map.find (type); |
|
425 |
|
426 if (p != dispatch_map.end ()) |
|
427 dispatch_map.erase (p); |
|
428 } |
|
429 |
|
430 void print_dispatch (std::ostream& os) const; |
|
431 |
|
432 std::string help_for_dispatch (void) const; |
|
433 |
|
434 dispatch_map_type get_dispatch (void) const { return dispatch_map; } |
|
435 |
|
436 std::string name; |
|
437 |
|
438 // Scope id to function object. |
|
439 std::map<scope_id, octave_value> subfunctions; |
|
440 |
|
441 // Directory name to function object. |
|
442 std::map<std::string, octave_value> private_functions; |
|
443 |
|
444 // Class name to function object. |
|
445 std::map<std::string, octave_value> class_constructors; |
|
446 |
|
447 // Dispatch type to function object. |
|
448 std::map<std::string, octave_value> class_methods; |
|
449 |
|
450 // Legacy dispatch map (dispatch type name to function name). |
|
451 dispatch_map_type dispatch_map; |
|
452 |
|
453 octave_value cmdline_function; |
|
454 |
|
455 octave_value autoload_function; |
|
456 |
|
457 octave_value function_on_path; |
|
458 |
|
459 octave_value built_in_function; |
|
460 |
|
461 size_t count; |
|
462 |
|
463 private: |
|
464 |
|
465 // No copying! |
|
466 |
|
467 fcn_info_rep (const fcn_info_rep&); |
|
468 |
|
469 fcn_info_rep& operator = (const fcn_info_rep&); |
|
470 }; |
|
471 |
|
472 public: |
|
473 |
|
474 fcn_info (const std::string& nm = std::string ()) |
|
475 : rep (new fcn_info_rep (nm)) { } |
|
476 |
|
477 fcn_info (const fcn_info& ov) : rep (ov.rep) |
|
478 { |
|
479 rep->count++; |
|
480 } |
|
481 |
|
482 fcn_info& operator = (const fcn_info& ov) |
|
483 { |
|
484 if (this != &ov) |
4238
|
485 { |
7336
|
486 rep = ov.rep; |
|
487 rep->count++; |
4238
|
488 } |
7336
|
489 |
|
490 return *this; |
|
491 } |
|
492 |
|
493 ~fcn_info (void) |
|
494 { |
|
495 if (--rep->count == 0) |
|
496 delete rep; |
|
497 } |
|
498 |
|
499 octave_value |
|
500 find (tree_argument_list *args, const string_vector& arg_names, |
|
501 octave_value_list& evaluated_args, bool& args_evaluated, |
|
502 scope_id scope); |
|
503 |
|
504 octave_value find_method (const std::string& dispatch_type) const |
|
505 { |
|
506 return rep->find_method (dispatch_type); |
|
507 } |
|
508 |
|
509 octave_value find_built_in_function (void) const |
|
510 { |
|
511 return rep->built_in_function; |
|
512 } |
|
513 |
|
514 octave_value find_autoload (void) |
|
515 { |
|
516 return rep->find_autoload (); |
|
517 } |
|
518 |
|
519 octave_value find_user_function (void) |
|
520 { |
|
521 return rep->find_user_function (); |
|
522 } |
|
523 |
|
524 bool is_user_function_defined (void) const |
|
525 { |
|
526 return rep->is_user_function_defined (); |
|
527 } |
|
528 |
|
529 octave_value find_function (scope_id scope) |
|
530 { |
|
531 return rep->find_function (scope); |
|
532 } |
|
533 |
|
534 octave_value find_function (const octave_value_list& args, |
|
535 scope_id scope) |
|
536 { |
|
537 return rep->find_function (args, scope); |
|
538 } |
|
539 |
|
540 void install_cmdline_function (const octave_value& f) |
|
541 { |
|
542 rep->install_cmdline_function (f); |
|
543 } |
|
544 |
|
545 void install_subfunction (const octave_value& f, scope_id scope) |
|
546 { |
|
547 rep->install_subfunction (f, scope); |
|
548 } |
|
549 |
|
550 void install_user_function (const octave_value& f) |
|
551 { |
|
552 rep->install_user_function (f); |
|
553 } |
|
554 |
|
555 void install_built_in_function (const octave_value& f) |
|
556 { |
|
557 rep->install_built_in_function (f); |
|
558 } |
|
559 |
|
560 void clear (void) { rep->clear (); } |
|
561 |
|
562 void clear_user_function (void) { rep->clear_user_function (); } |
|
563 |
|
564 void clear_mex_function (void) { rep->clear_mex_function (); } |
|
565 |
|
566 void add_dispatch (const std::string& type, const std::string& fname) |
|
567 { |
|
568 rep->add_dispatch (type, fname); |
|
569 } |
|
570 |
|
571 void clear_dispatch (const std::string& type) |
|
572 { |
|
573 rep->clear_dispatch (type); |
|
574 } |
|
575 |
|
576 void print_dispatch (std::ostream& os) const |
|
577 { |
|
578 rep->print_dispatch (os); |
|
579 } |
|
580 |
|
581 std::string help_for_dispatch (void) const { return rep->help_for_dispatch (); } |
|
582 |
|
583 dispatch_map_type get_dispatch (void) const |
|
584 { |
|
585 return rep->get_dispatch (); |
4009
|
586 } |
3011
|
587 |
7336
|
588 private: |
|
589 |
|
590 fcn_info_rep *rep; |
|
591 }; |
|
592 |
|
593 static scope_id global_scope (void) { return xglobal_scope; } |
|
594 static scope_id top_scope (void) { return xtop_scope; } |
|
595 |
|
596 static scope_id current_scope (void) { return xcurrent_scope; } |
|
597 static scope_id current_caller_scope (void) { return xcurrent_caller_scope; } |
|
598 |
|
599 // We use parent_scope to handle parsing subfunctions. |
|
600 static scope_id parent_scope (void) { return xparent_scope; } |
|
601 |
|
602 static scope_id alloc_scope (void) |
|
603 { |
|
604 scope_id retval; |
|
605 |
|
606 scope_ids_free_list_iterator p = scope_ids_free_list.begin (); |
|
607 |
|
608 if (p != scope_ids_free_list.end ()) |
|
609 { |
|
610 retval = *p; |
|
611 scope_ids_free_list.erase (p); |
|
612 } |
|
613 else |
|
614 retval = next_available_scope++; |
|
615 |
|
616 scope_ids_in_use.insert (retval); |
|
617 |
|
618 return retval; |
|
619 } |
|
620 |
|
621 static void set_scope (scope_id scope) |
|
622 { |
|
623 if (scope == xglobal_scope) |
|
624 error ("can't set scope to global"); |
|
625 else if (scope != xcurrent_scope) |
|
626 { |
|
627 all_instances_iterator p = all_instances.find (scope); |
|
628 |
|
629 if (p == all_instances.end ()) |
|
630 { |
|
631 instance = new symbol_table (); |
8
|
632 |
7336
|
633 all_instances[scope] = instance; |
|
634 } |
|
635 else |
|
636 instance = p->second; |
|
637 |
|
638 xcurrent_scope = scope; |
|
639 } |
|
640 } |
|
641 |
|
642 static void push_scope (scope_id scope) |
|
643 { |
|
644 if (scope_stack.empty ()) |
|
645 scope_stack.push_front (xtop_scope); |
|
646 |
|
647 xcurrent_caller_scope = xcurrent_scope; |
|
648 |
|
649 set_scope (scope); |
|
650 |
|
651 scope_stack.push_front (scope); |
|
652 } |
|
653 |
|
654 static void pop_scope (void) |
|
655 { |
|
656 scope_stack.pop_front (); |
8
|
657 |
7336
|
658 set_scope (scope_stack[0]); |
|
659 |
|
660 xcurrent_caller_scope = scope_stack[1]; |
|
661 } |
|
662 |
|
663 static void pop_scope (void *) { pop_scope (); } |
|
664 |
|
665 static void reset_scope (void) |
|
666 { |
|
667 scope_stack.clear (); |
|
668 |
|
669 scope_stack.push_front (xtop_scope); |
572
|
670 |
7336
|
671 set_scope (xtop_scope); |
|
672 |
|
673 xcurrent_caller_scope = -1; |
|
674 } |
|
675 |
|
676 static void set_parent_scope (scope_id scope) |
|
677 { |
|
678 xparent_scope = scope; |
|
679 } |
|
680 |
|
681 static void reset_parent_scope (void) |
|
682 { |
|
683 set_parent_scope (-1); |
|
684 } |
4009
|
685 |
7336
|
686 static void erase_scope (scope_id scope = xcurrent_scope) |
|
687 { |
|
688 all_instances_iterator p = all_instances.find (scope); |
|
689 |
|
690 if (p != all_instances.end ()) |
|
691 all_instances.erase (p); |
|
692 |
|
693 // free_scope (scope); |
|
694 } |
|
695 |
|
696 static scope_id dup_scope (scope_id scope = xcurrent_scope) |
|
697 { |
|
698 scope_id retval = -1; |
|
699 |
|
700 symbol_table *inst = get_instance (scope); |
|
701 |
|
702 if (inst) |
|
703 { |
|
704 scope_id new_scope = alloc_scope (); |
|
705 |
|
706 symbol_table *new_symbol_table = new symbol_table (); |
|
707 |
|
708 if (new_symbol_table) |
|
709 { |
|
710 all_instances[new_scope] = new_symbol_table; |
|
711 |
|
712 inst->do_dup_scope (*new_symbol_table); |
|
713 |
|
714 retval = new_scope; |
|
715 } |
|
716 } |
|
717 |
|
718 return retval; |
|
719 } |
|
720 |
|
721 #if 0 |
|
722 static void print_scope (const std::string& tag, scope_id scope) |
|
723 { |
|
724 symbol_table *inst = get_instance (scope); |
|
725 |
|
726 if (inst) |
|
727 { |
|
728 std::cerr << "printing " << tag << ", scope: " << scope |
|
729 << ", inst: " << inst << std::endl; |
|
730 |
|
731 inst->do_print_scope (std::cerr); |
|
732 } |
|
733 } |
4009
|
734 |
7336
|
735 void do_print_scope (std::ostream& os) const |
|
736 { |
|
737 for (const_table_iterator p = table.begin (); p != table.end (); p++) |
|
738 { |
|
739 symbol_record sr = p->second; |
|
740 |
|
741 octave_value val = sr.varval (); |
|
742 |
|
743 if (val.is_defined ()) |
|
744 sr.varval ().print_with_name (os, sr.name ()); |
|
745 else |
|
746 os << sr.name () << " is not defined" << std::endl; |
|
747 } |
|
748 } |
|
749 #endif |
|
750 |
|
751 static symbol_record find_symbol (const std::string& name, |
|
752 scope_id scope = xcurrent_scope) |
|
753 { |
|
754 symbol_table *inst = get_instance (scope); |
|
755 |
|
756 return inst ? inst->do_find_symbol (name) : symbol_record (); |
|
757 } |
4009
|
758 |
7336
|
759 static void inherit (scope_id scope, scope_id donor_scope) |
|
760 { |
|
761 symbol_table *inst = get_instance (scope); |
|
762 |
|
763 if (inst) |
|
764 inst->do_inherit (donor_scope); |
|
765 } |
|
766 |
|
767 static bool at_top_level (void) { return xcurrent_scope == xtop_scope; } |
|
768 |
|
769 // Find a value corresponding to the given name in the table. |
|
770 static octave_value |
|
771 find (const std::string& name, tree_argument_list *args, |
|
772 const string_vector& arg_names, |
|
773 octave_value_list& evaluated_args, bool& args_evaluated, |
|
774 scope_id scope = xcurrent_scope, bool skip_variables = false); |
|
775 |
|
776 // Insert a new name in the table. |
|
777 static symbol_record& |
|
778 insert (const std::string& name, scope_id scope = xcurrent_scope) |
|
779 { |
|
780 static symbol_record foobar; |
|
781 |
|
782 symbol_table *inst = get_instance (scope); |
4009
|
783 |
7336
|
784 return inst ? inst->do_insert (name) : foobar; |
|
785 } |
|
786 |
|
787 static octave_value& |
|
788 varref (const std::string& name, scope_id scope = xcurrent_scope) |
|
789 { |
|
790 static octave_value foobar; |
|
791 |
|
792 symbol_table *inst = get_instance (scope); |
|
793 |
|
794 return inst ? inst->do_varref (name) : foobar; |
|
795 } |
|
796 |
|
797 static octave_value |
|
798 varval (const std::string& name, scope_id scope = xcurrent_scope) |
|
799 { |
|
800 symbol_table *inst = get_instance (scope); |
|
801 |
|
802 return inst ? inst->do_varval (name) : octave_value (); |
|
803 } |
|
804 |
|
805 static octave_value& |
|
806 persistent_varref (const std::string& name, scope_id scope = xcurrent_scope) |
|
807 { |
|
808 static octave_value foobar; |
|
809 |
|
810 symbol_table *inst = get_instance (scope); |
|
811 |
|
812 return inst ? inst->do_persistent_varref (name) : foobar; |
|
813 } |
|
814 |
|
815 static octave_value |
|
816 persistent_varval (const std::string& name, scope_id scope = xcurrent_scope) |
|
817 { |
|
818 symbol_table *inst = get_instance (scope); |
|
819 |
|
820 return inst ? inst->do_persistent_varval (name) : octave_value (); |
|
821 } |
|
822 |
|
823 static void |
|
824 erase_persistent (const std::string& name, scope_id scope = xcurrent_scope) |
|
825 { |
|
826 symbol_table *inst = get_instance (scope); |
|
827 |
|
828 if (inst) |
|
829 inst->do_erase_persistent (name); |
|
830 } |
8
|
831 |
7336
|
832 static bool |
|
833 is_variable (const std::string& name, scope_id scope = xcurrent_scope) |
|
834 { |
|
835 symbol_table *inst = get_instance (scope); |
|
836 |
|
837 return inst ? inst->do_is_variable (name) : false; |
|
838 } |
|
839 |
|
840 static bool |
|
841 is_built_in_function_name (const std::string& name) |
|
842 { |
|
843 octave_value val = find_built_in_function (name); |
|
844 |
|
845 return val.is_defined (); |
|
846 } |
|
847 |
|
848 static octave_value |
|
849 find_method (const std::string& name, const std::string& dispatch_type) |
|
850 { |
|
851 const_fcn_table_iterator p = fcn_table.find (name); |
8
|
852 |
7336
|
853 if (p != fcn_table.end ()) |
|
854 return p->second.find_method (dispatch_type); |
|
855 else |
|
856 { |
|
857 fcn_info finfo (name); |
|
858 |
|
859 octave_value fcn = finfo.find_method (dispatch_type); |
|
860 |
|
861 if (fcn.is_defined ()) |
|
862 fcn_table[name] = finfo; |
|
863 |
|
864 return fcn; |
|
865 } |
|
866 } |
|
867 |
|
868 static octave_value |
|
869 find_built_in_function (const std::string& name) |
|
870 { |
|
871 const_fcn_table_iterator p = fcn_table.find (name); |
|
872 |
|
873 return (p != fcn_table.end ()) |
|
874 ? p->second.find_built_in_function () : octave_value (); |
|
875 } |
|
876 |
|
877 static octave_value |
|
878 find_autoload (const std::string& name) |
|
879 { |
|
880 fcn_table_iterator p = fcn_table.find (name); |
4913
|
881 |
7336
|
882 return (p != fcn_table.end ()) |
|
883 ? p->second.find_autoload () : octave_value (); |
|
884 } |
|
885 |
|
886 static octave_value |
|
887 find_function (const std::string& name, tree_argument_list *args, |
|
888 const string_vector& arg_names, |
|
889 octave_value_list& evaluated_args, bool& args_evaluated, |
|
890 scope_id scope = xcurrent_scope); |
|
891 |
|
892 static octave_value |
|
893 find_user_function (const std::string& name) |
|
894 { |
|
895 fcn_table_iterator p = fcn_table.find (name); |
|
896 |
|
897 return (p != fcn_table.end ()) |
|
898 ? p->second.find_user_function () : octave_value (); |
|
899 } |
|
900 |
|
901 static octave_value |
|
902 find_function (const std::string& name, scope_id scope = xcurrent_scope) |
|
903 { |
|
904 octave_value_list evaluated_args; |
|
905 |
|
906 return find_function (name, evaluated_args, scope); |
|
907 } |
|
908 |
|
909 static octave_value |
|
910 find_function (const std::string& name, const octave_value_list& args, |
|
911 scope_id scope = xcurrent_scope) |
|
912 { |
|
913 string_vector arg_names; |
|
914 octave_value_list evaluated_args = args; |
|
915 bool args_evaluated = ! args.empty (); |
|
916 |
|
917 return find_function (name, 0, arg_names, evaluated_args, |
|
918 args_evaluated, scope); |
|
919 } |
|
920 |
|
921 static void install_cmdline_function (const std::string& name, |
|
922 const octave_value& fcn) |
|
923 { |
|
924 fcn_table_iterator p = fcn_table.find (name); |
|
925 |
|
926 if (p != fcn_table.end ()) |
|
927 { |
|
928 fcn_info& finfo = p->second; |
3013
|
929 |
7336
|
930 finfo.install_cmdline_function (fcn); |
|
931 } |
|
932 else |
|
933 { |
|
934 fcn_info finfo (name); |
195
|
935 |
7336
|
936 finfo.install_cmdline_function (fcn); |
|
937 |
|
938 fcn_table[name] = finfo; |
|
939 } |
|
940 } |
|
941 |
|
942 static void install_subfunction (const std::string& name, |
|
943 const octave_value& fcn, |
|
944 scope_id scope = xparent_scope) |
|
945 { |
|
946 fcn_table_iterator p = fcn_table.find (name); |
|
947 |
|
948 if (p != fcn_table.end ()) |
|
949 { |
|
950 fcn_info& finfo = p->second; |
|
951 |
|
952 finfo.install_subfunction (fcn, scope); |
|
953 } |
|
954 else |
|
955 { |
|
956 fcn_info finfo (name); |
|
957 |
|
958 finfo.install_subfunction (fcn, scope); |
|
959 |
|
960 fcn_table[name] = finfo; |
|
961 } |
|
962 } |
|
963 |
|
964 static void install_user_function (const std::string& name, |
|
965 const octave_value& fcn) |
|
966 { |
|
967 fcn_table_iterator p = fcn_table.find (name); |
|
968 |
|
969 if (p != fcn_table.end ()) |
|
970 { |
|
971 fcn_info& finfo = p->second; |
|
972 |
|
973 finfo.install_user_function (fcn); |
|
974 } |
|
975 else |
|
976 { |
|
977 fcn_info finfo (name); |
|
978 |
|
979 finfo.install_user_function (fcn); |
|
980 |
|
981 fcn_table[name] = finfo; |
|
982 } |
|
983 } |
605
|
984 |
7336
|
985 static void install_built_in_function (const std::string& name, |
|
986 const octave_value& fcn) |
|
987 { |
|
988 fcn_table_iterator p = fcn_table.find (name); |
|
989 |
|
990 if (p != fcn_table.end ()) |
|
991 { |
|
992 fcn_info& finfo = p->second; |
|
993 |
|
994 finfo.install_built_in_function (fcn); |
|
995 } |
|
996 else |
|
997 { |
|
998 fcn_info finfo (name); |
|
999 |
|
1000 finfo.install_built_in_function (fcn); |
|
1001 |
|
1002 fcn_table[name] = finfo; |
|
1003 } |
|
1004 } |
|
1005 |
|
1006 static void clear (const std::string& name, scope_id scope = xcurrent_scope) |
|
1007 { |
|
1008 clear_variable (name, scope); |
|
1009 } |
|
1010 |
|
1011 static void clear_all (void) |
|
1012 { |
|
1013 clear_variables (); |
|
1014 |
|
1015 clear_functions (); |
|
1016 } |
|
1017 |
|
1018 static void clear_variables (scope_id scope = xcurrent_scope) |
|
1019 { |
|
1020 symbol_table *inst = get_instance (scope); |
|
1021 |
|
1022 if (inst) |
|
1023 inst->do_clear_variables (); |
|
1024 } |
|
1025 |
|
1026 // For unwind_protect. |
|
1027 static void clear_variables (void *) { clear_variables (); } |
|
1028 |
|
1029 static void clear_functions (void) |
|
1030 { |
|
1031 for (fcn_table_iterator p = fcn_table.begin (); p != fcn_table.end (); p++) |
|
1032 p->second.clear (); |
|
1033 } |
|
1034 |
|
1035 static void clear_function (const std::string& name) |
|
1036 { |
|
1037 clear_user_function (name); |
|
1038 } |
|
1039 |
|
1040 static void clear_global (const std::string& name, |
|
1041 scope_id scope = xcurrent_scope) |
|
1042 { |
|
1043 symbol_table *inst = get_instance (scope); |
|
1044 |
|
1045 if (inst) |
|
1046 inst->do_clear_global (name); |
|
1047 } |
|
1048 |
|
1049 static void clear_variable (const std::string& name, |
|
1050 scope_id scope = xcurrent_scope) |
|
1051 { |
|
1052 symbol_table *inst = get_instance (scope); |
|
1053 |
|
1054 if (inst) |
|
1055 inst->do_clear_variable (name); |
|
1056 } |
|
1057 |
|
1058 static void clear_symbol (const std::string& name) |
|
1059 { |
|
1060 // FIXME -- are we supposed to do both here? |
|
1061 |
|
1062 clear_variable (name); |
|
1063 clear_function (name); |
|
1064 } |
|
1065 |
|
1066 static void clear_function_pattern (const std::string& pat) |
|
1067 { |
|
1068 glob_match pattern (pat); |
|
1069 |
|
1070 for (fcn_table_iterator p = fcn_table.begin (); p != fcn_table.end (); p++) |
|
1071 { |
|
1072 if (pattern.match (p->first)) |
|
1073 p->second.clear_user_function (); |
|
1074 } |
|
1075 } |
|
1076 |
|
1077 static void clear_global_pattern (const std::string& pat, |
|
1078 scope_id scope = xcurrent_scope) |
|
1079 { |
|
1080 symbol_table *inst = get_instance (scope); |
4009
|
1081 |
7336
|
1082 if (inst) |
|
1083 inst->do_clear_global_pattern (pat); |
|
1084 } |
|
1085 |
|
1086 static void clear_variable_pattern (const std::string& pat, |
|
1087 scope_id scope = xcurrent_scope) |
|
1088 { |
|
1089 symbol_table *inst = get_instance (scope); |
|
1090 |
|
1091 if (inst) |
|
1092 inst->do_clear_variable_pattern (pat); |
|
1093 } |
|
1094 |
|
1095 static void clear_symbol_pattern (const std::string& pat) |
|
1096 { |
|
1097 // FIXME -- are we supposed to do both here? |
|
1098 |
|
1099 clear_variable_pattern (pat); |
|
1100 clear_function_pattern (pat); |
|
1101 } |
|
1102 |
|
1103 static void clear_user_function (const std::string& name) |
|
1104 { |
|
1105 fcn_table_iterator p = fcn_table.find (name); |
|
1106 |
|
1107 if (p != fcn_table.end ()) |
|
1108 { |
|
1109 fcn_info& finfo = p->second; |
|
1110 |
|
1111 finfo.clear_user_function (); |
|
1112 } |
|
1113 // FIXME -- is this necessary, or even useful? |
|
1114 // else |
|
1115 // error ("clear: no such function `%s'", name.c_str ()); |
|
1116 } |
|
1117 |
|
1118 static void clear_mex_functions (void) |
|
1119 { |
|
1120 for (fcn_table_iterator p = fcn_table.begin (); p != fcn_table.end (); p++) |
|
1121 { |
|
1122 fcn_info& finfo = p->second; |
|
1123 |
|
1124 finfo.clear_mex_function (); |
|
1125 } |
|
1126 } |
|
1127 |
|
1128 static void alias_built_in_function (const std::string& alias, |
|
1129 const std::string& name) |
|
1130 { |
|
1131 octave_value fcn = find_built_in_function (name); |
|
1132 |
|
1133 if (fcn.is_defined ()) |
|
1134 { |
|
1135 fcn_info finfo (alias); |
|
1136 |
|
1137 finfo.install_built_in_function (fcn); |
|
1138 |
|
1139 fcn_table[alias] = finfo; |
|
1140 } |
|
1141 else |
|
1142 panic ("alias: `%s' is undefined", name.c_str ()); |
|
1143 } |
|
1144 |
|
1145 static void add_dispatch (const std::string& name, const std::string& type, |
|
1146 const std::string& fname) |
|
1147 { |
|
1148 fcn_table_iterator p = fcn_table.find (name); |
|
1149 |
|
1150 if (p != fcn_table.end ()) |
|
1151 { |
|
1152 fcn_info& finfo = p->second; |
|
1153 |
|
1154 finfo.add_dispatch (type, fname); |
|
1155 } |
|
1156 else |
|
1157 { |
|
1158 fcn_info finfo (name); |
|
1159 |
|
1160 finfo.add_dispatch (type, fname); |
|
1161 |
|
1162 fcn_table[name] = finfo; |
|
1163 } |
|
1164 } |
|
1165 |
|
1166 static void clear_dispatch (const std::string& name, const std::string& type) |
|
1167 { |
|
1168 fcn_table_iterator p = fcn_table.find (name); |
|
1169 |
|
1170 if (p != fcn_table.end ()) |
|
1171 { |
|
1172 fcn_info& finfo = p->second; |
|
1173 |
|
1174 finfo.clear_dispatch (type); |
|
1175 } |
|
1176 } |
|
1177 |
|
1178 static void print_dispatch (std::ostream& os, const std::string& name) |
|
1179 { |
|
1180 fcn_table_iterator p = fcn_table.find (name); |
4009
|
1181 |
7336
|
1182 if (p != fcn_table.end ()) |
|
1183 { |
|
1184 fcn_info& finfo = p->second; |
|
1185 |
|
1186 finfo.print_dispatch (os); |
|
1187 } |
|
1188 } |
|
1189 |
|
1190 static fcn_info::dispatch_map_type get_dispatch (const std::string& name) |
|
1191 { |
|
1192 fcn_info::dispatch_map_type retval; |
|
1193 |
|
1194 fcn_table_iterator p = fcn_table.find (name); |
|
1195 |
|
1196 if (p != fcn_table.end ()) |
|
1197 { |
|
1198 fcn_info& finfo = p->second; |
|
1199 |
|
1200 retval = finfo.get_dispatch (); |
|
1201 } |
|
1202 |
|
1203 return retval; |
|
1204 } |
|
1205 |
|
1206 static std::string help_for_dispatch (const std::string& name) |
|
1207 { |
|
1208 std::string retval; |
|
1209 |
|
1210 fcn_table_iterator p = fcn_table.find (name); |
|
1211 |
|
1212 if (p != fcn_table.end ()) |
|
1213 { |
|
1214 fcn_info& finfo = p->second; |
|
1215 |
|
1216 retval = finfo.help_for_dispatch (); |
|
1217 } |
|
1218 |
|
1219 return retval; |
|
1220 } |
|
1221 |
|
1222 static void push_context (scope_id scope = xcurrent_scope) |
|
1223 { |
|
1224 if (scope == xglobal_scope || scope == xtop_scope) |
|
1225 error ("invalid call to xymtab::push_context"); |
|
1226 else |
|
1227 { |
|
1228 symbol_table *inst = get_instance (scope); |
|
1229 |
|
1230 if (inst) |
|
1231 inst->do_push_context (); |
|
1232 } |
|
1233 } |
|
1234 |
|
1235 static void pop_context (scope_id scope = xcurrent_scope) |
|
1236 { |
|
1237 if (scope == xglobal_scope || scope == xtop_scope) |
|
1238 error ("invalid call to xymtab::push_context"); |
|
1239 else |
|
1240 { |
|
1241 symbol_table *inst = get_instance (scope); |
|
1242 |
|
1243 if (inst) |
|
1244 inst->do_pop_context (); |
|
1245 } |
|
1246 } |
|
1247 |
|
1248 // For unwind_protect. |
|
1249 static void pop_context (void *) { pop_context (); } |
|
1250 |
|
1251 static void mark_hidden (const std::string& name, |
|
1252 scope_id scope = xcurrent_scope) |
|
1253 { |
|
1254 symbol_table *inst = get_instance (scope); |
|
1255 |
|
1256 if (inst) |
|
1257 inst->do_mark_hidden (name); |
|
1258 } |
|
1259 |
|
1260 static void mark_global (const std::string& name, |
|
1261 scope_id scope = xcurrent_scope) |
|
1262 { |
|
1263 symbol_table *inst = get_instance (scope); |
|
1264 |
|
1265 if (inst) |
|
1266 inst->do_mark_global (name); |
|
1267 } |
|
1268 |
|
1269 static std::list<symbol_record> |
|
1270 all_variables (scope_id scope = xcurrent_scope, bool defined_only = true) |
|
1271 { |
|
1272 symbol_table *inst = get_instance (scope); |
|
1273 |
|
1274 return inst |
|
1275 ? inst->do_all_variables (defined_only) : std::list<symbol_record> (); |
|
1276 } |
3011
|
1277 |
7336
|
1278 static std::list<symbol_record> |
|
1279 glob (const std::string& pattern, scope_id scope = xcurrent_scope) |
|
1280 { |
|
1281 symbol_table *inst = get_instance (scope); |
|
1282 |
|
1283 return inst ? inst->do_glob (pattern) : std::list<symbol_record> (); |
|
1284 } |
|
1285 |
|
1286 static std::list<symbol_record> |
|
1287 glob_variables (const std::string& pattern, scope_id scope = xcurrent_scope) |
|
1288 { |
|
1289 symbol_table *inst = get_instance (scope); |
|
1290 |
|
1291 return inst ? inst->do_glob (pattern, true) : std::list<symbol_record> (); |
|
1292 } |
|
1293 |
|
1294 static std::list<symbol_record> |
|
1295 glob_variables (const string_vector& patterns, |
|
1296 scope_id scope = xcurrent_scope) |
|
1297 { |
|
1298 std::list<symbol_record> retval; |
|
1299 |
|
1300 size_t len = patterns.length (); |
8
|
1301 |
7336
|
1302 for (size_t i = 0; i < len; i++) |
|
1303 { |
|
1304 std::list<symbol_record> tmp = glob_variables (patterns[i], scope); |
|
1305 |
|
1306 retval.insert (retval.begin (), tmp.begin (), tmp.end ()); |
|
1307 } |
|
1308 |
|
1309 return retval; |
|
1310 } |
|
1311 |
|
1312 static std::list<std::string> user_function_names (void) |
|
1313 { |
|
1314 std::list<std::string> retval; |
|
1315 |
|
1316 for (fcn_table_iterator p = fcn_table.begin (); |
|
1317 p != fcn_table.end (); p++) |
|
1318 { |
|
1319 if (p->second.is_user_function_defined ()) |
|
1320 retval.push_back (p->first); |
|
1321 } |
|
1322 |
|
1323 if (! retval.empty ()) |
|
1324 retval.sort (); |
|
1325 |
|
1326 return retval; |
|
1327 } |
3011
|
1328 |
7336
|
1329 static std::list<std::string> |
|
1330 variable_names (scope_id scope = xcurrent_scope) |
|
1331 { |
|
1332 symbol_table *inst = get_instance (scope); |
|
1333 |
|
1334 return inst ? inst->do_variable_names () : std::list<std::string> (); |
|
1335 } |
|
1336 |
|
1337 static std::list<std::string> built_in_function_names (void) |
|
1338 { |
|
1339 std::list<std::string> retval; |
|
1340 |
|
1341 for (const_fcn_table_iterator p = fcn_table.begin (); |
|
1342 p != fcn_table.end (); p++) |
|
1343 { |
|
1344 octave_value fcn = p->second.find_built_in_function (); |
|
1345 |
|
1346 if (fcn.is_defined ()) |
|
1347 retval.push_back (p->first); |
|
1348 } |
|
1349 |
|
1350 if (! retval.empty ()) |
|
1351 retval.sort (); |
220
|
1352 |
7336
|
1353 return retval; |
|
1354 } |
|
1355 |
|
1356 static bool is_local_variable (const std::string& name, |
|
1357 scope_id scope = xcurrent_scope) |
|
1358 { |
|
1359 if (scope == xglobal_scope) |
|
1360 return false; |
|
1361 else |
|
1362 { |
|
1363 symbol_table *inst = get_instance (scope); |
5861
|
1364 |
7336
|
1365 return inst ? inst->do_is_local_variable (name) : false; |
|
1366 } |
|
1367 } |
5861
|
1368 |
7336
|
1369 static bool is_global (const std::string& name, |
|
1370 scope_id scope = xcurrent_scope) |
|
1371 { |
|
1372 if (scope == xglobal_scope) |
|
1373 return true; |
|
1374 else |
|
1375 { |
|
1376 symbol_table *inst = get_instance (scope); |
|
1377 |
|
1378 return inst ? inst->do_is_global (name) : false; |
|
1379 } |
|
1380 } |
3011
|
1381 |
8
|
1382 private: |
|
1383 |
7336
|
1384 typedef std::map<std::string, symbol_record>::const_iterator const_table_iterator; |
|
1385 typedef std::map<std::string, symbol_record>::iterator table_iterator; |
|
1386 |
|
1387 typedef std::map<std::string, octave_value>::const_iterator const_persistent_table_iterator; |
|
1388 typedef std::map<std::string, octave_value>::iterator persistent_table_iterator; |
|
1389 |
|
1390 typedef std::map<scope_id, symbol_table*>::const_iterator all_instances_const_iterator; |
|
1391 typedef std::map<scope_id, symbol_table*>::iterator all_instances_iterator; |
|
1392 |
|
1393 typedef std::map<std::string, fcn_info>::const_iterator const_fcn_table_iterator; |
|
1394 typedef std::map<std::string, fcn_info>::iterator fcn_table_iterator; |
|
1395 |
|
1396 typedef std::set<scope_id>::const_iterator scope_ids_free_list_const_iterator; |
|
1397 typedef std::set<scope_id>::iterator scope_ids_free_list_iterator; |
|
1398 |
|
1399 typedef std::set<scope_id>::const_iterator scope_ids_in_use_const_iterator; |
|
1400 typedef std::set<scope_id>::iterator scope_ids_in_use_iterator; |
|
1401 |
|
1402 // Map from symbol names to symbol info. |
|
1403 std::map<std::string, symbol_record> table; |
|
1404 |
|
1405 // Map from names of persistent variables to values. |
|
1406 std::map<std::string, octave_value> persistent_table; |
|
1407 |
|
1408 // Pointer to symbol table for current scope (variables only). |
|
1409 static symbol_table *instance; |
3011
|
1410 |
7336
|
1411 // Map from scope id to symbol table instances. |
|
1412 static std::map<scope_id, symbol_table*> all_instances; |
|
1413 |
|
1414 // Map from function names to function info (subfunctions, private |
|
1415 // functions, class constructors, class methods, etc.) |
|
1416 static std::map<std::string, fcn_info> fcn_table; |
|
1417 |
|
1418 static const scope_id xglobal_scope; |
|
1419 static const scope_id xtop_scope; |
|
1420 |
|
1421 static scope_id xcurrent_scope; |
|
1422 static scope_id xcurrent_caller_scope; |
|
1423 |
|
1424 // We use parent_scope to handle parsing subfunctions. |
|
1425 static scope_id xparent_scope; |
|
1426 |
|
1427 static std::deque<scope_id> scope_stack; |
|
1428 |
|
1429 // The next available scope ID. |
|
1430 static scope_id next_available_scope; |
|
1431 |
|
1432 // The set of scope IDs that are currently allocated. |
|
1433 static std::set<scope_id> scope_ids_in_use; |
|
1434 |
|
1435 // The set of scope IDs that are currently available. |
|
1436 static std::set<scope_id> scope_ids_free_list; |
|
1437 |
|
1438 symbol_table (void) : table () { } |
|
1439 |
|
1440 ~symbol_table (void) { } |
3011
|
1441 |
7336
|
1442 static void free_scope (scope_id scope) |
|
1443 { |
|
1444 if (scope == xglobal_scope || scope == xtop_scope) |
|
1445 error ("can't free global or top-level scopes!"); |
|
1446 else |
|
1447 { |
|
1448 scope_ids_in_use_iterator p = scope_ids_in_use.find (scope); |
|
1449 |
|
1450 if (p != scope_ids_in_use.end ()) |
|
1451 { |
|
1452 scope_ids_in_use.erase (p); |
|
1453 scope_ids_free_list.insert (*p); |
|
1454 } |
|
1455 else |
|
1456 error ("scope id = %ld not found!", scope); |
|
1457 } |
|
1458 } |
|
1459 |
|
1460 static symbol_table *get_instance (scope_id scope) |
|
1461 { |
|
1462 symbol_table *retval = 0; |
|
1463 |
|
1464 if (scope == xcurrent_scope) |
|
1465 { |
|
1466 if (! instance) |
|
1467 { |
|
1468 instance = new symbol_table (); |
4238
|
1469 |
7336
|
1470 all_instances[scope] = instance; |
|
1471 } |
|
1472 |
|
1473 if (! instance) |
|
1474 error ("unable to create symbol_table object!"); |
|
1475 |
|
1476 retval = instance; |
|
1477 } |
|
1478 else |
|
1479 { |
|
1480 all_instances_iterator p = all_instances.find (scope); |
|
1481 |
|
1482 if (p == all_instances.end ()) |
|
1483 { |
|
1484 retval = new symbol_table (); |
|
1485 |
|
1486 all_instances[scope] = retval; |
|
1487 } |
|
1488 else |
|
1489 retval = p->second; |
|
1490 } |
|
1491 |
|
1492 return retval; |
|
1493 } |
|
1494 |
|
1495 void insert_symbol_record (const symbol_record& sr) |
|
1496 { |
|
1497 table[sr.name ()] = sr; |
|
1498 } |
4238
|
1499 |
4913
|
1500 void |
7336
|
1501 do_dup_scope (symbol_table& new_symbol_table) const |
|
1502 { |
|
1503 for (const_table_iterator p = table.begin (); p != table.end (); p++) |
|
1504 new_symbol_table.insert_symbol_record (p->second.dup ()); |
|
1505 } |
|
1506 |
|
1507 symbol_record do_find_symbol (const std::string& name) |
|
1508 { |
|
1509 table_iterator p = table.find (name); |
|
1510 |
|
1511 if (p == table.end ()) |
|
1512 return do_insert (name); |
|
1513 else |
|
1514 return p->second; |
|
1515 } |
|
1516 |
|
1517 void do_inherit (scope_id donor_scope) |
|
1518 { |
|
1519 for (table_iterator p = table.begin (); p != table.end (); p++) |
|
1520 { |
|
1521 symbol_record& sr = p->second; |
|
1522 |
|
1523 std::string nm = sr.name (); |
|
1524 |
|
1525 if (! (sr.is_automatic () || sr.is_formal () || nm == "__retval__")) |
|
1526 { |
|
1527 octave_value val = symbol_table::varval (nm, donor_scope); |
|
1528 |
|
1529 if (val.is_defined ()) |
|
1530 { |
|
1531 sr.varref () = val; |
|
1532 |
|
1533 sr.mark_inherited (); |
|
1534 } |
|
1535 } |
|
1536 } |
|
1537 } |
|
1538 |
|
1539 octave_value |
|
1540 do_find (const std::string& name, tree_argument_list *args, |
|
1541 const string_vector& arg_names, |
|
1542 octave_value_list& evaluated_args, bool& args_evaluated, |
|
1543 scope_id scope, bool skip_variables); |
|
1544 |
|
1545 symbol_record& do_insert (const std::string& name) |
|
1546 { |
|
1547 table_iterator p = table.find (name); |
|
1548 |
|
1549 return p == table.end () |
|
1550 ? (table[name] = symbol_record (name)) : p->second; |
|
1551 } |
|
1552 |
|
1553 octave_value& do_varref (const std::string& name) |
|
1554 { |
|
1555 table_iterator p = table.find (name); |
|
1556 |
|
1557 if (p == table.end ()) |
|
1558 { |
|
1559 symbol_record& sr = do_insert (name); |
|
1560 |
|
1561 return sr.varref (); |
|
1562 } |
|
1563 else |
|
1564 return p->second.varref (); |
|
1565 } |
4913
|
1566 |
7336
|
1567 octave_value do_varval (const std::string& name) const |
|
1568 { |
|
1569 const_table_iterator p = table.find (name); |
|
1570 |
|
1571 return (p != table.end ()) ? p->second.varval () : octave_value (); |
|
1572 } |
|
1573 |
|
1574 octave_value& do_persistent_varref (const std::string& name) |
|
1575 { |
|
1576 persistent_table_iterator p = persistent_table.find (name); |
|
1577 |
|
1578 return (p == persistent_table.end ()) |
|
1579 ? persistent_table[name] : p->second; |
|
1580 } |
|
1581 |
|
1582 octave_value do_persistent_varval (const std::string& name) |
|
1583 { |
|
1584 const_persistent_table_iterator p = persistent_table.find (name); |
|
1585 |
|
1586 return (p != persistent_table.end ()) ? p->second : octave_value (); |
|
1587 } |
|
1588 |
|
1589 void do_erase_persistent (const std::string& name) |
|
1590 { |
|
1591 persistent_table_iterator p = persistent_table.find (name); |
|
1592 |
|
1593 if (p != persistent_table.end ()) |
|
1594 persistent_table.erase (p); |
|
1595 } |
|
1596 |
|
1597 bool do_is_variable (const std::string& name) const |
|
1598 { |
|
1599 bool retval = false; |
|
1600 |
|
1601 const_table_iterator p = table.find (name); |
|
1602 |
|
1603 if (p != table.end ()) |
|
1604 { |
|
1605 const symbol_record& sr = p->second; |
|
1606 |
|
1607 retval = sr.is_variable (); |
|
1608 } |
|
1609 |
|
1610 return retval; |
|
1611 } |
|
1612 |
|
1613 void do_push_context (void) |
|
1614 { |
|
1615 for (table_iterator p = table.begin (); p != table.end (); p++) |
|
1616 p->second.push_context (); |
|
1617 } |
|
1618 |
|
1619 void do_pop_context (void) |
|
1620 { |
|
1621 for (table_iterator p = table.begin (); p != table.end (); p++) |
|
1622 p->second.pop_context (); |
|
1623 } |
|
1624 |
|
1625 void do_clear_variables (void) |
|
1626 { |
|
1627 for (table_iterator p = table.begin (); p != table.end (); p++) |
|
1628 p->second.clear (); |
|
1629 } |
|
1630 |
|
1631 void do_clear_global (const std::string& name) |
|
1632 { |
|
1633 table_iterator p = table.find (name); |
4913
|
1634 |
7336
|
1635 if (p != table.end ()) |
|
1636 { |
|
1637 symbol_record& sr = p->second; |
|
1638 |
|
1639 if (sr.is_global ()) |
|
1640 { |
|
1641 symbol_table::clear_variable (p->first, xglobal_scope); |
|
1642 |
|
1643 sr.unmark_global (); |
|
1644 } |
|
1645 } |
|
1646 } |
|
1647 |
|
1648 void do_clear_variable (const std::string& name) |
|
1649 { |
|
1650 table_iterator p = table.find (name); |
|
1651 |
|
1652 if (p != table.end ()) |
|
1653 p->second.clear (); |
|
1654 } |
|
1655 |
|
1656 void do_clear_global_pattern (const std::string& pat) |
|
1657 { |
|
1658 glob_match pattern (pat); |
|
1659 |
|
1660 for (table_iterator p = table.begin (); p != table.end (); p++) |
|
1661 { |
|
1662 symbol_record& sr = p->second; |
8
|
1663 |
7336
|
1664 if (sr.is_global ()) |
|
1665 { |
|
1666 if (pattern.match (sr.name ())) |
|
1667 { |
|
1668 symbol_table::clear_variable (p->first, xglobal_scope); |
|
1669 |
|
1670 sr.unmark_global (); |
|
1671 } |
|
1672 } |
|
1673 } |
|
1674 } |
|
1675 |
|
1676 void do_clear_variable_pattern (const std::string& pat) |
|
1677 { |
|
1678 glob_match pattern (pat); |
|
1679 |
|
1680 for (table_iterator p = table.begin (); p != table.end (); p++) |
|
1681 { |
|
1682 symbol_record& sr = p->second; |
|
1683 |
|
1684 if (sr.is_defined () || sr.is_global ()) |
|
1685 { |
|
1686 if (pattern.match (sr.name ())) |
|
1687 sr.clear (); |
|
1688 } |
|
1689 } |
|
1690 } |
|
1691 |
|
1692 void do_mark_hidden (const std::string& name) |
|
1693 { |
|
1694 table_iterator p = table.find (name); |
|
1695 |
|
1696 if (p != table.end ()) |
|
1697 p->second.mark_hidden (); |
|
1698 } |
|
1699 |
|
1700 void do_mark_global (const std::string& name) |
|
1701 { |
|
1702 table_iterator p = table.find (name); |
8
|
1703 |
7336
|
1704 if (p != table.end ()) |
|
1705 p->second.mark_global (); |
|
1706 } |
|
1707 |
|
1708 std::list<symbol_record> do_all_variables (bool defined_only) const |
|
1709 { |
|
1710 std::list<symbol_record> retval; |
|
1711 |
|
1712 for (const_table_iterator p = table.begin (); p != table.end (); p++) |
|
1713 { |
|
1714 const symbol_record& sr = p->second; |
|
1715 |
|
1716 if (defined_only && ! sr.is_defined ()) |
|
1717 continue; |
|
1718 |
|
1719 retval.push_back (sr); |
|
1720 } |
|
1721 |
|
1722 return retval; |
|
1723 } |
|
1724 |
|
1725 std::list<symbol_record> do_glob (const std::string& pattern, |
|
1726 bool vars_only = false) const |
|
1727 { |
|
1728 std::list<symbol_record> retval; |
|
1729 |
|
1730 glob_match pat (pattern); |
|
1731 |
|
1732 for (const_table_iterator p = table.begin (); p != table.end (); p++) |
|
1733 { |
|
1734 if (pat.match (p->first)) |
|
1735 { |
|
1736 const symbol_record& sr = p->second; |
1962
|
1737 |
7336
|
1738 if (vars_only && ! sr.is_variable ()) |
|
1739 continue; |
|
1740 |
|
1741 retval.push_back (sr); |
|
1742 } |
|
1743 } |
|
1744 |
|
1745 return retval; |
|
1746 } |
|
1747 |
|
1748 std::list<std::string> do_variable_names (void) |
|
1749 { |
|
1750 std::list<std::string> retval; |
|
1751 |
|
1752 for (const_table_iterator p = table.begin (); p != table.end (); p++) |
|
1753 retval.push_back (p->first); |
|
1754 |
|
1755 retval.sort (); |
|
1756 |
|
1757 return retval; |
|
1758 } |
|
1759 |
|
1760 bool do_is_local_variable (const std::string& name) const |
|
1761 { |
|
1762 const_table_iterator p = table.find (name); |
|
1763 |
|
1764 return (p != table.end () |
|
1765 && ! p->second.is_global () |
|
1766 && p->second.is_defined ()); |
|
1767 } |
|
1768 |
|
1769 bool do_is_global (const std::string& name) const |
|
1770 { |
|
1771 const_table_iterator p = table.find (name); |
|
1772 |
|
1773 return p != table.end () && p->second.is_global (); |
|
1774 } |
3011
|
1775 }; |
2790
|
1776 |
7336
|
1777 extern bool out_of_date_check (octave_value& function); |
4913
|
1778 |
8
|
1779 #endif |
|
1780 |
|
1781 /* |
|
1782 ;;; Local Variables: *** |
|
1783 ;;; mode: C++ *** |
|
1784 ;;; End: *** |
|
1785 */ |