[comp.lang.pascal] Pascal Assignment statements

rcosw@koel.co.rmit.oz (Simon Wail) (10/05/89)

I have a question about expression evaluation in assignment statements in
Standard Pascal.

Q. Is all address calculations of the assignment variable performed before
   the expression on the R.H.S. is evaluated???

Some examples that cause problems :

x[a+b] := f;

	where "f" is a function that modifies the global variables "a" and "b"
	which are used to index the array on the L.H.S.  Is the index to the
	array calculated before "f" is called???

x[5].ptr^.y := f;

	where "f" is a function that modifies the fifth element of the 
	array "x" so "ptr" points to a different record.  Is the old value
	of "ptr" used to modify "y" or the new value of "ptr" changed 
	in "f"????

I know these are examples of terrible side-effects and bad programming style,
but when one is writing a Pascal compiler it is necessary to take into account
all the possibilities and to be consistent when evaluating expressions.

Thanks.
-------------------------------------------------------------------------------
Simon Wail,
Post Graduate Student.

ACSnet: rcosw@koel.co.rmit.oz	UUCP: ...!uunet!munnari!koel.co.rmit.oz!rcosw
PHONE:  + 61 3 660 2726		ARPA: rcosw%koel.co.rmit.oz@uunet.uu.net

RMIT / CSIRO Parallel Systems Architecture Project,
Department of Communication and Electrical Engineering,
Royal Melbourne Institute of Technology,
P.O. Box 2476V,
Latrobe St, Melbourne. 3000. Australia.

reeder@reed.UUCP (Doug Reeder) (10/07/89)

In article <723@chudich.co.rmit.oz> rcosw@koel.co.rmit.oz (Simon Wail) writes:

>Q. Is all address calculations of the assignment variable performed before
>   the expression on the R.H.S. is evaluated???

>Some examples that cause problems :

>x[a+b] := f;

>	where "f" is a function that modifies the global variables "a" and "b"
>	which are used to index the array on the L.H.S.  Is the index to the
>	array calculated before "f" is called???


From "Pascal For Programmers" by Lecarme & Nebutt (McGraw-Hill 1984) p27-28:

"The order in which the two sides of the assignment statement are evaluated
is implementation-dependent; i.e., a correct program must not depend on this
order."

Also, the terms in an expression are evaluated in an implementation-defined
manner. Thus, you can do whatever you like and don't even have to always do 
the same thing.  The book, by the way, is an excellent reference for the
experienced programmer.  I consult mine all the time.

-- 
Doug Reeder                          USENET: ...!tektronix!reed!reeder
Box 722 Reed College                 BITNET: reeder@reed.BITNET
Portland, OR 97202                from ARPA: tektronix!reed!reeder@berkeley.EDU
(503) 777-1551                  "A blaster can point two ways."  -Salvor Hardin