Funny behavior in a squaak 'for' loop

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.

# Question 2: How can squaak be changed to fix this up?

Integer PMCs

I'm not sure if you've already gotten an answer, but here's my $0.02:

i is probably a reference to an Integer PMC, an object that stores an integer value. a is a ResizablePMCArray. At the end of the loop, you have 10000 references to the same integer PMC.

I can describe it succinctly in C++.

int *i = new int;
int *a[10000]; // array of pointers to int
for( *i = 0; *i <= 10000; *i++ ) {  // increments the value
   a[*i] = i; // stores the pointer
}

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.