Questions about eval

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Questions about eval

John W. Eaton
Administrator
I noticed that Matlab's documentation for eval no longer mentions the form

   eval (try_code, catch_code)

to evaluate the "catch_code" if "try_code" throws an error.  Should we
also deprecate this feature in Octave?

Does Matlab restrict the code to be evaluated to be a single expression,
or are statements and multiple expressions allowed?  For example, what
happens for the following in current Matlab versions?

   eval ('x = 2+2')

   eval ('1+1, pi')

   eval ('for i = 1:3, i, end')

In Octave, all of these work.  If Matlab restricts the argument of eval
to be a single expression (not a statement) then should we also impose
that limitation?  It might simplify the implementation somewhat if we
did not have to handle arbitrary code.

Thanks,

jwe


Reply | Threaded
Open this post in threaded view
|

Re: Questions about eval

apjanke-floss


On 1/29/19 3:53 PM, John W. Eaton wrote:

> I noticed that Matlab's documentation for eval no longer mentions the form
>
>    eval (try_code, catch_code)
>
> to evaluate the "catch_code" if "try_code" throws an error.  Should we
> also deprecate this feature in Octave?
>
> Does Matlab restrict the code to be evaluated to be a single expression,
> or are statements and multiple expressions allowed?  For example, what
> happens for the following in current Matlab versions?
>
>    eval ('x = 2+2')
>
>    eval ('1+1, pi')
>
>    eval ('for i = 1:3, i, end')
>
> In Octave, all of these work.  If Matlab restricts the argument of eval
> to be a single expression (not a statement) then should we also impose
> that limitation?  It might simplify the implementation somewhat if we
> did not have to handle arbitrary code.
>
> Thanks,
>
> jwe

In Matlab, statements (including assignment) and compound expressions
(multiple expressions joined by semicolons or commas) are allowed in
eval. All your examples work.

If you use the output-capturing form `x = eval(expr)` then it needs to
be a single non-compound, non-assignment-statement expression.

Cheers,
Andrew

Reply | Threaded
Open this post in threaded view
|

Re: Questions about eval

John W. Eaton
Administrator
On 1/29/19 4:01 PM, Andrew Janke wrote:
 > In Matlab, statements (including assignment) and compound expressions
 > (multiple expressions joined by semicolons or commas) are allowed in
 > eval. All your examples work.

Thanks.

 > If you use the output-capturing form `x = eval(expr)` then it needs to
 > be a single non-compound, non-assignment-statement expression.

Does this appear to be a restriction on parsing or evaluation?  In other
words, does something like

   x = eval ('y = 1')

fail to parse the assignment statement or does it perform the assignment
to Y and then fail to assign anything to X because (as I remember
anyway) assignments in Matlab are statements and do not produce values.

Similarly, if you try

   x = eval ('for i = 1:3, i, end')

does the error happen when parsing the for loop statement or after the
loop is executed (no value to assign)?

jwe

Reply | Threaded
Open this post in threaded view
|

Re: Questions about eval

apjanke-floss


On 1/29/19 4:14 PM, John W. Eaton wrote:

> On 1/29/19 4:01 PM, Andrew Janke wrote:
>  > In Matlab, statements (including assignment) and compound expressions
>  > (multiple expressions joined by semicolons or commas) are allowed in
>  > eval. All your examples work.
>
> Thanks.
>
>  > If you use the output-capturing form `x = eval(expr)` then it needs to
>  > be a single non-compound, non-assignment-statement expression.
>
> Does this appear to be a restriction on parsing or evaluation?  In other
> words, does something like
>
>    x = eval ('y = 1')
>
> fail to parse the assignment statement or does it perform the assignment
> to Y and then fail to assign anything to X because (as I remember
> anyway) assignments in Matlab are statements and do not produce values.
>
> Similarly, if you try
>
>    x = eval ('for i = 1:3, i, end')
>
> does the error happen when parsing the for loop statement or after the
> loop is executed (no value to assign)?
>
> jwe

IIRC, the error happens at parse time, and eval balks at evaluating that
code, giving you some sort of syntax error before any of it is evaluated.

Cheers,
Andrew

Reply | Threaded
Open this post in threaded view
|

Re: Questions about eval

John W. Eaton
Administrator
On 1/29/19 4:19 PM, Andrew Janke wrote:

> IIRC, the error happens at parse time, and eval balks at evaluating that
> code, giving you some sort of syntax error before any of it is evaluated.

OK.?

Since assignment in Octave does produce a value, I think we would want
to continue allowing

   x = eval ('y = 1')

to also assign a value to X.

I thought that Octave allowed both of the following

   x = eval ('y = 1; z = 2;')

   x = eval ('for i = 1:3, i, end')

to perform the evaluations, but I see that neither of these work in
current Octave and they also fail at when parsing.  So I guess
compatibility here is already reasonably good.

My question about what to do for

   eval (try_code, catch_code)

remains.  Does anyone have comments about that?  If not, I can open a
bug report so that the issue is not forgotten.

jwe



Reply | Threaded
Open this post in threaded view
|

Re: Questions about eval

siko1056
On Tue, Jan 29, 2019 at 10:40 PM John W. Eaton <[hidden email]> wrote:
On 1/29/19 4:19 PM, Andrew Janke wrote:

> IIRC, the error happens at parse time, and eval balks at evaluating that
> code, giving you some sort of syntax error before any of it is evaluated.

OK.?

Since assignment in Octave does produce a value, I think we would want
to continue allowing

   x = eval ('y = 1')

Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality, use '=='.
 

to also assign a value to X.

I thought that Octave allowed both of the following

   x = eval ('y = 1; z = 2;')

Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality, use '=='.
 

   x = eval ('for i = 1:3, i, end')

Error: Illegal use of reserved keyword "for".
 

to perform the evaluations, but I see that neither of these work in
current Octave and they also fail at when parsing.  So I guess
compatibility here is already reasonably good.

My question about what to do for

   eval (try_code, catch_code)

>> eval ('x=1', 'x=2')

x =

     1

>> eval ('x=sin', 'x=2')

x =

     2 


remains.  Does anyone have comments about that?  If not, I can open a
bug report so that the issue is not forgotten.

jwe




The results above are from Matlab R2018b and except for "eval ('y = 1')" Octave 5.0.90 does almost the same.
So I think Octave should keep this eval (try_code, catch_code) too. (Btw. thanks I didn't know of this eval-feature ^^).

Best,
Kai
 
Reply | Threaded
Open this post in threaded view
|

Re: Questions about eval

John W. Eaton
Administrator
On 1/30/19 5:53 AM, Kai Torben Ohlhus wrote:

> The results above are from Matlab R2018b and except for "eval ('y = 1')"
> Octave 5.0.90 does almost the same.

Thanks.  I now see that we are more compatible than I thought.  I
suppose we can leave things as they are, but I will try to fix
differences that I've found recently with parsing command-style syntax
in eval.

> So I think Octave should keep this eval (try_code, catch_code) too.
> (Btw. thanks I didn't know of this eval-feature ^^).

It is also in Matlab, just undocumented now.

jwe