# HG changeset patch # User jwe # Date 791848654 0 # Node ID 51fd9e40a7f78b658c6a222ce7eb38ec7bf1fa18 # Parent 2e10146f7f72a069db5516790e62a650650a47f8 [project @ 1995-02-03 21:57:34 by jwe] diff -r 2e10146f7f72 -r 51fd9e40a7f7 src/pt-exp-base.cc --- a/src/pt-exp-base.cc Thu Feb 02 16:47:56 1995 +0000 +++ b/src/pt-exp-base.cc Fri Feb 03 21:57:34 1995 +0000 @@ -1940,8 +1940,12 @@ tree_constant rhs_val = rhs->eval (0); if (error_state) { - if (error_state) - eval_error (); + eval_error (); + } + else if (rhs_val.is_undefined ()) + { + error ("value on right hand side of assignment is undefined"); + eval_error (); } else if (! index) { @@ -2112,8 +2116,9 @@ tree_constant *tmp = 0; if (results(i).is_undefined ()) { - Matrix m; - tmp = new tree_constant (m); + error ("element number %d undefined in return list", i+1); + eval_error (); + break; } else tmp = new tree_constant (results(i)); @@ -2808,7 +2813,16 @@ // Copy return values out. if (ret_list) - retval = ret_list->convert_to_const_vector (vr_list); + { + if (nargout > 0 && user_pref.define_all_return_values) + { + tree_constant tmp = builtin_any_variable ("default_return_value"); + if (tmp.is_defined ()) + ret_list->initialize_undefined_elements (tmp); + } + + retval = ret_list->convert_to_const_vector (vr_list); + } else if (user_pref.return_last_computed_value) retval(0) = last_computed_value; } diff -r 2e10146f7f72 -r 51fd9e40a7f7 src/pt-misc.cc --- a/src/pt-misc.cc Thu Feb 02 16:47:56 1995 +0000 +++ b/src/pt-misc.cc Fri Feb 03 21:57:34 1995 +0000 @@ -295,6 +295,17 @@ } void +tree_parameter_list::initialize_undefined_elements (const tree_constant& val) +{ + for (Pix p = first (); p != 0; next (p)) + { + tree_identifier *elt = this->operator () (p); + if (! elt->is_defined ()) + elt->assign (val); + } +} + +void tree_parameter_list::define_from_arg_vector (const Octave_object& args) { int nargin = args.length (); diff -r 2e10146f7f72 -r 51fd9e40a7f7 src/pt-misc.h --- a/src/pt-misc.h Thu Feb 02 16:47:56 1995 +0000 +++ b/src/pt-misc.h Fri Feb 03 21:57:34 1995 +0000 @@ -183,6 +183,8 @@ int varargs_only (void) { return (marked_for_varargs < 0); } + void initialize_undefined_elements (const tree_constant& val); + void define_from_arg_vector (const Octave_object& args); int is_defined (void); diff -r 2e10146f7f72 -r 51fd9e40a7f7 src/user-prefs.cc --- a/src/user-prefs.cc Thu Feb 02 16:47:56 1995 +0000 +++ b/src/user-prefs.cc Fri Feb 03 21:57:34 1995 +0000 @@ -44,7 +44,7 @@ init_user_prefs (void) { user_pref.automatic_replot = 0; - user_pref.whitespace_in_literal_matrix = 0; + user_pref.define_all_return_values = 0; user_pref.do_fortran_indexing = 0; user_pref.empty_list_elements_ok = 0; user_pref.ignore_function_time_stamp = 0; @@ -69,6 +69,7 @@ user_pref.warn_comma_in_global_decl = 0; user_pref.warn_divide_by_zero = 0; user_pref.warn_function_name_clash = 0; + user_pref.whitespace_in_literal_matrix = 0; user_pref.default_save_format = 0; user_pref.editor = 0; @@ -120,59 +121,15 @@ } -// Should whitespace in a literal matrix list be automatically -// converted to commas and semicolons? -// -// user specifies value of pref -// -------------- ------------- -// "ignore" 2 -// "traditional" 1 -// anything else 0 -// -// Octave will never insert a comma in a literal matrix list if the -// user specifies "ignore". For example, the statement [1 2] will -// result in an error instead of being treated the same as [1, 2], and -// the statement -// -// [ 1, 2, -// 3, 4 ] -// -// will result in the vector [1 2 3 4] instead of a matrix. -// -// Traditional behavior makes Octave convert spaces to a comma between -// identifiers and `('. For example, the statement -// -// [eye (2)] -// -// will be parsed as -// -// [eye, (2)] -// -// and will result in an error since the `eye' function will be -// called with no arguments. To get around this, you would have to -// omit the space between `eye' and the `('. -// -// The default value is 0, which results in behavior that is the same -// as traditional, except that Octave does not convert spaces to a -// comma between identifiers and `('. For example, the statement -// -// [eye (2)] -// -// will result in a call to `eye' with the argument `2'. +// Should variables returned from functions have default values if +// they are otherwise uninitialized? int -whitespace_in_literal_matrix (void) +define_all_return_values (void) { - int pref = 0; - char *val = builtin_string_variable ("whitespace_in_literal_matrix"); - if (val) - { - if (strncmp (val, "ignore", 6) == 0) - pref = 2; - else if (strncmp (val, "traditional", 11) == 0) - pref = 1; - } - user_pref.whitespace_in_literal_matrix = pref; + user_pref.define_all_return_values = + check_str_pref ("define_all_return_values"); + return 0; } @@ -476,6 +433,64 @@ return 0; } + +// Should whitespace in a literal matrix list be automatically +// converted to commas and semicolons? +// +// user specifies value of pref +// -------------- ------------- +// "ignore" 2 +// "traditional" 1 +// anything else 0 +// +// Octave will never insert a comma in a literal matrix list if the +// user specifies "ignore". For example, the statement [1 2] will +// result in an error instead of being treated the same as [1, 2], and +// the statement +// +// [ 1, 2, +// 3, 4 ] +// +// will result in the vector [1 2 3 4] instead of a matrix. +// +// Traditional behavior makes Octave convert spaces to a comma between +// identifiers and `('. For example, the statement +// +// [eye (2)] +// +// will be parsed as +// +// [eye, (2)] +// +// and will result in an error since the `eye' function will be +// called with no arguments. To get around this, you would have to +// omit the space between `eye' and the `('. +// +// The default value is 0, which results in behavior that is the same +// as traditional, except that Octave does not convert spaces to a +// comma between identifiers and `('. For example, the statement +// +// [eye (2)] +// +// will result in a call to `eye' with the argument `2'. + +int +whitespace_in_literal_matrix (void) +{ + int pref = 0; + char *val = builtin_string_variable ("whitespace_in_literal_matrix"); + if (val) + { + if (strncmp (val, "ignore", 6) == 0) + pref = 2; + else if (strncmp (val, "traditional", 11) == 0) + pref = 1; + } + user_pref.whitespace_in_literal_matrix = pref; + return 0; +} + + int set_output_max_field_width (void) { diff -r 2e10146f7f72 -r 51fd9e40a7f7 src/user-prefs.h --- a/src/user-prefs.h Thu Feb 02 16:47:56 1995 +0000 +++ b/src/user-prefs.h Fri Feb 03 21:57:34 1995 +0000 @@ -27,7 +27,7 @@ struct user_preferences { int automatic_replot; - int whitespace_in_literal_matrix; + int define_all_return_values; int do_fortran_indexing; int empty_list_elements_ok; int ignore_function_time_stamp; @@ -52,6 +52,7 @@ int warn_comma_in_global_decl; int warn_divide_by_zero; int warn_function_name_clash; + int whitespace_in_literal_matrix; char *default_save_format; char *editor; @@ -71,7 +72,7 @@ extern void init_user_prefs (void); extern int automatic_replot (void); -extern int whitespace_in_literal_matrix (void); +extern int define_all_return_values (void); extern int do_fortran_indexing (void); extern int empty_list_elements_ok (void); extern int ignore_function_time_stamp (void); @@ -93,6 +94,7 @@ extern int warn_comma_in_global_decl (void); extern int warn_divide_by_zero (void); extern int warn_function_name_clash (void); +extern int whitespace_in_literal_matrix (void); extern int set_output_max_field_width (void); extern int set_output_precision (void); diff -r 2e10146f7f72 -r 51fd9e40a7f7 src/variables.cc --- a/src/variables.cc Thu Feb 02 16:47:56 1995 +0000 +++ b/src/variables.cc Fri Feb 03 21:57:34 1995 +0000 @@ -857,6 +857,27 @@ return status; } +// Look for the given name in the global symbol table. + +tree_constant +builtin_any_variable (const char *name) +{ + tree_constant retval; + + symbol_record *sr = global_sym_tab->lookup (name, 0, 0); + +// It is a prorgramming error to look for builtins that aren't. + + assert (sr); + + tree_fvc *defn = sr->def (); + + if (defn) + retval = defn->eval (0); + + return retval; +} + // Global stuff and links to builtin variables and functions. // Make the definition of the symbol record sr be the same as the @@ -1484,9 +1505,11 @@ 0, 0, 1, automatic_replot, "if true, auto-insert a replot command when a plot changes"); - DEFVAR ("whitespace_in_literal_matrix", SBV_whitespace_in_literal_matrix, "", - 0, 0, 1, whitespace_in_literal_matrix, - "control auto-insertion of commas and semicolons in literal matrices"); + DEFVAR ("default_return_value", SBV_default_return_value, Matrix (), + 0, 0, 1, 0, + "the default for value for unitialized variables returned from\n\ +functions. Only used if the variable initialize_return_values is\n\ +set to \"true\"."); DEFVAR ("default_save_format", SBV_default_save_format, "ascii", 0, 0, 1, sv_default_save_format, @@ -1524,6 +1547,12 @@ DEFVAR ("inf", SBV_inf, octave_Inf, 0, 1, 1, 0, "infinity"); + DEFVAR ("define_all_return_values", SBV_define_all_return_values, + "false", 0, 0, 1, define_all_return_values, + "control whether values returned from functions should have a\n\ +value even if one has not been explicitly assigned. See also\n\ +default_return_value"); + DEFVAR ("j", SBV_j, Complex (0.0, 1.0), 1, 1, 1, 0, "sqrt (-1)"); @@ -1634,6 +1663,11 @@ DEFVAR ("warn_function_name_clash", SBV_warn_function_name_clash, "true", 0, 0, 1, warn_function_name_clash, "produce warning if function name conflicts with file name"); + + DEFVAR ("whitespace_in_literal_matrix", SBV_whitespace_in_literal_matrix, "", + 0, 0, 1, whitespace_in_literal_matrix, + "control auto-insertion of commas and semicolons in literal matrices"); + } // Deleting names from the symbol tables. diff -r 2e10146f7f72 -r 51fd9e40a7f7 src/variables.h --- a/src/variables.h Thu Feb 02 16:47:56 1995 +0000 +++ b/src/variables.h Fri Feb 03 21:57:34 1995 +0000 @@ -72,6 +72,7 @@ extern char *builtin_string_variable (const char *); extern int builtin_real_scalar_variable (const char *, double&); +extern tree_constant builtin_any_variable (const char *); extern void link_to_global_variable (symbol_record *sr); extern void link_to_builtin_variable (symbol_record *sr);