diff src/pt-const.h @ 1558:297e084c3857

[project @ 1995-10-12 07:20:28 by jwe]
author jwe
date Thu, 12 Oct 1995 07:21:51 +0000
parents eaf4f68d3757
children 0d9e10d10bd7
line wrap: on
line diff
--- a/src/pt-const.h	Thu Oct 12 00:34:40 1995 +0000
+++ b/src/pt-const.h	Thu Oct 12 07:21:51 1995 +0000
@@ -52,9 +52,260 @@
 {
 private:
 
-// The real representation of a constant, declared in tc-rep.h
+// The actual representation of the tree_constant.
+
+  class
+  tree_constant_rep
+    {
+      friend class tree_constant;
+
+      private:
+
+	enum constant_type
+	  {
+	    unknown_constant,
+	    scalar_constant,
+	    matrix_constant,
+	    complex_scalar_constant,
+	    complex_matrix_constant,
+	    string_constant,
+	    range_constant,
+	    map_constant,
+	    magic_colon,
+	    all_va_args,
+	  };
+
+	enum force_orient
+	  {
+	    no_orient,
+	    row_orient,
+	    column_orient,
+	  };
+
+	tree_constant_rep (void);
+
+	tree_constant_rep (double d);
+	tree_constant_rep (const Matrix& m);
+	tree_constant_rep (const DiagMatrix& d);
+	tree_constant_rep (const RowVector& v, int pcv);
+	tree_constant_rep (const ColumnVector& v, int pcv);
+
+	tree_constant_rep (const Complex& c);
+	tree_constant_rep (const ComplexMatrix& m);
+	tree_constant_rep (const ComplexDiagMatrix& d);
+	tree_constant_rep (const ComplexRowVector& v, int pcv);
+	tree_constant_rep (const ComplexColumnVector& v, int pcv);
+
+	tree_constant_rep (const char *s);
+	tree_constant_rep (const Octave_str_obj& s);
+
+	tree_constant_rep (double base, double limit, double inc);
+	tree_constant_rep (const Range& r);
+
+	tree_constant_rep (const Octave_map& m);
+
+	tree_constant_rep (tree_constant_rep::constant_type t);
+
+	tree_constant_rep (const tree_constant_rep& t);
+
+	~tree_constant_rep (void);
+
+	void *operator new (size_t size);
+	void operator delete (void *p, size_t size);
+
+	int rows (void) const;
+	int columns (void) const;
+
+	int is_defined (void) const
+	  { return type_tag != tree_constant_rep::unknown_constant; }
+
+	int is_undefined (void) const
+	  { return type_tag == tree_constant_rep::unknown_constant; }
+
+	int is_unknown (void) const
+	  { return type_tag == tree_constant_rep::unknown_constant; }
+
+	int is_real_scalar (void) const
+	  { return type_tag == tree_constant_rep::scalar_constant; }
+
+	int is_real_matrix (void) const
+	  { return type_tag == tree_constant_rep::matrix_constant; }
+
+	int is_complex_scalar (void) const
+	  { return type_tag == tree_constant_rep::complex_scalar_constant; }
+
+	int is_complex_matrix (void) const
+	  { return type_tag == tree_constant_rep::complex_matrix_constant; }
+
+	int is_string (void) const
+	  { return type_tag == tree_constant_rep::string_constant; }
+
+	int is_range (void) const
+	  { return type_tag == tree_constant_rep::range_constant; }
+
+	int is_map (void) const
+	  { return type_tag == tree_constant_rep::map_constant; }
+
+	int is_magic_colon (void) const
+	  { return type_tag == tree_constant_rep::magic_colon; }
+
+	int is_all_va_args (void) const
+	  { return type_tag == tree_constant_rep::all_va_args; }
+
+	tree_constant all (void) const;
+	tree_constant any (void) const;
+
+	int is_real_type (void) const
+	  {
+	    return (type_tag == scalar_constant
+		    || type_tag == matrix_constant
+		    || type_tag == range_constant
+		    || type_tag == string_constant);
+	  }
+
+	int is_complex_type (void) const
+	  {
+	    return (type_tag == complex_matrix_constant
+		    || type_tag == complex_scalar_constant);
+	  }
+
+	// Would be nice to get rid of the next four functions:
+
+	int is_scalar_type (void) const
+	  {
+	    return (type_tag == scalar_constant
+		    || type_tag == complex_scalar_constant);
+	  }
 
-#include "tc-rep.h"
+	int is_matrix_type (void) const
+	  {
+	    return (type_tag == matrix_constant
+		    || type_tag == complex_matrix_constant);
+	  }
+
+	int is_numeric_type (void) const
+	  {
+	    return (type_tag == scalar_constant
+		    || type_tag == matrix_constant
+		    || type_tag == complex_matrix_constant
+		    || type_tag == complex_scalar_constant);
+	  }
+
+	int valid_as_scalar_index (void) const;
+	int valid_as_zero_index (void) const;
+
+	int is_true (void) const;
+
+	int is_empty (void) const
+	  {
+	    return ((! (is_magic_colon ()
+			|| is_all_va_args ()
+			|| is_unknown ()))
+		    && (rows () == 0
+			|| columns () == 0));
+	  }
+
+	double double_value (int frc_str_conv = 0) const;
+	Matrix matrix_value (int frc_str_conv = 0) const;
+	Complex complex_value (int frc_str_conv = 0) const;
+	ComplexMatrix complex_matrix_value (int frc_str_conv = 0) const;
+	Octave_str_obj all_strings (void) const;
+	const char *string_value (void) const;
+	Range range_value (void) const;
+	Octave_map map_value (void) const;
+
+	tree_constant& lookup_map_element (const char *name, int insert = 0,
+					   int silent = 0);
+
+	ColumnVector vector_value (int frc_str_conv = 0,
+				   int frc_vec_conv = 0) const;
+
+	ComplexColumnVector complex_vector_value (int frc_str_conv = 0,
+						  int frc_vec_conv = 0) const;
+
+	tree_constant convert_to_str (void) const;
+
+	void convert_to_row_or_column_vector (void);
+
+	void bump_value (tree_expression::type);
+
+	void resize (int i, int j);
+	void resize (int i, int j, double val);
+
+	void stash_original_text (char *s);
+
+	void maybe_mutate (void);
+
+	void print (void);
+	void print (ostream& os);
+
+	void print_code (ostream& os);
+
+	void gripe_wrong_type_arg (const char *name,
+				   const tree_constant_rep& tcr) const;
+
+	char *type_as_string (void) const;
+
+	// Binary and unary operations.
+
+	friend tree_constant do_binary_op (tree_constant& a, tree_constant& b,
+					   tree_expression::type t);
+
+	friend tree_constant do_unary_op (tree_constant& a,
+					  tree_expression::type t);
+
+	// We want to eliminate this.
+
+	constant_type const_type (void) const { return type_tag; }
+
+	// We want to get rid of these too:
+
+	void force_numeric (int frc_str_conv = 0);
+	tree_constant make_numeric (int frc_str_conv = 0) const;
+
+	// But not this.
+
+	void convert_to_matrix_type (void);
+
+	// Indexing and assignment.
+
+	void clear_index (void);
+
+	// void set_index (double d);
+	void set_index (const Range& r);
+	void set_index (const ColumnVector& v);
+	void set_index (const Matrix& m);
+	void set_index (char c);
+
+	void set_index (const Octave_object& args);
+
+	tree_constant do_index (const Octave_object& args);
+
+	void maybe_widen (constant_type t);
+
+	void assign (tree_constant& rhs, const Octave_object& args);
+
+	// Data.
+
+	union
+	  {
+	    double scalar;		    // A real scalar constant.
+	    Matrix *matrix;		    // A real matrix constant.
+	    Complex *complex_scalar;	    // A real scalar constant.
+	    ComplexMatrix *complex_matrix;  // A real matrix constant.
+	    Octave_str_obj *str_obj;	    // A character string constant.
+	    Range *range;		    // A set of evenly spaced values.
+	    Octave_map *a_map;	      	    // An associative array.
+
+	    tree_constant_rep *freeptr;     // For custom memory management.
+	  };
+
+	constant_type type_tag;
+
+	int count;
+
+	char *orig_text;
+    };
 
   union
     {
@@ -67,29 +318,29 @@
   enum magic_colon { magic_colon_t };
   enum all_va_args { all_va_args_t };
 
-// Constructors.  It is possible to create the following types of
-// constants:
-//
-// constant type    constructor arguments
-// -------------    ---------------------
-// unknown          none
-// real scalar      double
-// real matrix      Matrix
-//                  DiagMatrix
-//                  RowVector
-//                  ColumnVector
-// complex scalar   Complex
-// complex matrix   ComplexMatrix
-//                  ComplexDiagMatrix
-//                  ComplexRowVector
-//                  ComplexColumnVector
-// string           char* (null terminated)
-//                  Octave_str_obj
-// range            double, double, double
-//                  Range
-// map              Octave_map
-// magic colon      tree_constant::magic_colon
-// all_va_args      tree_constant::all_va_args
+  // Constructors.  It is possible to create the following types of
+  // constants:
+  //
+  // constant type    constructor arguments
+  // -------------    ---------------------
+  // unknown          none
+  // real scalar      double
+  // real matrix      Matrix
+  //                  DiagMatrix
+  //                  RowVector
+  //                  ColumnVector
+  // complex scalar   Complex
+  // complex matrix   ComplexMatrix
+  //                  ComplexDiagMatrix
+  //                  ComplexRowVector
+  //                  ComplexColumnVector
+  // string           char* (null terminated)
+  //                  Octave_str_obj
+  // range            double, double, double
+  //                  Range
+  // map              Octave_map
+  // magic colon      tree_constant::magic_colon
+  // all_va_args      tree_constant::all_va_args
 
   tree_constant (void) : tree_fvc ()
     { rep = new tree_constant_rep (); rep->count = 1; }
@@ -155,24 +406,24 @@
       rep->count = 1;
     }
 
-// Copy constructor.
+  // Copy constructor.
 
   tree_constant (const tree_constant& a) : tree_fvc ()
     { rep = a.rep; rep->count++; }
 
-// Delete the representation of this constant if the count drops to
-// zero.
+  // Delete the representation of this constant if the count drops to
+  // zero.
 
   ~tree_constant (void);
 
   void *operator new (size_t size);
   void operator delete (void *p, size_t size);
 
-// Simple assignment.
+  // Simple assignment.
 
   tree_constant operator = (const tree_constant& a);
 
-// Indexed assignment.
+  // Indexed assignment.
 
   tree_constant assign (tree_constant& rhs, const Octave_object& args)
     {
@@ -188,34 +439,35 @@
       return *this;
     }
 
-// Simple structure assignment.
+  // Simple structure assignment.
 
   tree_constant assign_map_element (SLList<char*>& list,
 				    tree_constant& rhs);
 
-// Indexed structure assignment.
+  // Indexed structure assignment.
 
   tree_constant assign_map_element (SLList<char*>& list,
 				    tree_constant& rhs,
 				    const Octave_object& args);
 
-// Type.  It would be nice to eliminate the need for this.
+  // Type.  It would be nice to eliminate the need for this.
 
   int is_constant (void) const { return 1; }
 
-// Size.
+  // Size.
 
   int rows (void) const { return rep->rows (); }
   int columns (void) const { return rep->columns (); }
 
-// Does this constant have a type?  Both of these are provided since
-// it is sometimes more natural to write is_undefined() instead of
-// ! is_defined().
+  // Does this constant have a type?  Both of these are provided since
+  // it is sometimes more natural to write is_undefined() instead of
+  // ! is_defined().
 
   int is_defined (void) const { return rep->is_defined (); }
   int is_undefined (void) const { return rep->is_undefined (); }
 
-// What type is this constant?
+  // Is this constant a particular type, or does it belong to a
+  // particular class of types?
 
   int is_unknown (void) const { return rep->is_unknown (); }
   int is_real_scalar (void) const { return rep->is_real_scalar (); }
@@ -228,46 +480,39 @@
   int is_magic_colon (void) const { return rep->is_magic_colon (); }
   int is_all_va_args (void) const { return rep->is_all_va_args (); }
 
-// Are any or all of the elements in this constant nonzero?
+  // Are any or all of the elements in this constant nonzero?
 
   tree_constant all (void) const { return rep->all (); }
   tree_constant any (void) const { return rep->any (); }
 
+  // Other type stuff.
+
   int is_real_type (void) const { return rep->is_real_type (); }
 
   int is_complex_type (void) const { return rep->is_complex_type (); }
 
-// Would be nice to get rid of the next four functions:
-
   int is_scalar_type (void) const { return rep->is_scalar_type (); }
   int is_matrix_type (void) const { return rep->is_matrix_type (); }
 
   int is_numeric_type (void) const
     { return rep->is_numeric_type (); }
 
-  int is_numeric_or_range_type (void) const
-    { return rep->is_numeric_or_range_type (); }
-
-// Is this constant valid as a scalar index?
-
   int valid_as_scalar_index (void) const
     { return rep->valid_as_scalar_index (); }
 
-// Is this constant valid as a zero scalar index?
-
   int valid_as_zero_index (void) const
     { return rep->valid_as_zero_index (); }
 
-// Does this constant correspond to a truth value?
+  // Does this constant correspond to a truth value?
 
   int is_true (void) const { return rep->is_true (); }
 
-// Is at least one of the dimensions of this constant zero?
+  // Is at least one of the dimensions of this constant zero?
 
   int is_empty (void) const
     { return rep->is_empty (); }
 
-// Are the dimensions of this constant zero by zero?
+  // Are the dimensions of this constant zero by zero?
 
   int is_zero_by_zero (void) const
     {
@@ -275,19 +520,19 @@
 	      && rows () == 0 && columns () == 0);
     } 
 
-// Values.
+  // Values.
 
-  double double_value (int force_string_conversion = 0) const
-    { return rep->double_value (force_string_conversion); }
+  double double_value (int frc_str_conv = 0) const
+    { return rep->double_value (frc_str_conv); }
 
-  Matrix matrix_value (int force_string_conversion = 0) const
-    { return rep->matrix_value (force_string_conversion); }
+  Matrix matrix_value (int frc_str_conv = 0) const
+    { return rep->matrix_value (frc_str_conv); }
 
-  Complex complex_value (int force_string_conversion = 0) const
-    { return rep->complex_value (force_string_conversion); }
+  Complex complex_value (int frc_str_conv = 0) const
+    { return rep->complex_value (frc_str_conv); }
 
-  ComplexMatrix complex_matrix_value (int force_string_conversion = 0) const
-    { return rep->complex_matrix_value (force_string_conversion); }
+  ComplexMatrix complex_matrix_value (int frc_str_conv = 0) const
+    { return rep->complex_matrix_value (frc_str_conv); }
 
   Octave_str_obj all_strings (void) const
     { return rep->all_strings (); }
@@ -306,15 +551,15 @@
   tree_constant lookup_map_element (SLList<char*>& list,
 				    int insert = 0, int silent = 0);
 
-  ColumnVector vector_value (int /* force_string_conversion */ = 0,
-			     int /* force_vector_conversion */ = 0) const 
+  ColumnVector vector_value (int /* frc_str_conv */ = 0,
+			     int /* frc_vec_conv */ = 0) const 
     { return rep->vector_value (); }
 
-  ComplexColumnVector complex_vector_value (int /* force_string_conv */ = 0,
-					    int /* force_vec_conv */ = 0) const
+  ComplexColumnVector complex_vector_value (int /* frc_str_conv */ = 0,
+					    int /* frc_vec_conv */ = 0) const
     { return rep->complex_vector_value (); }
 
-// Binary and unary operations.
+  // Binary and unary operations.
 
   friend tree_constant do_binary_op (tree_constant& a, tree_constant& b,
 				     tree_expression::type t);
@@ -322,9 +567,9 @@
   friend tree_constant do_unary_op (tree_constant& a,
 				    tree_expression::type t);
 
-// Conversions.  These should probably be private.  If a user of this
-// class wants a certain kind of constant, he should simply ask for
-// it, and we should convert it if possible.
+  // Conversions.  These should probably be private.  If a user of this
+  // class wants a certain kind of constant, he should simply ask for
+  // it, and we should convert it if possible.
 
   tree_constant convert_to_str (void)
     { return rep->convert_to_str (); }
@@ -332,7 +577,7 @@
   void convert_to_row_or_column_vector (void)
     { rep->convert_to_row_or_column_vector (); }
 
-// Increment or decrement this constant.
+  // Increment or decrement this constant.
 
   void bump_value (tree_expression::type et)
     {
@@ -349,8 +594,8 @@
   void print (void);
   void print (ostream& os) { rep->print (os); }
 
-// Evaluate this constant, possibly converting complex to real, or
-// matrix to scalar, etc.
+  // Evaluate this constant, possibly converting complex to real, or
+  // matrix to scalar, etc.
 
   tree_constant eval (int print_result)
     {
@@ -367,9 +612,6 @@
     {
       Octave_object retval;
 
-// XXX FIXME XXX -- make it safe to call do_index() with
-// args.length () == 0
-
       if (args.length () > 0)
 	retval(0) = rep->do_index (args);
       else
@@ -381,20 +623,20 @@
       return retval;
     }
 
-// Store the original text corresponding to this constant for later
-// pretty printing.
+  // Store the original text corresponding to this constant for later
+  // pretty printing.
 
   void stash_original_text (char *s)
     { rep->stash_original_text (s); }
 
-// Pretty print this constant.
+  // Pretty print this constant.
  
   void print_code (ostream& os);
 
   char *type_as_string (void) const
     { return rep->type_as_string (); }
 
-// We really do need this, and it should be private:
+  // We really do need this, and it should be private:
 
 private:
 
@@ -402,57 +644,26 @@
 
   tree_constant_rep *make_unique_map (void);
 
-public:
-
-// -------------------------------------------------------------------
-
-// We want to eliminate this, or at least make it private.
+  // We want to eliminate this, or at least make it private.
 
   tree_constant_rep::constant_type const_type (void) const
     { return rep->const_type (); }
 
-private:
+  void convert_to_matrix_type (void) { rep->convert_to_matrix_type (); }
 
-// Can we make these go away?
+  // Can we make these go away?
 
-// These need better names, since a range really is a numeric type.
+  // These need better names, since a range really is a numeric type.
 
-  void force_numeric (int force_str_conv = 0)
-    { rep->force_numeric (force_str_conv); }
+  void force_numeric (int frc_str_conv = 0)
+    { rep->force_numeric (frc_str_conv); }
 
-  tree_constant make_numeric (int force_str_conv = 0) const
+  tree_constant make_numeric (int frc_str_conv = 0) const
     {
       if (is_numeric_type ())
 	return *this;
       else
-	return rep->make_numeric (force_str_conv);
-    }
-
-#if 0
-  tree_constant make_numeric_or_range (void) const
-    {
-      if (is_numeric_type () || is_range ())
-	return *this;
-      else
-	return rep->make_numeric ();
-    }
-#endif
-
-  tree_constant make_numeric_or_magic (void) const
-    {
-      if (is_numeric_type () || is_all_va_args () || is_magic_colon ())
-	return *this;
-      else
-	return rep->make_numeric ();
-    }
-
-  tree_constant make_numeric_or_range_or_magic (void) const
-    {
-      if (is_numeric_type () || is_range () || is_all_va_args ()
-	  || is_magic_colon ())
-	return *this;
-      else
-	return rep->make_numeric ();
+	return rep->make_numeric (frc_str_conv);
     }
 };
 
@@ -460,10 +671,6 @@
 
 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);
-
 #endif
 
 /*