mindcode icon indicating copy to clipboard operation
mindcode copied to clipboard

Expressions containing variable assignments possibly not correctly evaluated

Open cardillan opened this issue 1 year ago • 0 comments

Inspired by https://github.com/mlogjs/mlogjs/issues/180.

Variables reused in expressions that modify them do not preserve the original value. This code:

a = 1
print(a + (a += 1))
printflush(message1)

currently prints "4".

I'm not entirely sure how to handle this issue. It would be possible to save the value of a before the expression is evaluated, which would produce "3", an output that would be expected. On the other hand, print((a += 1) + a) might also produce "3" after the fix - depending on the details of the implementation - which perhaps isn't entirely expected either.

In C, behavior of similar constructs is explicitly declared as undefined. There is little practical value in supporting this kind of constructs in a language, and we might just left them undefined in Mindcode too. Otherwise, what the heck to do with cell1[i] = (i += 1)?

cardillan avatar Jun 11 '23 18:06 cardillan