comparison src/variables.cc @ 2975:b9c107cbe388

[project @ 1997-05-15 20:01:00 by jwe]
author jwe
date Thu, 15 May 1997 20:02:55 +0000
parents c0c280cda856
children 49de01238638
comparison
equal deleted inserted replaced
2974:ebbc34ff7f66 2975:b9c107cbe388
61 #include "input.h" 61 #include "input.h"
62 #include "lex.h" 62 #include "lex.h"
63 #include "sysdep.h" 63 #include "sysdep.h"
64 #include "oct-hist.h" 64 #include "oct-hist.h"
65 #include "oct-map.h" 65 #include "oct-map.h"
66 #include "oct-mapper.h" 66 #include "ov-mapper.h"
67 #include "oct-obj.h" 67 #include "oct-obj.h"
68 #include "oct-sym.h"
69 #include "ov.h" 68 #include "ov.h"
70 #include "pager.h" 69 #include "pager.h"
71 #include "parse.h" 70 #include "parse.h"
72 #include "pt-id.h" 71 #include "pt-id.h"
73 #include "pt-indir.h" 72 #include "pt-indir.h"
173 return (sr && sr->is_linked_to_global ()); 172 return (sr && sr->is_linked_to_global ());
174 } 173 }
175 174
176 // Is this octave_value a valid function? 175 // Is this octave_value a valid function?
177 176
178 octave_symbol * 177 octave_function *
179 is_valid_function (const octave_value& arg, const string& warn_for, bool warn) 178 is_valid_function (const octave_value& arg, const string& warn_for, bool warn)
180 { 179 {
181 octave_symbol *ans = 0; 180 octave_function *ans = 0;
182 181
183 string fcn_name; 182 string fcn_name;
184 183
185 if (arg.is_string ()) 184 if (arg.is_string ())
186 fcn_name = arg.string_value (); 185 fcn_name = arg.string_value ();
197 196
198 if (! fcn_name.empty ()) 197 if (! fcn_name.empty ())
199 sr = lookup_by_name (fcn_name); 198 sr = lookup_by_name (fcn_name);
200 199
201 if (sr) 200 if (sr)
202 ans = sr->def (); 201 {
202 octave_value tmp = sr->def ();
203 ans = tmp.function_value (true);
204 }
203 205
204 if (! sr || ! ans || ! sr->is_function ()) 206 if (! sr || ! ans || ! sr->is_function ())
205 { 207 {
206 if (warn) 208 if (warn)
207 error ("%s: the symbol `%s' is not valid as a function", 209 error ("%s: the symbol `%s' is not valid as a function",
210 } 212 }
211 213
212 return ans; 214 return ans;
213 } 215 }
214 216
215 octave_symbol * 217 octave_function *
216 extract_function (const octave_value& arg, const string& warn_for, 218 extract_function (const octave_value& arg, const string& warn_for,
217 const string& fname, const string& header, 219 const string& fname, const string& header,
218 const string& trailer) 220 const string& trailer)
219 { 221 {
220 octave_symbol *retval = 0; 222 octave_function *retval = 0;
221 223
222 retval = is_valid_function (arg, warn_for, 0); 224 retval = is_valid_function (arg, warn_for, 0);
223 225
224 if (! retval) 226 if (! retval)
225 { 227 {
330 if (! sr) 332 if (! sr)
331 sr = global_sym_tab->lookup (id); 333 sr = global_sym_tab->lookup (id);
332 334
333 if (sr && sr->is_defined ()) 335 if (sr && sr->is_defined ())
334 { 336 {
335 octave_symbol *tmp = sr->def (); 337 octave_value tmp = sr->def ();
336
337 octave_value vtmp;
338
339 if (tmp->is_constant ())
340 vtmp = tmp->eval ();
341 338
342 // XXX FIXME XXX -- make this work for all types that can do 339 // XXX FIXME XXX -- make this work for all types that can do
343 // structure reference operations. 340 // structure reference operations.
344 if (vtmp.is_map ()) 341 if (tmp.is_map ())
345 { 342 {
346 for (int i = 1; i < elts.length (); i++) 343 for (int i = 1; i < elts.length (); i++)
347 { 344 {
348 vtmp = vtmp.do_struct_elt_index_op (elts[i], true); 345 tmp = tmp.do_struct_elt_index_op (elts[i], true);
349 346
350 if (! vtmp.is_map ()) 347 if (! tmp.is_map ())
351 break; 348 break;
352 } 349 }
353 350
354 if (vtmp.is_map ()) 351 if (tmp.is_map ())
355 { 352 {
356 Octave_map m = vtmp.map_value (); 353 Octave_map m = tmp.map_value ();
357 354
358 names = m.make_name_list (); 355 names = m.make_name_list ();
359 } 356 }
360 } 357 }
361 } 358 }
377 if (! sr) 374 if (! sr)
378 sr = global_sym_tab->lookup (id); 375 sr = global_sym_tab->lookup (id);
379 376
380 if (sr && sr->is_defined ()) 377 if (sr && sr->is_defined ())
381 { 378 {
382 octave_symbol *tmp = sr->def (); 379 octave_value tmp = sr->def ();
383
384 octave_value vtmp;
385
386 if (tmp->is_constant ())
387 vtmp = tmp->eval ();
388 380
389 // XXX FIXME XXX -- should this work for all types that can do 381 // XXX FIXME XXX -- should this work for all types that can do
390 // structure reference operations? 382 // structure reference operations?
391 383
392 if (vtmp.is_map ()) 384 if (tmp.is_map ())
393 { 385 {
394 for (int i = 1; i < elts.length (); i++) 386 for (int i = 1; i < elts.length (); i++)
395 { 387 {
396 vtmp = vtmp.do_struct_elt_index_op (elts[i], true); 388 tmp = tmp.do_struct_elt_index_op (elts[i], true);
397 389
398 if (! vtmp.is_map ()) 390 if (! tmp.is_map ())
399 { 391 {
400 retval = false; 392 retval = false;
401 break; 393 break;
402 } 394 }
403 } 395 }
536 { 528 {
537 bool retval = false; 529 bool retval = false;
538 530
539 if (Vignore_function_time_stamp != 2 && sr) 531 if (Vignore_function_time_stamp != 2 && sr)
540 { 532 {
541 octave_symbol *ans = sr->def (); 533 octave_value ans = sr->def ();
542 if (ans) 534
543 { 535 if (! Vignore_function_time_stamp && ans.is_defined ())
544 string ff = ans->fcn_file_name (); 536 {
545 if (! ff.empty () 537 octave_function *tmp = ans.function_value (true);
546 && ! (Vignore_function_time_stamp 538
547 && ans->is_system_fcn_file ())) 539 if (tmp && tmp->is_system_fcn_file ())
548 { 540 {
549 time_t tp = ans->time_parsed (); 541 string ff = tmp->fcn_file_name ();
550 542
551 string fname = fcn_file_in_path (ff); 543 if (! ff.empty ())
552 544 {
553 int status = file_stat::is_newer (fname, tp); 545 time_t tp = tmp->time_parsed ();
554 546
555 if (status > 0) 547 string fname = fcn_file_in_path (ff);
556 retval = true; 548
549 int status = file_stat::is_newer (fname, tp);
550
551 if (status > 0)
552 retval = true;
553 }
557 } 554 }
558 } 555 }
559 } 556 }
560 557
561 return retval; 558 return retval;
943 940
944 symbol_record *sr = global_sym_tab->lookup (nm); 941 symbol_record *sr = global_sym_tab->lookup (nm);
945 942
946 if (sr) 943 if (sr)
947 { 944 {
948 octave_symbol *sr_def = sr->def (); 945 octave_value sr_def = sr->def ();
949 946
950 if (sr_def) 947 if (sr_def.is_undefined ())
951 retval = sr_def->eval ();
952 else
953 error ("get_global_by_name: undefined symbol `%s'", nm.c_str ()); 948 error ("get_global_by_name: undefined symbol `%s'", nm.c_str ());
954 } 949 }
955 else 950 else
956 error ("get_global_by_name: unknown symbol `%s'", nm.c_str ()); 951 error ("get_global_by_name: unknown symbol `%s'", nm.c_str ());
957 952
1005 1000
1006 assert (sr); 1001 assert (sr);
1007 1002
1008 string retval; 1003 string retval;
1009 1004
1010 octave_symbol *defn = sr->def (); 1005 octave_value val = sr->def ();
1011 1006
1012 if (defn) 1007 if (! error_state && val.is_string ())
1013 { 1008 retval = val.string_value ();
1014 octave_value val = defn->eval ();
1015
1016 if (! error_state && val.is_string ())
1017 retval = val.string_value ();
1018 }
1019 1009
1020 return retval; 1010 return retval;
1021 } 1011 }
1022 1012
1023 // Look for the given name in the global symbol table. If it refers 1013 // Look for the given name in the global symbol table. If it refers
1032 1022
1033 // It is a prorgramming error to look for builtins that aren't. 1023 // It is a prorgramming error to look for builtins that aren't.
1034 1024
1035 assert (sr); 1025 assert (sr);
1036 1026
1037 octave_symbol *defn = sr->def (); 1027 octave_value val = sr->def ();
1038 1028
1039 if (defn) 1029 if (! error_state && val.is_scalar_type ())
1040 { 1030 {
1041 octave_value val = defn->eval (); 1031 d = val.double_value ();
1042 1032 status = 1;
1043 if (! error_state && val.is_scalar_type ())
1044 {
1045 d = val.double_value ();
1046 status = 1;
1047 }
1048 } 1033 }
1049 1034
1050 return status; 1035 return status;
1051 } 1036 }
1052 1037
1053 // Look for the given name in the global symbol table. 1038 // Look for the given name in the global symbol table.
1054 1039
1055 octave_value 1040 octave_value
1056 builtin_any_variable (const string& name) 1041 builtin_any_variable (const string& name)
1057 { 1042 {
1058 octave_value retval;
1059
1060 symbol_record *sr = global_sym_tab->lookup (name); 1043 symbol_record *sr = global_sym_tab->lookup (name);
1061 1044
1062 // It is a prorgramming error to look for builtins that aren't. 1045 // It is a prorgramming error to look for builtins that aren't.
1063 1046
1064 assert (sr); 1047 assert (sr);
1065 1048
1066 octave_symbol *defn = sr->def (); 1049 return sr->def ();
1067
1068 if (defn)
1069 retval = defn->eval ();
1070
1071 return retval;
1072 } 1050 }
1073 1051
1074 // Global stuff and links to builtin variables and functions. 1052 // Global stuff and links to builtin variables and functions.
1075 1053
1076 // Make the definition of the symbol record sr be the same as the 1054 // Make the definition of the symbol record sr be the same as the
1078 // it doesn't already exist. 1056 // it doesn't already exist.
1079 1057
1080 void 1058 void
1081 link_to_global_variable (symbol_record *sr) 1059 link_to_global_variable (symbol_record *sr)
1082 { 1060 {
1083 if (sr->is_linked_to_global ()) 1061 if (! sr->is_linked_to_global ())
1084 return; 1062 {
1085 1063 sr->mark_as_linked_to_global ();
1086 string nm = sr->name (); 1064
1087 1065 if (! error_state)
1088 symbol_record *gsr = global_sym_tab->lookup (nm, true); 1066 {
1089 1067 string nm = sr->name ();
1090 if (sr->is_formal_parameter ()) 1068
1091 { 1069 symbol_record *gsr = global_sym_tab->lookup (nm, true);
1092 error ("can't make function parameter `%s' global", nm.c_str ()); 1070
1093 return; 1071 // There must be a better way to do this. XXX FIXME XXX
1094 } 1072
1095 1073 if (sr->is_variable ())
1096 if (sr->is_static ()) 1074 gsr->define (sr->def ());
1097 { 1075 else
1098 error ("can't make static variable `%s' global", nm.c_str ()); 1076 sr->clear ();
1099 return; 1077
1100 } 1078 // Make sure this symbol is a variable.
1101 1079
1102 // There must be a better way to do this. XXX FIXME XXX 1080 if (! gsr->is_variable ())
1103 1081 gsr->define (octave_value ());
1104 if (sr->is_variable ()) 1082
1105 { 1083 sr->alias (gsr, 1);
1106 octave_symbol *tmp = sr->def (); 1084 }
1107 1085 }
1108 octave_value vtmp;
1109
1110 if (tmp)
1111 vtmp = tmp->eval ();
1112
1113 gsr->define (vtmp);
1114 }
1115 else
1116 sr->clear ();
1117
1118 // If the global symbol is currently defined as a function, we need
1119 // to hide it with a variable.
1120
1121 if (gsr->is_function ())
1122 gsr->define (octave_value ());
1123
1124 sr->alias (gsr, 1);
1125 sr->mark_as_linked_to_global ();
1126 } 1086 }
1127 1087
1128 // Make the definition of the symbol record sr be the same as the 1088 // Make the definition of the symbol record sr be the same as the
1129 // definition of the builtin variable of the same name. 1089 // definition of the builtin variable of the same name.
1130 1090
1500 retval = do_who (argc, argv); 1460 retval = do_who (argc, argv);
1501 1461
1502 return retval; 1462 return retval;
1503 } 1463 }
1504 1464
1505 // Install variables and functions in the symbol tables.
1506
1507 void
1508 install_builtin_mapper (octave_mapper *mf)
1509 {
1510 symbol_record *sym_rec = global_sym_tab->lookup (mf->name (), true);
1511
1512 unsigned int t
1513 = symbol_def::BUILTIN_FUNCTION | symbol_def::MAPPER_FUNCTION;
1514
1515 sym_rec->unprotect ();
1516 sym_rec->define (mf, t);
1517 sym_rec->document (mf->doc_string ());
1518 sym_rec->make_eternal ();
1519 sym_rec->protect ();
1520 }
1521
1522 void
1523 install_builtin_function (octave_builtin *f, bool is_text_fcn)
1524 {
1525 symbol_record *sym_rec = global_sym_tab->lookup (f->name (), true);
1526
1527 unsigned int t
1528 = symbol_def::BUILTIN_FUNCTION | symbol_def::MAPPER_FUNCTION;
1529
1530 if (is_text_fcn)
1531 t |= symbol_def::TEXT_FUNCTION;
1532
1533 sym_rec->unprotect ();
1534 sym_rec->define (f, t);
1535 sym_rec->document (f->doc_string ());
1536 sym_rec->make_eternal ();
1537 sym_rec->protect ();
1538 }
1539
1540 void
1541 install_builtin_variable (const string& name, const octave_value& value,
1542 bool install_as_function, bool protect,
1543 bool eternal, symbol_record::sv_function sv_fcn,
1544 const string& help_string)
1545 {
1546 if (install_as_function)
1547 install_builtin_variable_as_function (name, value, protect,
1548 eternal, help_string);
1549 else
1550 bind_builtin_variable (name, value, protect, eternal,
1551 sv_fcn, help_string);
1552 }
1553
1554 void
1555 install_builtin_variable_as_function (const string& name,
1556 const octave_value& val,
1557 bool protect, bool eternal,
1558 const string& help)
1559 {
1560 symbol_record *sym_rec = global_sym_tab->lookup (name, true);
1561 sym_rec->unprotect ();
1562
1563 string tmp_help = help.empty () ? sym_rec->help () : help;
1564
1565 sym_rec->define_as_fcn (val);
1566
1567 sym_rec->document (tmp_help);
1568
1569 if (protect)
1570 sym_rec->protect ();
1571
1572 if (eternal)
1573 sym_rec->make_eternal ();
1574 }
1575
1576 void
1577 alias_builtin (const string& alias, const string& name)
1578 {
1579 symbol_record *sr_name = global_sym_tab->lookup (name);
1580
1581 if (! sr_name)
1582 panic ("can't alias to undefined name!");
1583
1584 symbol_record *sr_alias = global_sym_tab->lookup (alias, true);
1585
1586 if (sr_alias)
1587 sr_alias->alias (sr_name);
1588 else
1589 panic ("can't find symbol record for builtin function `%s'",
1590 alias.c_str ());
1591 }
1592
1593 // Defining variables. 1465 // Defining variables.
1594 1466
1595 void 1467 void
1596 bind_ans (const octave_value& val, bool print) 1468 bind_ans (const octave_value& val, bool print)
1597 { 1469 {