Mercurial > octave-nkf
comparison src/symtab.h @ 8881:8ed42c679af5
after defining a script or function, clear local variables created for parsing
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 26 Feb 2009 13:58:47 -0500 |
parents | 96d87674b818 |
children | 579de77acd90 |
comparison
equal
deleted
inserted
replaced
8880:078ca05e4ef8 | 8881:8ed42c679af5 |
---|---|
180 static const unsigned int global = 32; | 180 static const unsigned int global = 32; |
181 | 181 |
182 // not cleared at function exit | 182 // not cleared at function exit |
183 static const unsigned int persistent = 64; | 183 static const unsigned int persistent = 64; |
184 | 184 |
185 // temporary variables forced into symbol table for parsing | |
186 static const unsigned int forced = 128; | |
187 | |
185 private: | 188 private: |
186 | 189 |
187 class | 190 class |
188 symbol_record_rep | 191 symbol_record_rep |
189 { | 192 { |
192 symbol_record_rep (const std::string& nm, const octave_value& v, | 195 symbol_record_rep (const std::string& nm, const octave_value& v, |
193 unsigned int sc) | 196 unsigned int sc) |
194 : name (nm), value_stack (), storage_class (sc), count (1) | 197 : name (nm), value_stack (), storage_class (sc), count (1) |
195 { | 198 { |
196 value_stack.push_back (v); | 199 value_stack.push_back (v); |
200 } | |
201 | |
202 void force_variable (context_id context) | |
203 { | |
204 octave_value& val = varref (context); | |
205 | |
206 if (! val.is_defined ()) | |
207 { | |
208 val = Matrix (); | |
209 mark_forced (); | |
210 } | |
197 } | 211 } |
198 | 212 |
199 octave_value& varref (context_id context) | 213 octave_value& varref (context_id context) |
200 { | 214 { |
201 if (is_global ()) | 215 if (is_global ()) |
277 | 291 |
278 varref (xcurrent_context) = octave_value (); | 292 varref (xcurrent_context) = octave_value (); |
279 } | 293 } |
280 } | 294 } |
281 | 295 |
296 void clear_forced (void) | |
297 { | |
298 if (is_forced ()) | |
299 { | |
300 varref (xcurrent_context) = octave_value (); | |
301 unmark_forced (); | |
302 } | |
303 } | |
304 | |
282 bool is_defined (context_id context) const | 305 bool is_defined (context_id context) const |
283 { | 306 { |
284 return varval (context).is_defined (); | 307 return varval (context).is_defined (); |
285 } | 308 } |
286 | 309 |
294 bool is_formal (void) const { return storage_class & formal; } | 317 bool is_formal (void) const { return storage_class & formal; } |
295 bool is_hidden (void) const { return storage_class & hidden; } | 318 bool is_hidden (void) const { return storage_class & hidden; } |
296 bool is_inherited (void) const { return storage_class & inherited; } | 319 bool is_inherited (void) const { return storage_class & inherited; } |
297 bool is_global (void) const { return storage_class & global; } | 320 bool is_global (void) const { return storage_class & global; } |
298 bool is_persistent (void) const { return storage_class & persistent; } | 321 bool is_persistent (void) const { return storage_class & persistent; } |
322 bool is_forced (void) const { return storage_class & forced; } | |
299 | 323 |
300 void mark_local (void) { storage_class |= local; } | 324 void mark_local (void) { storage_class |= local; } |
301 void mark_automatic (void) { storage_class |= automatic; } | 325 void mark_automatic (void) { storage_class |= automatic; } |
302 void mark_formal (void) { storage_class |= formal; } | 326 void mark_formal (void) { storage_class |= formal; } |
303 void mark_hidden (void) { storage_class |= hidden; } | 327 void mark_hidden (void) { storage_class |= hidden; } |
314 if (is_global ()) | 338 if (is_global ()) |
315 error ("can't make global variable %s persistent", name.c_str ()); | 339 error ("can't make global variable %s persistent", name.c_str ()); |
316 else | 340 else |
317 storage_class |= persistent; | 341 storage_class |= persistent; |
318 } | 342 } |
343 void mark_forced (void) { storage_class |= forced; } | |
319 | 344 |
320 void unmark_local (void) { storage_class &= ~local; } | 345 void unmark_local (void) { storage_class &= ~local; } |
321 void unmark_automatic (void) { storage_class &= ~automatic; } | 346 void unmark_automatic (void) { storage_class &= ~automatic; } |
322 void unmark_formal (void) { storage_class &= ~formal; } | 347 void unmark_formal (void) { storage_class &= ~formal; } |
323 void unmark_hidden (void) { storage_class &= ~hidden; } | 348 void unmark_hidden (void) { storage_class &= ~hidden; } |
324 void unmark_inherited (void) { storage_class &= ~inherited; } | 349 void unmark_inherited (void) { storage_class &= ~inherited; } |
325 void unmark_global (void) { storage_class &= ~global; } | 350 void unmark_global (void) { storage_class &= ~global; } |
326 void unmark_persistent (void) { storage_class &= ~persistent; } | 351 void unmark_persistent (void) { storage_class &= ~persistent; } |
352 void unmark_forced (void) { storage_class &= ~forced; } | |
327 | 353 |
328 void init_persistent (void) | 354 void init_persistent (void) |
329 { | 355 { |
330 if (! is_defined (xcurrent_context)) | 356 if (! is_defined (xcurrent_context)) |
331 { | 357 { |
405 | 431 |
406 octave_value | 432 octave_value |
407 find (tree_argument_list *args, const string_vector& arg_names, | 433 find (tree_argument_list *args, const string_vector& arg_names, |
408 octave_value_list& evaluated_args, bool& args_evaluated) const; | 434 octave_value_list& evaluated_args, bool& args_evaluated) const; |
409 | 435 |
436 void force_variable (context_id context = xcurrent_context) | |
437 { | |
438 rep->force_variable (context); | |
439 } | |
440 | |
410 octave_value& varref (context_id context = xcurrent_context) | 441 octave_value& varref (context_id context = xcurrent_context) |
411 { | 442 { |
412 return rep->varref (context); | 443 return rep->varref (context); |
413 } | 444 } |
414 | 445 |
421 | 452 |
422 size_t pop_context (void) { return rep->pop_context (); } | 453 size_t pop_context (void) { return rep->pop_context (); } |
423 | 454 |
424 void clear (void) { rep->clear (); } | 455 void clear (void) { rep->clear (); } |
425 | 456 |
457 void clear_forced (void) { rep->clear_forced (); } | |
458 | |
426 bool is_defined (context_id context = xcurrent_context) const | 459 bool is_defined (context_id context = xcurrent_context) const |
427 { | 460 { |
428 return rep->is_defined (context); | 461 return rep->is_defined (context); |
429 } | 462 } |
430 | 463 |
438 bool is_formal (void) const { return rep->is_formal (); } | 471 bool is_formal (void) const { return rep->is_formal (); } |
439 bool is_global (void) const { return rep->is_global (); } | 472 bool is_global (void) const { return rep->is_global (); } |
440 bool is_hidden (void) const { return rep->is_hidden (); } | 473 bool is_hidden (void) const { return rep->is_hidden (); } |
441 bool is_inherited (void) const { return rep->is_inherited (); } | 474 bool is_inherited (void) const { return rep->is_inherited (); } |
442 bool is_persistent (void) const { return rep->is_persistent (); } | 475 bool is_persistent (void) const { return rep->is_persistent (); } |
476 bool is_forced (void) const { return rep->is_forced (); } | |
443 | 477 |
444 void mark_local (void) { rep->mark_local (); } | 478 void mark_local (void) { rep->mark_local (); } |
445 void mark_automatic (void) { rep->mark_automatic (); } | 479 void mark_automatic (void) { rep->mark_automatic (); } |
446 void mark_formal (void) { rep->mark_formal (); } | 480 void mark_formal (void) { rep->mark_formal (); } |
447 void mark_hidden (void) { rep->mark_hidden (); } | 481 void mark_hidden (void) { rep->mark_hidden (); } |
448 void mark_inherited (void) { rep->mark_inherited (); } | 482 void mark_inherited (void) { rep->mark_inherited (); } |
449 void mark_global (void) { rep->mark_global (); } | 483 void mark_global (void) { rep->mark_global (); } |
450 void mark_persistent (void) { rep->mark_persistent (); } | 484 void mark_persistent (void) { rep->mark_persistent (); } |
485 void mark_forced (void) { rep->mark_forced (); } | |
451 | 486 |
452 void unmark_local (void) { rep->unmark_local (); } | 487 void unmark_local (void) { rep->unmark_local (); } |
453 void unmark_automatic (void) { rep->unmark_automatic (); } | 488 void unmark_automatic (void) { rep->unmark_automatic (); } |
454 void unmark_formal (void) { rep->unmark_formal (); } | 489 void unmark_formal (void) { rep->unmark_formal (); } |
455 void unmark_hidden (void) { rep->unmark_hidden (); } | 490 void unmark_hidden (void) { rep->unmark_hidden (); } |
456 void unmark_inherited (void) { rep->unmark_inherited (); } | 491 void unmark_inherited (void) { rep->unmark_inherited (); } |
457 void unmark_global (void) { rep->unmark_global (); } | 492 void unmark_global (void) { rep->unmark_global (); } |
458 void unmark_persistent (void) { rep->unmark_persistent (); } | 493 void unmark_persistent (void) { rep->unmark_persistent (); } |
494 void unmark_forced (void) { rep->unmark_forced (); } | |
459 | 495 |
460 void init_persistent (void) { rep->init_persistent (); } | 496 void init_persistent (void) { rep->init_persistent (); } |
461 | 497 |
462 void erase_persistent (void) { rep->erase_persistent (); } | 498 void erase_persistent (void) { rep->erase_persistent (); } |
463 | 499 |
819 { | 855 { |
820 rep->install_built_in_function (f); | 856 rep->install_built_in_function (f); |
821 } | 857 } |
822 | 858 |
823 void clear (void) { rep->clear (); } | 859 void clear (void) { rep->clear (); } |
824 | 860 |
825 void clear_user_function (void) { rep->clear_user_function (); } | 861 void clear_user_function (void) { rep->clear_user_function (); } |
826 | 862 |
827 void clear_mex_function (void) { rep->clear_mex_function (); } | 863 void clear_mex_function (void) { rep->clear_mex_function (); } |
828 | 864 |
829 void add_dispatch (const std::string& type, const std::string& fname) | 865 void add_dispatch (const std::string& type, const std::string& fname) |
1049 symbol_table *inst = get_instance (xcurrent_scope); | 1085 symbol_table *inst = get_instance (xcurrent_scope); |
1050 | 1086 |
1051 return inst ? inst->do_insert (name) : foobar; | 1087 return inst ? inst->do_insert (name) : foobar; |
1052 } | 1088 } |
1053 | 1089 |
1090 static void force_variable (const std::string& name, | |
1091 scope_id scope = xcurrent_scope, | |
1092 context_id context = xcurrent_context) | |
1093 { | |
1094 assert (xcurrent_context == 0); | |
1095 | |
1096 symbol_table *inst = get_instance (scope); | |
1097 | |
1098 if (inst) | |
1099 inst->do_force_variable (name, context); | |
1100 } | |
1101 | |
1054 static octave_value& varref (const std::string& name, | 1102 static octave_value& varref (const std::string& name, |
1055 scope_id scope = xcurrent_scope, | 1103 scope_id scope = xcurrent_scope, |
1056 context_id context = xcurrent_context) | 1104 context_id context = xcurrent_context) |
1057 { | 1105 { |
1058 static octave_value foobar; | 1106 static octave_value foobar; |
1295 { | 1343 { |
1296 symbol_table *inst = get_instance (scope); | 1344 symbol_table *inst = get_instance (scope); |
1297 | 1345 |
1298 if (inst) | 1346 if (inst) |
1299 inst->do_clear_variables (); | 1347 inst->do_clear_variables (); |
1348 } | |
1349 | |
1350 static void clear_forced_variables (scope_id scope = xcurrent_scope) | |
1351 { | |
1352 symbol_table *inst = get_instance (scope); | |
1353 | |
1354 if (inst) | |
1355 inst->do_clear_forced_variables (); | |
1300 } | 1356 } |
1301 | 1357 |
1302 // For unwind_protect. | 1358 // For unwind_protect. |
1303 static void clear_variables (void *) { clear_variables (); } | 1359 static void clear_variables (void *) { clear_variables (); } |
1304 | 1360 |
1996 | 2052 |
1997 return p == table.end () | 2053 return p == table.end () |
1998 ? (table[name] = symbol_record (name)) : p->second; | 2054 ? (table[name] = symbol_record (name)) : p->second; |
1999 } | 2055 } |
2000 | 2056 |
2057 void do_force_variable (const std::string& name, context_id context) | |
2058 { | |
2059 table_iterator p = table.find (name); | |
2060 | |
2061 if (p == table.end ()) | |
2062 { | |
2063 symbol_record& sr = do_insert (name); | |
2064 | |
2065 sr.force_variable (context); | |
2066 } | |
2067 } | |
2068 | |
2001 octave_value& do_varref (const std::string& name, context_id context) | 2069 octave_value& do_varref (const std::string& name, context_id context) |
2002 { | 2070 { |
2003 table_iterator p = table.find (name); | 2071 table_iterator p = table.find (name); |
2004 | 2072 |
2005 if (p == table.end ()) | 2073 if (p == table.end ()) |
2077 | 2145 |
2078 void do_clear_variables (void) | 2146 void do_clear_variables (void) |
2079 { | 2147 { |
2080 for (table_iterator p = table.begin (); p != table.end (); p++) | 2148 for (table_iterator p = table.begin (); p != table.end (); p++) |
2081 p->second.clear (); | 2149 p->second.clear (); |
2150 } | |
2151 | |
2152 void do_clear_forced_variables (void) | |
2153 { | |
2154 for (table_iterator p = table.begin (); p != table.end (); p++) | |
2155 p->second.clear_forced (); | |
2082 } | 2156 } |
2083 | 2157 |
2084 void do_clear_global (const std::string& name) | 2158 void do_clear_global (const std::string& name) |
2085 { | 2159 { |
2086 table_iterator p = table.find (name); | 2160 table_iterator p = table.find (name); |