array element assignment statement semantics

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

array element assignment statement semantics

dlk
Hi,

In an assignment statement for an array element

  let arr[expr1] = expr2;

where expr1 and expr2 are arbitrary expressions, does the Jack language specify whether
expr1 or expr2 is evaluated first? It matters if the expressions have side-effects, such that
evaluating expr2 changes the value of expr1, or vice-versa. Also, i suppose expr2 or expr1 could
affect the value of arr, particularly if arr were a class field or static variable. So, I suppose I need to know
what order Jack defines for the evaluation of arr, expr1, and expr2.

- Dan

P.S. Sorry if this is in the text; I don't have it.
dlk
Reply | Threaded
Open this post in threaded view
|

Re: array element assignment statement semantics

dlk
This post was updated on .
Hi,

I guess I also need to know when evaluating a binary operation
like (expr1 + expr2), or even (expr1 - expr2), what order (if any) Jack defines for the
evaluation of expr1 and expr2; since with side-effects, the evaluation of one could
affect the value (or behavior) of the other.

- Dan
Reply | Threaded
Open this post in threaded view
|

Re: array element assignment statement semantics

cadet1620
Administrator
From 9.2.5 in the book
Operator Priority and Order of Evaluation Operator priority is not defined by the language, except that expressions in parentheses are evaluated first. Thus an expression like 2+3*4 may yield either 20 or 14, whereas 2+(3*4) is guaranteed to yield 14. The need to use parentheses in such expressions makes Jack programming a bit cumbersome. However, the lack of formal operator priority is intentional, since it simplifies the writing of Jack compilers. Of course, different language implementations (compilers) can specify an operator priority and add it to the language documentation, if so desired.
There is no discussion of side effects, but in keeping with the general philosophy of the book, I'd say that anything goes.

That said, the parsing described in the book results in strictly left to right evaluation of subroutine calls within statements.

--Mark
   
dlk
Reply | Threaded
Open this post in threaded view
|

Re: array element assignment statement semantics

dlk
Hi Mark,
Thanks; I had a hunch it would be undefined...