assignment: left-to-right or right-to-left evaluation?

Steven Schveighoffer schveiguy at yahoo.com
Mon May 11 04:45:26 PDT 2009


On Mon, 11 May 2009 07:34:38 -0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> For example:
>
> mydic[x] = mydic[y] = mydic[z] = mydic.length;
>
> if evaluating right to left, this looks like:
>
> 1. calculate mydic.length, store it in register A.
> 2. lookup mydic[z], if it doesn't exist, add it.  Store register A to it.
> 3. lookup mydic[y], if it doesn't exist, add it.  Store register A to it.
> 4. ditto for mydic[x]
>
> If evaluating left to right, this looks like:
>
> 1. lookup mydic[x], if it doesn't exist, add it.  Store a reference to  
> it on the stack.
> 2. lookup mydic[y], if it doesn't exist, add it.  Store a reference to  
> it on the stack.
> 3. lookup mydic[z], if it doesn't eixst, add it.  Store the reference to  
> it in register B.
> 4. calculate mydic.length, store it in register A.  Store the result in  
> the reference pointed to by register B.
> 5. pop register B from the stack, store register A to the value it  
> references.
> 6. Repeat step 5.
>
> Two extra steps, and I have to use a stack.  Maybe 3 chained assignments  
> would be easy to store without a stack, but try 10 chained assignments.
>
> I'd think the compiler code to evaluate right to left would be simpler  
> also, because you can reduce the expression at every assignment.

BTW, I'm curious to know how Java does this...

-Steve



More information about the Digitalmars-d mailing list