simonask / snow-deprecated

(ATTENTION WATCHERS: This repository is deprecated. Please watch 'snow' instead.) Snow is a simple, dynamic, and expressive programming language with an emphasis on speed and simplicity. Snow is fully object-oriented, and fully function-oriented at the same time.
http://snow.meta.io/
Other
21 stars 1 forks source link

Recursive closures behaving oddly. #45

Open Mikoangelo opened 15 years ago

Mikoangelo commented 15 years ago

The following, rather simple, script gives unexpected results:

MyClass: Object.new()
MyClass.object_eval {
  .new: {
    instance: Object.new(self)
    instance.subs: @()
    instance
  }

  .count_it: [level] {
    puts("${level} Calculating count")
    wrapper: Object.new()
    wrapper.count: 1
    .subs.each [sub] {
      wrapper.count: wrapper.count + sub.count_it(level+1)
    }
    puts("${level} Count calculated to be: ${wrapper.count}")
    wrapper.count
  }
}

main: MyClass.new()
main.subs.push(MyClass.new())
main.subs.push(MyClass.new())

puts("Final result: " + main.count_it(0))

Running it with snow gives the following output:

0 Calculating count
1 Calculating count
1 Count calculated to be: 1
2 Calculating count
2 Count calculated to be: 1
0 Count calculated to be: 1
Final result: 1

I would expect level to never reach 2, and the two last lines to end in 2. Currently I'm using a cache in a member to circumvent this issue.

simonask commented 15 years ago

Looks like a codegen problem. Please provide arch and revision. :)

Mikoangelo commented 15 years ago

Oh, sorry. Mac OS X 1.5.7, x86_64, 9704c6ac454698a2268f840daaa56c012c239571.