octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: more compatibility changes


From: John W. Eaton
Subject: Re: more compatibility changes
Date: Fri, 11 Jul 2003 23:44:04 -0500

On 11-Jul-2003, Paul Kienzle <address@hidden> wrote:

| >  * Remove the built-in variable default_eval_print_flag and only
| >    implement Matlab-compatible behavior, or make the default value
| >    Matlab compatible.
| >
| This just means putting ; after eval --- no big deal.

After looking at this some more, I see that there are some differences
in the way Octave an Matlab behave when running eval.

In Matlab, 

  x = 1;
  eval ('x')       ==> prints x = 1
  eval ('x;')      ==> evaluates x (?), nothing is printed
  eval ('x');      ==> prints x = 1
  eval ('x;');     ==> evaluates x (?), nothing is printed
  y = eval ('x')   ==> assigns 1 to y, prints y = 1
  y = eval ('x');  ==> assigns 1 to y, nothing is printed
  y = eval ('x;')  ==> assigns 1 to y, prints y = 1
  y = eval ('x;'); ==> assigns 1 to y, nothing is printed

In Octave, with default_eval_print_flag = 1,

  x = 1;
  eval ('x')       ==> prints x = 1, ans = 1
  eval ('x;')      ==> prints ans = 1
  eval ('x');      ==> prints x = 1
  eval ('x;');     ==> evaluates x, nothing is printed
  y = eval ('x')   ==> assigns 1 to y, prints y = 1
  y = eval ('x');  ==> assigns 1 to y, nothing is printed
  y = eval ('x;')  ==> assigns 1 to y, prints y = 1
  y = eval ('x;'); ==> assigns 1 to y, nothing is printed

In Octave, with default_eval_print_flag = 0,

  x = 1;
  eval ('x')       ==> prints ans = 1
  eval ('x;')      ==> prints ans = 1
  eval ('x');      ==> evaluates x, nothing is printed
  eval ('x;');     ==> evaluates x, nothing is printed
  y = eval ('x')   ==> assigns 1 to y, prints y = 1
  y = eval ('x');  ==> assigns 1 to y, nothing is printed
  y = eval ('x;')  ==> assigns 1 to y, prints y = 1
  y = eval ('x;'); ==> assigns 1 to y, nothing is printed

So I think I misunderstood what was happening when I implemented this
"feature".  It seems that for compatibility, what should really happen
is that Octave should be changed to behave as though

  default_eval_print_flag is true when nargout is zero; false otherwise

  return nothing if nargout is zero

The following patch implements this and default_eval_print_flag is no
longer needed.  It also affects evalin, so that its behavior remains
consistent with eval.

Does this seem reasonable?

Thanks,

jwe


2003-07-11  John W. Eaton  <address@hidden>

        * parse.y (Vdefault_eval_print_flag): Delete: 
        (default_eval_print_flag): Delete.
        (symbols_of_parse): Delete DEFVAR for default_eval_print_flag.
        (Feval, Fevalin): Return empty octave_value_list and turn printing
        on in eval_string if nargout is zero.


Index: parse.y
===================================================================
RCS file: /usr/local/cvsroot/octave/src/parse.y,v
retrieving revision 1.207
diff -u -r1.207 parse.y
--- parse.y     9 Jul 2003 23:20:19 -0000       1.207
+++ parse.y     12 Jul 2003 04:38:23 -0000
@@ -69,9 +69,6 @@
 #include "utils.h"
 #include "variables.h"
 
-// TRUE means we print 
-static bool Vdefault_eval_print_flag = true;
-
 // If TRUE, generate a warning for the assignment in things like
 //
 //   octave> if (a = 2 < n)
@@ -3745,33 +3742,9 @@
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} eval (@var{try}, @var{catch})\n\
 Parse the string @var{try} and evaluate it as if it were an Octave\n\
-program, returning the last value computed.  If that fails, evaluate\n\
-the string @var{catch}.  The string @var{try} is evaluated in the\n\
-current context, so any results remain available after @code{eval}\n\
-returns.  For example,\n\
-\n\
address@hidden
address@hidden
-eval (\"a = 13\")\n\
-     @print{} a = 13\n\
-     @result{} 13\n\
address@hidden group\n\
address@hidden example\n\
-\n\
-In this case, the value of the evaluated expression is printed and it is\n\
-also returned returned from @code{eval}.  Just as with any other\n\
-expression, you can turn printing off by ending the expression in a\n\
-semicolon.  For example,\n\
-\n\
address@hidden
-eval (\"a = 13;\")\n\
-     @result{} 13\n\
address@hidden example\n\
-\n\
-In this example, the variable @code{a} has been given the value 13, but\n\
-the value of the expression is not printed.  You can also turn off\n\
-automatic printing for all expressions executed by @code{eval} using the\n\
-variable @code{default_eval_print_flag}.\n\
+program.  If that fails, evaluate the string @var{catch}.\n\
+The string @var{try} is evaluated in the current context,\n\
+so any results remain available after @code{eval} returns.\n\
 @end deftypefn")
 {
   octave_value_list retval;
@@ -3790,8 +3763,11 @@
 
       int parse_status = 0;
 
-      retval = eval_string (args(0), ! Vdefault_eval_print_flag,
-                           parse_status, nargout);
+      octave_value_list tmp = eval_string (args(0), nargout > 0,
+                                          parse_status, nargout);
+
+      if (nargout > 0)
+       retval = tmp;
 
       if (nargin > 1 && (parse_status != 0 || error_state))
        {
@@ -3924,8 +3900,11 @@
 
              int parse_status = 0;
 
-             retval = eval_string (args(1), ! Vdefault_eval_print_flag,
-                                   parse_status, nargout);
+             octave_value_list tmp = eval_string (args(1), nargout > 0,
+                                                  parse_status, nargout);
+
+             if (nargout > 0)
+               retval = tmp;
 
              if (nargin > 2 && (parse_status != 0 || error_state))
                {
@@ -3958,14 +3937,6 @@
 }
 
 static int
-default_eval_print_flag (void)
-{
-  Vdefault_eval_print_flag = check_preference ("default_eval_print_flag");
-
-  return 0;
-}
-
-static int
 warn_assign_as_truth_value (void)
 {
   Vwarn_assign_as_truth_value
@@ -4018,14 +3989,6 @@
 void
 symbols_of_parse (void)
 {
-  DEFVAR (default_eval_print_flag, true, default_eval_print_flag,
-    "-*- texinfo -*-\n\
address@hidden {Built-in Variable} default_eval_print_flag\n\
-If the value of this variable is nonzero, Octave prints the results of\n\
-commands executed by @code{eval} that do not end with semicolons.  If it\n\
-is zero, automatic printing is suppressed.  The default value is 1.\n\
address@hidden defvr");
-
   DEFVAR (warn_assign_as_truth_value, true, warn_assign_as_truth_value,
     "-*- texinfo -*-\n\
 @defvr {Built-in Variable} warn_assign_as_truth_value\n\



reply via email to

[Prev in Thread] Current Thread [Next in Thread]