Funny behavior in a squaak 'for' loop

Submitted by jayemerson on Sun, 07/17/2011 - 04:19

This may seem like an unusual blog post: why not a post to parrot-dev? Well, I've struggled the past few weeks as a newbie, largely with language syntax. But this problem is different: this is an interesting problem, one that I think shows that language/compiler design is more than just mastering basic language syntax and getting something to run approximately.

For readers unfamiliar with squaak, I think it is a teaching tool, not a finished high-level language (HLL) that would likely be used to get some job done.

# In squaak:
for var i = 0, 10000 do
a[i] = i
end
print(a[0])
print(a[10000])

# Question 1: Why is the answer:
10001
10001
# and not
0
10000
??? My guess: there are hidden PMCs here, so the pointer is being assigned rather than the value, and so the value of i at the end is really just being pointed to? In other words, it may not be a 'for' loop problem at all.

The simplest way I can think of to fix it is to change the action for the for loop to not use the increment op and instead use the add opcode to create a new Integer PMC for each iteration of the loop.