Skip to content

Commit

Permalink
- do{...} and loop{...} blocks are now subject to constant folding.
Browse files Browse the repository at this point in the history
Example:
	do { 1+2 }

will get translated into:

	do { 3 }
  • Loading branch information
trizen committed Nov 12, 2015
1 parent 8d3c57a commit b18f611
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/Sidef/Optimizer.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Sidef::Optimizer {

use 5.014;
require Scalar::Util;
use Scalar::Util qw(refaddr);

use constant {
STRING => 'Sidef::Types::String::String',
Expand Down Expand Up @@ -492,7 +492,7 @@ package Sidef::Optimizer {
## ok
}
elsif ($obj->{type} eq 'func' or $obj->{type} eq 'method') {
if ($addr{Scalar::Util::refaddr($obj)}++) {
if ($addr{refaddr($obj)}++) {
## ok
}
else {
Expand All @@ -501,15 +501,15 @@ package Sidef::Optimizer {
}
}
elsif ($ref eq 'Sidef::Variable::ClassInit') {
if ($addr{Scalar::Util::refaddr($obj)}++) {
if ($addr{refaddr($obj)}++) {
## ok
}
else {
$obj->{block} = $self->optimize_expr({self => $obj->{block}});
}
}
elsif ($ref eq 'Sidef::Variable::Init') {
if ($addr{Scalar::Util::refaddr($obj)}++) {
if ($addr{refaddr($obj)}++) {
## ok
}
else {
Expand All @@ -519,8 +519,26 @@ package Sidef::Optimizer {
}
}
}
elsif ($ref eq 'Sidef::Types::Block::Do') {
if ($addr{refaddr($obj)}++) {
## ok
}
else {
my %code = $self->optimize($obj->{block}{code});
$obj->{block}{code} = \%code;
}
}
elsif ($ref eq 'Sidef::Types::Block::Loop') {
if ($addr{refaddr($obj)}++) {
## ok
}
else {
my %code = $self->optimize($obj->{block}{code});
$obj->{block}{code} = \%code;
}
}
elsif ($ref eq 'Sidef::Types::Block::CodeInit') {
if ($addr{Scalar::Util::refaddr($obj)}++) {
if ($addr{refaddr($obj)}++) {
## ok
}
else {
Expand Down

0 comments on commit b18f611

Please sign in to comment.