> Sorry, the example was unnecessarily long.
>
> > After a quick reading of this, I'd have expected the value of "f" at the indicated
> > point to be 1, but instead it's 2.
>
> .local int f
> .sub _main
> .local int x
> .sub _foo1
> f=1
> x=2
> call _foo2
> end
> .end
> .sub _foo2
> print "f is 1: "
> print f
> print "\n"
> ret
> .end
> .end
Yeah, I don't think you can use .local across subs like that. I think
.local means "local to this sub" and *inner subs aren't closures*. In
fact, I don't think inner subs are useful for much of anything at all.
If you're a compiler, what you probably want to do is set up a some
scratchpads and put f and x in them. So the perl code:
my $f;
{
my $x;
foo1();
sub foo1 { ($f, $x) = (1, 2); foo2() }
sub foo2 { print "f is 1: $f\n"; print "x is 2: $x\n" }
}
Is roughly equivalent to:
# Main, unlabeled program
.sub _main
.local PerlInt f
f = new PerlInt
new_pad 0
store_lex -1, "f", f
call _anon1
end
.end
# Outermost { }
.sub _anon1
.local PerlInt x
x = new PerlInt
new_pad 1
store_lex -1, "x", x
call _foo1
ret
.end
.sub _foo1
.local PerlInt f
.local PerlInt x
f = new PerlInt
x = new PerlInt
f = 1
x = 2
store_lex "f", f
store_lex "x", x
call _foo2
ret
.end
.sub _foo2
.local PerlInt f
.local PerlInt x
find_lex f, "f"
print "f is 1: "
print f
print "\n"
find_lex x, "x"
print "x is 2: "
print x
print "\n"
ret
.end
, Unfortunately.