changeset 3208:e8a7163701be

[project @ 1998-11-03 05:12:47 by jwe]
author jwe
date Tue, 03 Nov 1998 05:12:48 +0000
parents dddfaa93a99c
children fbb332b96e4f
files src/ChangeLog src/lex.l src/parse.y src/pt-assign.cc src/pt-assign.h src/pt-pr-code.cc
diffstat 6 files changed, 191 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Tue Nov 03 04:04:38 1998 +0000
+++ b/src/ChangeLog	Tue Nov 03 05:12:48 1998 +0000
@@ -1,5 +1,21 @@
 Mon Nov  2 13:36:04 1998  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
+	* lex.l (handle_close_brace): Also handle case of ']' followed by
+	other assignment ops (+=, -=, ...).
+
+	* pt-assign.cc (tree_simple_assignment::rvalue): Correctly handle
+	return value and printing for operators other than `='.
+	(tree_multi_assignment::rvalue): Likewise.
+
+	* pt-assign.h (tree_multi_assignment::etype): New data member.
+	* pt-assign.cc 	(tree_multi_assignment::rvalue): Use it instead of
+	assuming `='.
+	(tree_multi_assignment::oper): New function.
+	* pt-pr-code.cc (tree_print_code::visit_multi_assignment): Use
+	it instead of always printing `='. 
+	* parse.y (make_assign_op): Pass expression type to
+	tree_multi_assignment constructor.	
+
 	* Makefile.in (stmp-pic): New target.
 	($(PICOBJ)): Depend on stmp-pic, not pic.
 	(clean): Delete stmp-pic.
--- a/src/lex.l	Tue Nov 03 04:04:38 1998 +0000
+++ b/src/lex.l	Tue Nov 03 05:12:48 1998 +0000
@@ -1613,9 +1613,95 @@
   return LEXICAL_ERROR;
 }
 
+static bool
+next_token_is_assign_op (void)
+{
+  bool retval = false;
+
+  int c0 = yyinput ();
+
+  switch (c0)
+    {
+    case '=':
+      {
+	int c1 = yyinput ();
+	unput (c1);
+	if (c1 != '=')
+	  retval = true;
+      }
+      break;
+
+    case '+':
+    case '-':
+    case '*':
+    case '/':
+    case '\\':
+    case '&':
+    case '|':
+      {
+	int c1 = yyinput ();
+	unput (c1);
+	if (c1 == '=')
+	  retval = true;
+      }
+      break;
+
+    case '.':
+      {
+	int c1 = yyinput ();
+	if (match_any (c1, "+-*/\\"))
+	  {
+	    int c2 = yyinput ();
+	    unput (c2);
+	    if (c2 == '=')
+	      retval = true;
+	  }
+	unput (c1);
+      }
+      break;
+
+    case '>':
+      {
+	int c1 = yyinput ();
+	if (c1 == '>')
+	  {
+	    int c2 = yyinput ();
+	    unput (c2);
+	    if (c2 == '=')
+	      retval = true;
+	  }
+	unput (c1);
+      }
+      break;
+
+    case '<':
+      {
+	int c1 = yyinput ();
+	if (c1 == '<')
+	  {
+	    int c2 = yyinput ();
+	    unput (c2);
+	    if (c2 == '=')
+	      retval = true;
+	  }
+	unput (c1);
+      }
+      break;
+
+    default:
+      break;
+    }
+
+  unput (c0);
+
+  return retval;
+}
+
 static int
 handle_close_brace (int spc_gobbled)
 {
+  int retval = ']';
+
   if (! nesting_level.none ())
     {
       nesting_level.remove ();
@@ -1625,26 +1711,13 @@
   if (lexer_flags.braceflag == 0)
     BEGIN 0;
 
-  // XXX FIXME XXX -- this needs to handle +=, -=, etc.
-
-  int c1 = yyinput ();
-  if (c1 == '=')
+  if (next_token_is_assign_op () && ! lexer_flags.looking_at_return_list)
     {
-      lexer_flags.quote_is_transpose = false;
-      lexer_flags.cant_be_identifier = false;
-      lexer_flags.convert_spaces_to_comma = true;
-
-      int c2 = yyinput ();
-      unput (c2);
-      unput (c1);
-
-      if (c2 == '=' || lexer_flags.looking_at_return_list)
-	return ']';
-      else
-	return CLOSE_BRACE;
+      retval = CLOSE_BRACE;
     }
   else
     {
+      int c1 = yyinput ();
       unput (c1);
 
       if (lexer_flags.braceflag && Vwhitespace_in_literal_matrix != 2)
@@ -1668,7 +1741,8 @@
   lexer_flags.quote_is_transpose = true;
   lexer_flags.cant_be_identifier = false;
   lexer_flags.convert_spaces_to_comma = true;
-  return ']';
+
+  return retval;
 }
 
 static void
--- a/src/parse.y	Tue Nov 03 04:04:38 1998 +0000
+++ b/src/parse.y	Tue Nov 03 05:12:48 1998 +0000
@@ -2218,7 +2218,7 @@
       delete lhs;
     }
   else
-    return new tree_multi_assignment (lhs, rhs, 0, l, c);
+    return new tree_multi_assignment (lhs, rhs, false, l, c, t);
 
   return retval;
 }
--- a/src/pt-assign.cc	Tue Nov 03 04:04:38 1998 +0000
+++ b/src/pt-assign.cc	Tue Nov 03 05:12:48 1998 +0000
@@ -76,10 +76,10 @@
 octave_value
 tree_simple_assignment::rvalue (void)
 {
-  octave_value rhs_val;
+  octave_value retval;
 
   if (error_state)
-    return rhs_val;
+    return retval;
 
   if (rhs)
     {
@@ -87,7 +87,7 @@
 
       if (! (error_state || tmp.empty ()))
 	{
-	  rhs_val = tmp(0);
+	  octave_value rhs_val = tmp(0);
 
 	  if (rhs_val.is_undefined ())
 	    {
@@ -104,25 +104,28 @@
 		{
 		  ult.assign (etype, rhs_val);
 
-		  // We clear any index here so that we can get the
-		  // new value of the referenced object below, instead
-		  // of the indexed value (which should be the same as
-		  // the right hand side value).
-
-		  ult.clear_index ();
+		  retval = ult.value ();
 
 		  if (error_state)
 		    eval_error ();
-		  else
+		  else if (print_result ())
 		    {
-		      octave_value lhs_val = ult.value ();
+		      if (Vprint_rhs_assign_val)
+			retval.print_with_name (octave_stdout,
+						lhs->str_print_code ());
+		      else
+			{
+			  // We clear any index here so that we can
+			  // get the new value of the referenced
+			  // object below, instead of the indexed
+			  // value (which should be the same as the
+			  // right hand side value).
 
-		      if (! error_state && print_result ())
-			{
-			  if (Vprint_rhs_assign_val)
-			    rhs_val.print_with_name (octave_stdout,
-						     lhs->str_print_code ());
-			  else
+			  ult.clear_index ();
+
+			  octave_value lhs_val = ult.value ();
+
+			  if (! error_state)
 			    lhs_val.print_with_name (octave_stdout,
 						     lhs->name ());
 			}
@@ -134,7 +137,7 @@
 	eval_error ();
     }
 
-  return rhs_val;
+  return retval;
 }
 
 void
@@ -192,18 +195,18 @@
 octave_value_list
 tree_multi_assignment::rvalue (int)
 {
-  octave_value_list rhs_val;
+  octave_value_list retval;
 
   if (error_state)
-    return rhs_val;
+    return retval;
 
   if (rhs)
     {
       int n_out = lhs->length ();
 
-      rhs_val = rhs->rvalue (n_out);
+      octave_value_list rhs_val = rhs->rvalue (n_out);
 
-      if (! (error_state || rhs_val.empty ()))
+      if (! error_state)
 	{
 	  if (rhs_val.empty ())
 	    {
@@ -216,6 +219,8 @@
 
 	      int n = rhs_val.length ();
 
+	      retval.resize (n, octave_value ());
+
 	      for (Pix p = lhs->first (); p != 0; lhs->next (p))
 		{
 		  tree_expression *lhs_elt = lhs->operator () (p);
@@ -226,57 +231,56 @@
 
 		      if (error_state)
 			eval_error ();
-		      else
+		      else if (k < n)
 			{
-			  octave_value tmp = k < n
-			    ? rhs_val(k++) : octave_value ();
+			  ult.assign (etype, rhs_val(k));
+
+			  retval(k) = ult.value ();
+			}
+		      else
+			error ("element number %d undefined in return list",
+			       k+1);
 
-			  if (tmp.is_defined ())
+		      if (error_state)
+			eval_error ();
+		      else if (print_result ())
+			{
+			  if (Vprint_rhs_assign_val)
+			    retval(k).print_with_name
+			      (octave_stdout, lhs_elt->str_print_code ());
+			  else
 			    {
-			      // XXX FIXME XXX -- handle other assignment ops.
-			      ult.assign (octave_value::asn_eq, tmp);
-
-			      // We clear any index here so that we
-			      // can get the new value of the
-			      // referenced object below, instead of
-			      // the indexed value (which should be
-			      // the same as the right hand side
-			      // value).
+			      // We clear any index here so that we can
+			      // get the new value of the referenced
+			      // object below, instead of the indexed
+			      // value (which should be the same as the
+			      // right hand side value).
 
 			      ult.clear_index ();
-			    }
-			  else
-			    error ("element number %d undefined in return list", k);
 
-			  if (error_state)
-			    eval_error ();
-			  else if (! Vprint_rhs_assign_val)
-			    {
 			      octave_value lhs_val = ult.value ();
 
-			      if (! error_state && print_result ())
-				{
-				  if (Vprint_rhs_assign_val)
-				    tmp.print_with_name (octave_stdout,
-							 lhs_elt->str_print_code ());
-				  else
-				    lhs_val.print_with_name (octave_stdout,
-							     lhs_elt->name ());
-				}
+			      if (! error_state)
+				lhs_val.print_with_name (octave_stdout,
+							 lhs_elt->name ());
 			    }
 			}
 		    }
+		  else
+		    eval_error ();
 
 		  if (error_state)
 		    break;
+
+		  k++;
 		}
 	    }
 	}
-      else
-	eval_error ();
     }
+  else
+    eval_error ();
 
-  return rhs_val;
+  return retval;
 }
 
 void
@@ -293,6 +297,12 @@
     }
 }
 
+string
+tree_multi_assignment::oper (void) const
+{
+  return octave_value::assign_op_as_string (etype);
+}
+
 void
 tree_multi_assignment::accept (tree_walker& tw)
 {
--- a/src/pt-assign.h	Tue Nov 03 04:04:38 1998 +0000
+++ b/src/pt-assign.h	Tue Nov 03 05:12:48 1998 +0000
@@ -116,12 +116,15 @@
 {
 public:
 
-  tree_multi_assignment (bool plhs = false, int l = -1, int c = -1)
-    : tree_expression (l, c), preserve (plhs), lhs (0), rhs (0) { }
+  tree_multi_assignment (bool plhs = false, int l = -1, int c = -1,
+			 octave_value::assign_op t = octave_value::asn_eq)
+    : tree_expression (l, c), lhs (0), rhs (0), preserve (plhs), etype(t) { }
 
   tree_multi_assignment (tree_argument_list *lst, tree_expression *r,
-			 bool plhs = false, int l = -1, int c = -1)
-    : tree_expression (l, c), preserve (plhs), lhs (lst), rhs (r) { }
+			 bool plhs = false, int l = -1, int c = -1,
+			  octave_value::assign_op t = octave_value::asn_eq)
+    : tree_expression (l, c), lhs (lst), rhs (r), preserve (plhs),
+      etype (t) { }
 
   ~tree_multi_assignment (void);
 
@@ -137,6 +140,8 @@
 
   void eval_error (void);
 
+  string oper (void) const;
+
   tree_argument_list *left_hand_side (void) { return lhs; }
 
   tree_expression *right_hand_side (void) { return rhs; }
@@ -145,10 +150,18 @@
 
 private:
 
-  bool preserve;
+  // The left hand side of the assignment.
   tree_argument_list *lhs;
+
+  // The right hand side of the assignment.
   tree_expression *rhs;
 
+  // True if we should not delete the lhs.
+  bool preserve;
+
+  // The type of the expression.
+  octave_value::assign_op etype;
+
   // No copying!
 
   tree_multi_assignment (const tree_multi_assignment&);
--- a/src/pt-pr-code.cc	Tue Nov 03 04:04:38 1998 +0000
+++ b/src/pt-pr-code.cc	Tue Nov 03 05:12:48 1998 +0000
@@ -527,7 +527,7 @@
 	os << "]";
     }
 
-  os << " = ";
+  os << " " << expr.oper () << " ";
 
   tree_expression *rhs = expr.right_hand_side ();