Michal Wallace <[EMAIL PROTECTED]> writes: > Okay, I definitely need some help understanding this.
Okay, I definitely did a suboptimal job trying to clarify... > Here's some python code that defines a closure: > > def make_adder(base): > def adder(x): > return base+x > return adder > h = make_adder(10) > print h(5) > > When I run this in python 2.2, it prints "15". > > When I run it through pirate, I get this: > > -scratch_pad: too deep > > Now, in my mind: > > depth 0 has: make_adder, h > depth 1 has: base, adder > depth 2 has: x This sounds right. > The top level .sub should start with 'new_pad 0' > The make_adder .pcc_sub should start with 'new_pad 1' > The adder .pcc_sub should start with 'new_pad 2' Right. > I think the error happens because I'm calling a depth 2 > function from depth 0, but if I change adder's new_pad > depth to 1, it can't find "base". > > I don't know how to get this to work the way I want. > Can anyone help me out here? > ... > # make_adder from line 2 > .pcc_sub _sub0 non_prototyped > .param object base > new_pad 1 > store_lex -1, 'base', base > setline 3 > newsub $P1, .Sub, _sub1 The problem is that when adder() gets returned, it needs to remember the enclosing pad. So this needs to be newsub $P1, .Closure, _sub1 which (IIRC) will save the lexical environment in which it was created (see closure.pmc), then restore that when it is invoked. /s