Mercurial > octave-nkf
comparison src/variables.cc @ 2856:8c516da3c1f7
[project @ 1997-03-31 06:37:21 by jwe]
author | jwe |
---|---|
date | Mon, 31 Mar 1997 06:39:28 +0000 |
parents | 5338beb20eb9 |
children | 00c0cd0f2ee7 |
comparison
equal
deleted
inserted
replaced
2855:1bb7dc230a4b | 2856:8c516da3c1f7 |
---|---|
191 // Is this variable a builtin? | 191 // Is this variable a builtin? |
192 | 192 |
193 bool | 193 bool |
194 is_builtin_variable (const string& name) | 194 is_builtin_variable (const string& name) |
195 { | 195 { |
196 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); | 196 symbol_record *sr = global_sym_tab->lookup (name); |
197 return (sr && sr->is_builtin_variable ()); | 197 return (sr && sr->is_builtin_variable ()); |
198 } | 198 } |
199 | 199 |
200 // Is this a text-style function? | 200 // Is this a text-style function? |
201 | 201 |
227 // Is this function globally in this scope? | 227 // Is this function globally in this scope? |
228 | 228 |
229 bool | 229 bool |
230 is_globally_visible (const string& name) | 230 is_globally_visible (const string& name) |
231 { | 231 { |
232 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); | 232 symbol_record *sr = curr_sym_tab->lookup (name); |
233 return (sr && sr->is_linked_to_global ()); | 233 return (sr && sr->is_linked_to_global ()); |
234 } | 234 } |
235 | 235 |
236 // Is this octave_value a valid function? | 236 // Is this octave_value a valid function? |
237 | 237 |
238 tree_fvc * | 238 tree_fvc * |
239 is_valid_function (const octave_value& arg, const string& warn_for, int warn) | 239 is_valid_function (const octave_value& arg, const string& warn_for, bool warn) |
240 { | 240 { |
241 tree_fvc *ans = 0; | 241 tree_fvc *ans = 0; |
242 | 242 |
243 string fcn_name; | 243 string fcn_name; |
244 | 244 |
339 { | 339 { |
340 error ("is_global: expecting string argument"); | 340 error ("is_global: expecting string argument"); |
341 return retval; | 341 return retval; |
342 } | 342 } |
343 | 343 |
344 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); | 344 symbol_record *sr = curr_sym_tab->lookup (name); |
345 | 345 |
346 retval = static_cast<double> (sr && sr->is_linked_to_global ()); | 346 retval = static_cast<double> (sr && sr->is_linked_to_global ()); |
347 | 347 |
348 return retval; | 348 return retval; |
349 } | 349 } |
386 { | 386 { |
387 struct_elts = name.substr (pos+1); | 387 struct_elts = name.substr (pos+1); |
388 symbol_name = name.substr (0, pos); | 388 symbol_name = name.substr (0, pos); |
389 } | 389 } |
390 | 390 |
391 symbol_record *sr = curr_sym_tab->lookup (symbol_name, 0, 0); | 391 symbol_record *sr = curr_sym_tab->lookup (symbol_name); |
392 if (! sr) | 392 if (! sr) |
393 sr = global_sym_tab->lookup (symbol_name, 0, 0); | 393 sr = global_sym_tab->lookup (symbol_name); |
394 | 394 |
395 retval = 0.0; | 395 retval = 0.0; |
396 | 396 |
397 if (sr && sr->is_variable () && sr->is_defined ()) | 397 if (sr && sr->is_variable () && sr->is_defined ()) |
398 { | 398 { |
644 if (f) | 644 if (f) |
645 fclose (static_cast<FILE *> (f)); | 645 fclose (static_cast<FILE *> (f)); |
646 } | 646 } |
647 | 647 |
648 static int | 648 static int |
649 parse_fcn_file (int exec_script, const string& ff) | 649 parse_fcn_file (bool exec_script, const string& ff) |
650 { | 650 { |
651 begin_unwind_frame ("parse_fcn_file"); | 651 begin_unwind_frame ("parse_fcn_file"); |
652 | 652 |
653 int script_file_executed = 0; | 653 int script_file_executed = 0; |
654 | 654 |
752 | 752 |
753 return script_file_executed; | 753 return script_file_executed; |
754 } | 754 } |
755 | 755 |
756 static bool | 756 static bool |
757 load_fcn_from_file (symbol_record *sym_rec, int exec_script) | 757 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) |
758 { | 758 { |
759 bool script_file_executed = false; | 759 bool script_file_executed = false; |
760 | 760 |
761 string nm = sym_rec->name (); | 761 string nm = sym_rec->name (); |
762 | 762 |
789 | 789 |
790 return script_file_executed; | 790 return script_file_executed; |
791 } | 791 } |
792 | 792 |
793 bool | 793 bool |
794 lookup (symbol_record *sym_rec, int exec_script) | 794 lookup (symbol_record *sym_rec, bool exec_script) |
795 { | 795 { |
796 bool script_executed = false; | 796 bool script_executed = false; |
797 | 797 |
798 if (! sym_rec->is_linked_to_global ()) | 798 if (! sym_rec->is_linked_to_global ()) |
799 { | 799 { |
821 // out of date. If a function is available in a file but is not | 821 // out of date. If a function is available in a file but is not |
822 // currently loaded, this will load it and insert the name in the | 822 // currently loaded, this will load it and insert the name in the |
823 // current symbol table. | 823 // current symbol table. |
824 | 824 |
825 symbol_record * | 825 symbol_record * |
826 lookup_by_name (const string& nm, int exec_script) | 826 lookup_by_name (const string& nm, bool exec_script) |
827 { | 827 { |
828 symbol_record *sym_rec = curr_sym_tab->lookup (nm, 1, 0); | 828 symbol_record *sym_rec = curr_sym_tab->lookup (nm, true); |
829 | 829 |
830 lookup (sym_rec, exec_script); | 830 lookup (sym_rec, exec_script); |
831 | 831 |
832 return sym_rec; | 832 return sym_rec; |
833 } | 833 } |
857 } | 857 } |
858 | 858 |
859 void | 859 void |
860 set_global_value (const string& nm, const octave_value& val) | 860 set_global_value (const string& nm, const octave_value& val) |
861 { | 861 { |
862 symbol_record *sr = global_sym_tab->lookup (nm, 1); | 862 symbol_record *sr = global_sym_tab->lookup (nm, true); |
863 | 863 |
864 if (sr) | 864 if (sr) |
865 sr->define (val); | 865 sr->define (val); |
866 else | 866 else |
867 panic_impossible (); | 867 panic_impossible (); |
895 // to a string, return a new copy. If not, return 0; | 895 // to a string, return a new copy. If not, return 0; |
896 | 896 |
897 string | 897 string |
898 builtin_string_variable (const string& name) | 898 builtin_string_variable (const string& name) |
899 { | 899 { |
900 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); | 900 symbol_record *sr = global_sym_tab->lookup (name); |
901 | 901 |
902 // It is a prorgramming error to look for builtins that aren't. | 902 // It is a prorgramming error to look for builtins that aren't. |
903 | 903 |
904 assert (sr); | 904 assert (sr); |
905 | 905 |
924 | 924 |
925 int | 925 int |
926 builtin_real_scalar_variable (const string& name, double& d) | 926 builtin_real_scalar_variable (const string& name, double& d) |
927 { | 927 { |
928 int status = 0; | 928 int status = 0; |
929 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); | 929 symbol_record *sr = global_sym_tab->lookup (name); |
930 | 930 |
931 // It is a prorgramming error to look for builtins that aren't. | 931 // It is a prorgramming error to look for builtins that aren't. |
932 | 932 |
933 assert (sr); | 933 assert (sr); |
934 | 934 |
953 octave_value | 953 octave_value |
954 builtin_any_variable (const string& name) | 954 builtin_any_variable (const string& name) |
955 { | 955 { |
956 octave_value retval; | 956 octave_value retval; |
957 | 957 |
958 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); | 958 symbol_record *sr = global_sym_tab->lookup (name); |
959 | 959 |
960 // It is a prorgramming error to look for builtins that aren't. | 960 // It is a prorgramming error to look for builtins that aren't. |
961 | 961 |
962 assert (sr); | 962 assert (sr); |
963 | 963 |
981 if (sr->is_linked_to_global ()) | 981 if (sr->is_linked_to_global ()) |
982 return; | 982 return; |
983 | 983 |
984 string nm = sr->name (); | 984 string nm = sr->name (); |
985 | 985 |
986 symbol_record *gsr = global_sym_tab->lookup (nm, 1, 0); | 986 symbol_record *gsr = global_sym_tab->lookup (nm, true); |
987 | 987 |
988 if (sr->is_formal_parameter ()) | 988 if (sr->is_formal_parameter ()) |
989 { | 989 { |
990 error ("can't make function parameter `%s' global", nm.c_str ()); | 990 error ("can't make function parameter `%s' global", nm.c_str ()); |
991 return; | 991 return; |
1027 // definition of the builtin variable of the same name. | 1027 // definition of the builtin variable of the same name. |
1028 | 1028 |
1029 void | 1029 void |
1030 link_to_builtin_variable (symbol_record *sr) | 1030 link_to_builtin_variable (symbol_record *sr) |
1031 { | 1031 { |
1032 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); | 1032 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name ()); |
1033 | 1033 |
1034 if (tmp_sym && tmp_sym->is_builtin_variable ()) | 1034 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
1035 sr->alias (tmp_sym); | 1035 sr->alias (tmp_sym); |
1036 } | 1036 } |
1037 | 1037 |
1041 // parameter. | 1041 // parameter. |
1042 | 1042 |
1043 void | 1043 void |
1044 link_to_builtin_or_function (symbol_record *sr) | 1044 link_to_builtin_or_function (symbol_record *sr) |
1045 { | 1045 { |
1046 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); | 1046 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name ()); |
1047 | 1047 |
1048 if (tmp_sym | 1048 if (tmp_sym |
1049 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) | 1049 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
1050 && ! tmp_sym->is_formal_parameter ()) | 1050 && ! tmp_sym->is_formal_parameter ()) |
1051 sr->alias (tmp_sym); | 1051 sr->alias (tmp_sym); |
1060 // given name defined in the global symbol table. | 1060 // given name defined in the global symbol table. |
1061 | 1061 |
1062 void | 1062 void |
1063 force_link_to_function (const string& id_name) | 1063 force_link_to_function (const string& id_name) |
1064 { | 1064 { |
1065 symbol_record *gsr = global_sym_tab->lookup (id_name, 1, 0); | 1065 symbol_record *gsr = global_sym_tab->lookup (id_name, true); |
1066 if (gsr->is_function ()) | 1066 if (gsr->is_function ()) |
1067 { | 1067 { |
1068 curr_sym_tab->clear (id_name); | 1068 curr_sym_tab->clear (id_name); |
1069 symbol_record *csr = curr_sym_tab->lookup (id_name, 1, 0); | 1069 symbol_record *csr = curr_sym_tab->lookup (id_name, true); |
1070 csr->alias (gsr); | 1070 csr->alias (gsr); |
1071 } | 1071 } |
1072 } | 1072 } |
1073 | 1073 |
1074 // Help stuff. Shouldn't this go in help.cc? | 1074 // Help stuff. Shouldn't this go in help.cc? |
1174 } | 1174 } |
1175 } | 1175 } |
1176 | 1176 |
1177 static int | 1177 static int |
1178 maybe_list (const char *header, const string_vector& argv, int argc, | 1178 maybe_list (const char *header, const string_vector& argv, int argc, |
1179 ostream& os, int show_verbose, symbol_table | 1179 ostream& os, bool show_verbose, symbol_table |
1180 *sym_tab, unsigned type, unsigned scope) | 1180 *sym_tab, unsigned type, unsigned scope) |
1181 { | 1181 { |
1182 int count; | 1182 int count; |
1183 int status = 0; | 1183 int status = 0; |
1184 if (show_verbose) | 1184 if (show_verbose) |
1234 || is_mapper_function_name (name) | 1234 || is_mapper_function_name (name) |
1235 || is_builtin_function_name (name)) | 1235 || is_builtin_function_name (name)) |
1236 error ("document: can't redefine help for built-in variables and functions"); | 1236 error ("document: can't redefine help for built-in variables and functions"); |
1237 else | 1237 else |
1238 { | 1238 { |
1239 symbol_record *sym_rec = curr_sym_tab->lookup (name, 0); | 1239 symbol_record *sym_rec = curr_sym_tab->lookup (name); |
1240 | 1240 |
1241 if (sym_rec) | 1241 if (sym_rec) |
1242 sym_rec->document (help); | 1242 sym_rec->document (help); |
1243 else | 1243 else |
1244 error ("document: no such symbol `%s'", name.c_str ()); | 1244 error ("document: no such symbol `%s'", name.c_str ()); |
1258 static octave_value_list | 1258 static octave_value_list |
1259 do_who (int argc, const string_vector& argv) | 1259 do_who (int argc, const string_vector& argv) |
1260 { | 1260 { |
1261 octave_value_list retval; | 1261 octave_value_list retval; |
1262 | 1262 |
1263 int show_builtins = 0; | 1263 bool show_builtins = false; |
1264 int show_functions = (curr_sym_tab == top_level_sym_tab); | 1264 bool show_functions = (curr_sym_tab == top_level_sym_tab); |
1265 int show_variables = 1; | 1265 bool show_variables = true; |
1266 int show_verbose = 0; | 1266 bool show_verbose = false; |
1267 | 1267 |
1268 string my_name = argv[0]; | 1268 string my_name = argv[0]; |
1269 | 1269 |
1270 if (argc > 1) | 1270 if (argc > 1) |
1271 { | 1271 { |
1272 show_functions = 0; | 1272 show_functions = false; |
1273 show_variables = 0; | 1273 show_variables = false; |
1274 } | 1274 } |
1275 | 1275 |
1276 int i; | 1276 int i; |
1277 for (i = 1; i < argc; i++) | 1277 for (i = 1; i < argc; i++) |
1278 { | 1278 { |
1279 if (argv[i] == "-all" || argv[i] == "-a") | 1279 if (argv[i] == "-all" || argv[i] == "-a") |
1280 { | 1280 { |
1281 show_builtins++; | 1281 show_builtins = true; |
1282 show_functions++; | 1282 show_functions = true; |
1283 show_variables++; | 1283 show_variables = true; |
1284 } | 1284 } |
1285 else if (argv[i] == "-builtins" || argv[i] == "-b") | 1285 else if (argv[i] == "-builtins" || argv[i] == "-b") |
1286 show_builtins++; | 1286 show_builtins = true; |
1287 else if (argv[i] == "-functions" || argv[i] == "-f") | 1287 else if (argv[i] == "-functions" || argv[i] == "-f") |
1288 show_functions++; | 1288 show_functions = true; |
1289 else if (argv[i] == "-long" || argv[i] == "-l") | 1289 else if (argv[i] == "-long" || argv[i] == "-l") |
1290 show_verbose++; | 1290 show_verbose = true; |
1291 else if (argv[i] == "-variables" || argv[i] == "-v") | 1291 else if (argv[i] == "-variables" || argv[i] == "-v") |
1292 show_variables++; | 1292 show_variables = true; |
1293 else if (argv[i][0] == '-') | 1293 else if (argv[i][0] == '-') |
1294 warning ("%s: unrecognized option `%s'", my_name.c_str (), | 1294 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
1295 argv[i].c_str ()); | 1295 argv[i].c_str ()); |
1296 else | 1296 else |
1297 break; | 1297 break; |
1403 // Install variables and functions in the symbol tables. | 1403 // Install variables and functions in the symbol tables. |
1404 | 1404 |
1405 void | 1405 void |
1406 install_builtin_mapper (const builtin_mapper_function& mf) | 1406 install_builtin_mapper (const builtin_mapper_function& mf) |
1407 { | 1407 { |
1408 symbol_record *sym_rec = global_sym_tab->lookup (mf.name, 1); | 1408 symbol_record *sym_rec = global_sym_tab->lookup (mf.name, true); |
1409 sym_rec->unprotect (); | 1409 sym_rec->unprotect (); |
1410 | 1410 |
1411 tree_builtin *def = new tree_builtin (mf, mf.name); | 1411 tree_builtin *def = new tree_builtin (mf, mf.name); |
1412 | 1412 |
1413 sym_rec->define (def); | 1413 sym_rec->define (def); |
1418 } | 1418 } |
1419 | 1419 |
1420 void | 1420 void |
1421 install_builtin_function (const builtin_function& f) | 1421 install_builtin_function (const builtin_function& f) |
1422 { | 1422 { |
1423 symbol_record *sym_rec = global_sym_tab->lookup (f.name, 1); | 1423 symbol_record *sym_rec = global_sym_tab->lookup (f.name, true); |
1424 sym_rec->unprotect (); | 1424 sym_rec->unprotect (); |
1425 | 1425 |
1426 tree_builtin *def = new tree_builtin (f.fcn, f.name); | 1426 tree_builtin *def = new tree_builtin (f.fcn, f.name); |
1427 | 1427 |
1428 sym_rec->define (def, f.is_text_fcn); | 1428 sym_rec->define (def, f.is_text_fcn); |
1444 } | 1444 } |
1445 | 1445 |
1446 void | 1446 void |
1447 install_builtin_variable_as_function (const string& name, | 1447 install_builtin_variable_as_function (const string& name, |
1448 const octave_value& val, | 1448 const octave_value& val, |
1449 int protect, int eternal, | 1449 bool protect, bool eternal, |
1450 const string& help) | 1450 const string& help) |
1451 { | 1451 { |
1452 symbol_record *sym_rec = global_sym_tab->lookup (name, 1); | 1452 symbol_record *sym_rec = global_sym_tab->lookup (name, true); |
1453 sym_rec->unprotect (); | 1453 sym_rec->unprotect (); |
1454 | 1454 |
1455 string tmp_help = help.empty () ? sym_rec->help () : help; | 1455 string tmp_help = help.empty () ? sym_rec->help () : help; |
1456 | 1456 |
1457 sym_rec->define_as_fcn (val); | 1457 sym_rec->define_as_fcn (val); |
1466 } | 1466 } |
1467 | 1467 |
1468 void | 1468 void |
1469 alias_builtin (const string& alias, const string& name) | 1469 alias_builtin (const string& alias, const string& name) |
1470 { | 1470 { |
1471 symbol_record *sr_name = global_sym_tab->lookup (name, 0, 0); | 1471 symbol_record *sr_name = global_sym_tab->lookup (name); |
1472 | 1472 |
1473 if (! sr_name) | 1473 if (! sr_name) |
1474 panic ("can't alias to undefined name!"); | 1474 panic ("can't alias to undefined name!"); |
1475 | 1475 |
1476 symbol_record *sr_alias = global_sym_tab->lookup (alias, 1, 0); | 1476 symbol_record *sr_alias = global_sym_tab->lookup (alias, true); |
1477 | 1477 |
1478 if (sr_alias) | 1478 if (sr_alias) |
1479 sr_alias->alias (sr_name); | 1479 sr_alias->alias (sr_name); |
1480 else | 1480 else |
1481 panic ("can't find symbol record for builtin function `%s'", | 1481 panic ("can't find symbol record for builtin function `%s'", |
1483 } | 1483 } |
1484 | 1484 |
1485 // Defining variables. | 1485 // Defining variables. |
1486 | 1486 |
1487 void | 1487 void |
1488 bind_ans (const octave_value& val, int print) | 1488 bind_ans (const octave_value& val, bool print) |
1489 { | 1489 { |
1490 static symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); | 1490 static symbol_record *sr = global_sym_tab->lookup ("ans", true); |
1491 | 1491 |
1492 tree_identifier *ans_id = new tree_identifier (sr); | 1492 tree_identifier *ans_id = new tree_identifier (sr); |
1493 tree_constant *tmp = new tree_constant (val); | 1493 tree_constant *tmp = new tree_constant (val); |
1494 | 1494 |
1495 // XXX FIXME XXX -- making ans_id static, passing its address to | 1495 // XXX FIXME XXX -- making ans_id static, passing its address to |
1532 // How is this different than install_builtin_variable? Are both | 1532 // How is this different than install_builtin_variable? Are both |
1533 // functions needed? | 1533 // functions needed? |
1534 | 1534 |
1535 void | 1535 void |
1536 bind_builtin_variable (const string& varname, const octave_value& val, | 1536 bind_builtin_variable (const string& varname, const octave_value& val, |
1537 int protect, int eternal, sv_Function sv_fcn, | 1537 bool protect, bool eternal, sv_Function sv_fcn, |
1538 const string& help) | 1538 const string& help) |
1539 { | 1539 { |
1540 symbol_record *sr = global_sym_tab->lookup (varname, 1, 0); | 1540 symbol_record *sr = global_sym_tab->lookup (varname, true); |
1541 | 1541 |
1542 // It is a programming error for a builtin symbol to be missing. | 1542 // It is a programming error for a builtin symbol to be missing. |
1543 // Besides, we just inserted it, so it must be there. | 1543 // Besides, we just inserted it, so it must be there. |
1544 | 1544 |
1545 assert (sr); | 1545 assert (sr); |
1738 | 1738 |
1739 // Always clear the local table, but don't clear currently compiled | 1739 // Always clear the local table, but don't clear currently compiled |
1740 // functions unless we are at the top level. (Allowing that to | 1740 // functions unless we are at the top level. (Allowing that to |
1741 // happen inside functions would result in pretty odd behavior...) | 1741 // happen inside functions would result in pretty odd behavior...) |
1742 | 1742 |
1743 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); | 1743 bool clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
1744 | 1744 |
1745 if (argc == 1) | 1745 if (argc == 1) |
1746 { | 1746 { |
1747 curr_sym_tab->clear (); | 1747 curr_sym_tab->clear (); |
1748 global_sym_tab->clear (clear_user_functions); | 1748 global_sym_tab->clear (clear_user_functions); |