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