[Nickle]Sequencing of expressions with side effects

Carl Worth nickle@nickle.org
Tue, 23 Jul 2002 15:42:09 +0000


I noticed that nickle includes the unary increment/decrement operators
from C and I started wondering about the sequencing of their side
effects. In C, the behavior of these operators makes possible
statements such as:

	a = a++;

the behavior of which is undefined.

Do nickle's semantics prevent such undefined behavior? I couldn't find
any definition of ++/-- nor any discussion of the sequencing of the
side effects of expressions.

I've done a little experimenting, and empirically, it seems that a
working definition for ++a could be (a=a+1). The postfix form is a bit
trickier, but defining a++ as ((poly t), a=a+1, t) seems to do the
trick for all cases that I have tested.

Are those accurate definitions?

As I was abusing nickle along these lines, I discovered an oddity with
the += operator. The man page mentions that a += b should be the same
as a = a + b, but I found cases where that does not hold, (again
involving expressions with side effects):

	> a=1; a += ++a      
	4
	> a=1; a = a + (++a) 
	3

This doesn't rely on the unary increment operator appearing in the
right-hand expression. We can replace it with the definition I used
above and still get the same result:

	> a=1; a += (a = a+1)
	4
	> a=1; a = a + (a = a+1)
	3

Is this behavior "correct"? What are the defined semantics for
assignment side-effects within expressions in nickle?

-Carl

-- 
Carl Worth                                        
USC Information Sciences Institute                 cworth@east.isi.edu
3811 N. Fairfax Dr. #200, Arlington VA 22203		  703-812-3725