changeset 1093:51fd9e40a7f7

[project @ 1995-02-03 21:57:34 by jwe]
author jwe
date Fri, 03 Feb 1995 21:57:34 +0000
parents 2e10146f7f72
children b680e0eb5f23
files src/pt-exp-base.cc src/pt-misc.cc src/pt-misc.h src/user-prefs.cc src/user-prefs.h src/variables.cc src/variables.h
diffstat 7 files changed, 140 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/src/pt-exp-base.cc
+++ b/src/pt-exp-base.cc
@@ -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;
   }
--- a/src/pt-misc.cc
+++ b/src/pt-misc.cc
@@ -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 ();
--- a/src/pt-misc.h
+++ b/src/pt-misc.h
@@ -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);
--- a/src/user-prefs.cc
+++ b/src/user-prefs.cc
@@ -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)
 {
--- a/src/user-prefs.h
+++ b/src/user-prefs.h
@@ -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);
--- a/src/variables.cc
+++ b/src/variables.cc
@@ -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.
--- a/src/variables.h
+++ b/src/variables.h
@@ -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);