Skip to content

Commit

Permalink
- All variables are now block-scoped instead of function-scoped
Browse files Browse the repository at this point in the history
This fixes some bugs related to the creation of closures inside blocks.
  • Loading branch information
trizen committed Nov 24, 2015
1 parent 3b1e788 commit 3507cad
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ HEADER
ref($var) || next;
if (exists $var->{array}) {
my $name = $var->{name} . refaddr($var);
push @{$self->{function_declarations}}, [-1, 'my @' . $name . ';'];
push @{$self->{block_declarations}}, [$self->{current_block} // -1, 'my @' . $name . ';'];

# Overwrite with the default values, when the array is empty
if (exists $var->{value}) {
Expand All @@ -195,7 +195,7 @@ HEADER
}
elsif (exists $var->{hash}) {
my $name = $var->{name} . refaddr($var);
push @{$self->{function_declarations}}, [-1, 'my %' . $name . ';'];
push @{$self->{block_declarations}}, [$self->{current_block} // -1, 'my %' . $name . ';'];

# Overwrite with the default values, when the hash has no keys
if (exists $var->{value}) {
Expand All @@ -218,8 +218,8 @@ HEADER
}
}

# XXX: should we store the declaration in something like 'block_declarations'?
push @{$self->{function_declarations}}, [-1, 'my(' . join(', ', map { $self->_dump_var($_) } @vars) . ')' . ';'];
push @{$self->{block_declarations}},
[$self->{current_block} // -1, 'my(' . join(', ', map { $self->_dump_var($_) } @vars) . ')' . ';'];

# Return the variables on assignments
if (@code > 1 or exists($init_obj->{args})) {
Expand Down Expand Up @@ -669,6 +669,8 @@ HEADER
my $is_function = exists($self->{function}) && $self->{function} == $refaddr;
my $is_class = exists($self->{class}) && $self->{class} == $refaddr;

local $self->{current_block} = $refaddr;

if ($is_class) {
$code = " {\n";

Expand Down Expand Up @@ -746,6 +748,12 @@ HEADER
}
}

while ( exists($self->{block_declarations})
and @{$self->{block_declarations}}
and $self->{block_declarations}[-1][0] == $refaddr) {
$code .= (' ' x $Sidef::SPACES) . pop(@{$self->{block_declarations}})->[1] . "\n";
}

# Make the last statement to be the return value
if ($is_function && @statements) {

Expand Down Expand Up @@ -975,6 +983,7 @@ HEADER
local \$Sidef::DEPARSER->{top_program} = '';
local \$Sidef::DEPARSER->{_has_constant} = 0;
local \$Sidef::DEPARSER->{function_declarations} = [];
local \$Sidef::DEPARSER->{block_declarations} = [];
\$Sidef::DEPARSER->deparse(
do {
local \$Sidef::PARSER->{vars} = \$Sidef::EVALS{$refaddr}{vars};
Expand Down Expand Up @@ -1418,6 +1427,11 @@ HEADER
? ("\n" . join("\n", map { $_->[1] } @{$self->{function_declarations}}) . "\n")
: ''
)
. (
exists($self->{block_declarations})
&& @{$self->{block_declarations}} ? ("\n" . join("\n", map { $_->[1] } @{$self->{block_declarations}}) . "\n")
: ''
)
. $self->{top_program} . "\n"
. join($self->{between}, @statements)
. $self->{after}
Expand Down

0 comments on commit 3507cad

Please sign in to comment.