changeset 1199:4f1bfa351b99

[project @ 1995-03-31 22:16:54 by jwe]
author jwe
date Fri, 31 Mar 1995 22:21:38 +0000
parents 1352ecca2d91
children 50cc3480a2db
files src/pt-const.cc src/pt-const.h src/pt-exp-base.cc src/tc-rep.cc
diffstat 4 files changed, 92 insertions(+), 86 deletions(-) [+]
line wrap: on
line diff
--- a/src/pt-const.cc	Fri Mar 31 22:13:32 1995 +0000
+++ b/src/pt-const.cc	Fri Mar 31 22:21:38 1995 +0000
@@ -26,11 +26,13 @@
 #endif
 
 #include <iostream.h>
+#include <strstream.h>
 
 #include "tree-const.h"
+#include "user-prefs.h"
+#include "pager.h"
 #include "error.h"
 #include "gripes.h"
-#include "user-prefs.h"
 #include "oct-map.h"
 
 // The following three variables could be made static members of the
@@ -134,6 +136,15 @@
   return retval;
 }
 
+void
+tree_constant::print (void)
+{
+  ostrstream output_buf;
+  print (output_buf);
+  output_buf << ends;
+  maybe_page_output (output_buf);
+}
+
 // Simple structure assignment.
 
 void
@@ -251,6 +262,25 @@
     os << ")";
 }
 
+int
+print_as_scalar (const tree_constant& val)
+{
+  int nr = val.rows ();
+  int nc = val.columns ();
+  return (val.is_scalar_type ()
+	  || val.is_string ()
+	  || (val.is_matrix_type ()
+	      && ((nr == 1 && nc == 1)
+		  || nr == 0
+		  || nc == 0)));
+}
+
+int
+print_as_structure (const tree_constant& val)
+{
+  return val.is_map ();
+}
+
 // Construct return vector of empty matrices.  Return empty matrices
 // and/or gripe when appropriate.
 
--- a/src/pt-const.h	Fri Mar 31 22:13:32 1995 +0000
+++ b/src/pt-const.h	Fri Mar 31 22:21:38 1995 +0000
@@ -324,16 +324,19 @@
       rep->bump_value (et);
     }
 
+  void print (void);
+  void print (ostream& os) { rep->print (os); }
+
 // Evaluate this constant, possibly converting complex to real, or
 // matrix to scalar, etc.
 
-  tree_constant eval (int print)
+  tree_constant eval (int print_result)
     {
       if (! is_scalar_type ())
 	rep->maybe_mutate ();
 
-      if (print)
-	rep->print ();
+      if (print_result)
+	print ();
 
       return *this;
     }
@@ -431,6 +434,10 @@
     }
 };
 
+extern int print_as_scalar (const tree_constant& val);
+
+extern int print_as_structure (const tree_constant& val);
+
 // XXX FIXME XXX -- this is not used very much now.  Perhaps it can be
 // eliminated.
 extern Octave_object vector_of_empties (int nargout, const char *fcn_name);
--- a/src/pt-exp-base.cc	Fri Mar 31 22:13:32 1995 +0000
+++ b/src/pt-exp-base.cc	Fri Mar 31 22:21:38 1995 +0000
@@ -114,21 +114,8 @@
   return 0;
 }
 
-static int
-print_as_scalar (const tree_constant& val)
-{
-  int nr = val.rows ();
-  int nc = val.columns ();
-  return (val.is_scalar_type ()
-	  || val.is_string ()
-	  || (val.is_matrix_type ()
-	      && ((nr == 1 && nc == 1)
-		  || nr == 0
-		  || nc == 0)));
-}
-
 static void
-print_constant (tree_constant& tc, char *name)
+print_constant (tree_constant& tc, char *name, int print_padding = 1)
 {
   int pad_after = 0;
   if (user_pref.print_answer_id_name)
@@ -139,6 +126,12 @@
 	  output_buf << name << " = " << ends;
 	  maybe_page_output (output_buf);
 	}
+      else if (print_as_structure (tc))
+	{
+	  ostrstream output_buf;
+	  output_buf << name << " = {\n" << ends;
+	  maybe_page_output (output_buf);
+	}
       else
 	{
 	  pad_after = 1;
@@ -150,7 +143,7 @@
 
   tc.eval (1);
 
-  if (pad_after)
+  if (print_padding && pad_after)
     {
       ostrstream output_buf;
       output_buf << "\n" << ends;
@@ -1942,35 +1935,9 @@
 	}
     }
 
-  if (! error_state && retval.is_defined ())
-    {
-      int pad_after = 0;
-      if (print && user_pref.print_answer_id_name)
-	{
-	  if (print_as_scalar (retval))
-	    {
-	      ostrstream output_buf;
-	      output_buf << lhs->name () << " = " << ends;
-	      maybe_page_output (output_buf);
-	    }
-	  else
-	    {
-	      pad_after = 1;
-	      ostrstream output_buf;
-	      output_buf << lhs->name () << " =\n\n" << ends;
-	      maybe_page_output (output_buf);
-	    }
-	}
-
-      retval.eval (print);
-
-      if (print && pad_after)
-	{
-	  ostrstream output_buf;
-	  output_buf << "\n" << ends;
-	  maybe_page_output (output_buf);
-	}
-    }
+  if (! error_state && print && retval.is_defined ()
+      && user_pref.print_answer_id_name) 
+    print_constant (retval, lhs->name ());
 
   return retval;
 }
@@ -2099,31 +2066,12 @@
 	      if (print && pad_after)
 		{
 		  ostrstream output_buf;
-		  output_buf << "\n" << '\0';
+		  output_buf << "\n" << ends;
 		  maybe_page_output (output_buf);
 		}
 
 	      if (print && user_pref.print_answer_id_name)
-		{
-		  char *tmp_nm = lhs_expr->name ();
-		  
-		  if (print_as_scalar (results(i)))
-		    {
-		      ostrstream output_buf;
-		      output_buf << tmp_nm << " = " << '\0';
-		      maybe_page_output (output_buf);
-		      last_was_scalar_type = 1;
-		    }
-		  else
-		    {
-		      ostrstream output_buf;
-		      output_buf << tmp_nm << " =\n\n" << '\0';
-		      maybe_page_output (output_buf);
-		      last_was_scalar_type = 0;
-		    }
-		}
-
-	      results(i).eval (print);
+		print_constant (results(i), lhs_expr->name (), 0);
 
 	      pad_after++;
 	      i++;
@@ -2148,7 +2096,7 @@
       if (print && pad_after)
 	{
 	  ostrstream output_buf;
-	  output_buf << "\n" << '\0';
+	  output_buf << "\n" << ends;
 	  maybe_page_output (output_buf);
 	}
     }
--- a/src/tc-rep.cc	Fri Mar 31 22:13:32 1995 +0000
+++ b/src/tc-rep.cc	Fri Mar 31 22:21:38 1995 +0000
@@ -29,7 +29,6 @@
 #include <string.h>
 #include <fstream.h>
 #include <iostream.h>
-#include <strstream.h>
 
 #include "mx-base.h"
 #include "Range.h"
@@ -41,10 +40,10 @@
 #include "gripes.h"
 #include "user-prefs.h"
 #include "utils.h"
-#include "pager.h"
 #include "pr-output.h"
 #include "tree-const.h"
 #include "idx-vector.h"
+#include "unwind-prot.h"
 #include "oct-map.h"
 
 #include "tc-inlines.h"
@@ -58,6 +57,9 @@
 // Multiplier for allocating new blocks.
 static const int newlist_grow_size = 128;
 
+// Indentation level for structures.
+static int structure_indent_level = 0;
+
 static int
 any_element_is_complex (const ComplexMatrix& a)
 {
@@ -1729,13 +1731,11 @@
 }
 
 void
-TC_REP::print (void)
+TC_REP::print (ostream& output_buf)
 {
   if (error_state)
     return;
 
-  ostrstream output_buf;
-
   switch (type_tag)
     {
     case scalar_constant:
@@ -1764,18 +1764,42 @@
 
     case map_constant:
       {
-	output_buf << "<structure";
-	int first = 1;
-	for (Pix p = a_map->first (); p != 0; a_map->next (p))
+// XXX FIXME XXX -- would be nice to print the output in some standard
+// order.  Maybe all substructures first, maybe alphabetize entries,
+// etc.
+	begin_unwind_frame ("TC_REP_print");
+
+	unwind_protect_int (structure_indent_level);
+	unwind_protect_int (user_pref.struct_levels_to_print);
+
+	structure_indent_level += 2;
+
+	if (user_pref.struct_levels_to_print > 0)
 	  {
-	    if (first)
+	    user_pref.struct_levels_to_print--;
+
+	    for (Pix p = a_map->first (); p != 0; a_map->next (p))
 	      {
-		output_buf << ":";
-		first = 0;
+		const char *key = a_map->key (p);
+		tree_constant val = a_map->contents (p);
+
+		output_buf.form ("%*s%s = ", structure_indent_level,
+				 "", key);
+
+		if (print_as_structure (val))
+		  output_buf << "{\n";
+		else if (! print_as_scalar (val))
+		  output_buf << "\n";
+
+		val.print (output_buf);
 	      }
-	    output_buf << " " << a_map->key (p);
+
+	    output_buf.form ("%*s%s", structure_indent_level, "", "}\n");
 	  }
-	output_buf << ">\n";
+	else
+	  output_buf << "<structure>\n";
+
+	run_unwind_frame ("TC_REP_print");
       }
       break;
 
@@ -1785,9 +1809,6 @@
       panic_impossible ();
       break;
     }
-
-  output_buf << ends;
-  maybe_page_output (output_buf);
 }
 
 void