Skip to content

Commit

Permalink
- Minor improvements to the -O3 optimization level;
Browse files Browse the repository at this point in the history
It now supports DATA fh, local variables and Sys method calls, in addition to optimizations for STDERR, STDOUT, STDIN and ARGF filehandles.

- Simplified the Sys.printh() method;

- Replaced
	$obj->can('run')
with
	$obj->SUPER::isa('Sidef::Types::Block::Code')

which is much safer, but less flexible.
  • Loading branch information
trizen committed Sep 8, 2015
1 parent e5f7f26 commit 376d7bf
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 33 deletions.
21 changes: 12 additions & 9 deletions lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ HEADER
}
}
elsif ($ref eq 'Sidef::Variable::InitLocal') {
$code = "my $obj->{name}";
$code = "local $obj->{name}";
}
elsif ($ref eq 'Sidef::Variable::Local') {
$code = "$obj->{name}";
Expand Down Expand Up @@ -306,7 +306,7 @@ HEADER

$code .= "\n";
$code .= " " x $Sidef::SPACES;
$code .= 'sub init {';
$code .= 'sub new {';
$code .= "\n";

$Sidef::SPACES += $Sidef::SPACES_INCR;
Expand Down Expand Up @@ -370,7 +370,7 @@ HEADER

if (not $is_class) {
if ($#vars == 0 and $vars[0]{name} eq '_') {
$code .= ' ' x $Sidef::SPACES . "\$_ = Sidef::Types::Array::Array->new(\@_) if \@_;\n";
$code .= ' ' x $Sidef::SPACES . "\$_ = \$_[0] if exists \$_[0];\n";
}
else {
foreach my $i (0 .. $#{vars}) {
Expand Down Expand Up @@ -406,7 +406,7 @@ HEADER
$code = $ref . '->new';
}
elsif ($ref eq 'Sidef::Sys::Sys') {
$code = exists($obj->{file_name}) ? '' : $self->make_constant($ref, 'Sys');
$code = $self->make_constant($ref, 'Sys');
}
elsif ($ref eq 'Sidef::Parser') {
$code = $ref . '->new';
Expand Down Expand Up @@ -450,19 +450,22 @@ HEADER
}
elsif ($ref eq 'Sidef::Types::Glob::FileHandle') {
if ($obj->{fh} eq \*STDIN) {
$code = $ref . '->new(fh => \*STDIN)';
$code = $self->make_constant($ref, 'STDIN', 'fh => \*STDIN');
}
elsif ($obj->{fh} eq \*STDOUT) {
$code = $ref . '->new(fh => \*STDOUT)';
$code = $self->make_constant($ref, 'STDOUT', 'fh => \*STDOUT');
}
elsif ($obj->{fh} eq \*STDERR) {
$code = $ref . '->new(fh => \*STDERR)';
$code = $self->make_constant($ref, 'STDERR', 'fh => \*STDERR');
}
elsif ($obj->{fh} eq \*ARGV) {
$code = $ref . '->new(fh => \*ARGV)';
$code = $self->make_constant($ref, 'ARGF', 'fh => \*ARGV');
}
else {
$code = $ref . '->new(fh => \*DATA)';
my $data = quotemeta(
do { seek($obj->{fh}, 0, 0); local $/; readline($obj->{fh}) }
);
$code = $self->make_constant($ref, 'DATA', qq{fh => do {open my \$fh, '<', \\"$data"; \$fh}});
}
}
elsif ($ref eq 'Sidef::Variable::Magic') {
Expand Down
6 changes: 1 addition & 5 deletions lib/Sidef/Sys/Sys.pm
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,8 @@ package Sidef::Sys::Sys {
if (CORE::ref($fh) eq 'GLOB') {
return Sidef::Types::Bool::Bool->new(print {$fh} @args);
}
elsif (eval { $fh->can('print') || $fh->can('AUTOLOAD') }) {
return $fh->print(@args);
}

CORE::warn "[WARN] Sys.printh(): invalid object handle: `$fh'\n";
return;
$fh->print(@args);
}

sub println {
Expand Down
14 changes: 7 additions & 7 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ package Sidef::Types::Array::Array {
my ($self, $obj) = @_;

my $counter = 0;
if ($obj->can('run')) {
if ($obj->SUPER::isa('Sidef::Types::Block::Code')) {

foreach my $item (@{$self}) {
if ($obj->run($item->get_value)) {
Expand Down Expand Up @@ -430,7 +430,7 @@ package Sidef::Types::Array::Array {
my ($self, $arg) = @_;

if (defined $arg) {
if ($arg->can('run')) {
if ($arg->SUPER::isa('Sidef::Types::Block::Code')) {
return return $self->find($arg);
}

Expand Down Expand Up @@ -689,7 +689,7 @@ package Sidef::Types::Array::Array {
&& return $self->new;

my @array;
if ($obj->can('run')) {
if ($obj->SUPER::isa('Sidef::Types::Block::Code')) {
for (my $i = 1 ; $i <= $offset ; $i += 2) {
push @array, $obj->run($self->[$i - 1]->get_value, $self->[$i]->get_value);
}
Expand Down Expand Up @@ -746,7 +746,7 @@ package Sidef::Types::Array::Array {
sub reduce {
my ($self, $obj) = @_;

if ($obj->can('run')) {
if ($obj->SUPER::isa('Sidef::Types::Block::Code')) {
(my $offset = $#{$self}) >= 0 || return;

my $x = $self->[0]->get_value;
Expand Down Expand Up @@ -863,8 +863,8 @@ package Sidef::Types::Array::Array {
sub abbrev {
my ($self, $code) = @_;

my $__END__ = {}; # some unique value
my $__CALL__ = defined($code) && $code->can('call');
my $__END__ = {}; # some unique value
my $__CALL__ = defined($code) && $code->SUPER::isa('Sidef::Types::Block::Code');
my %table;
foreach my $sub_array (map { $_->get_value } @{$self}) {
Expand Down Expand Up @@ -921,7 +921,7 @@ package Sidef::Types::Array::Array {
sub contains {
my ($self, $obj) = @_;
if ($obj->can('run')) {
if ($obj->SUPER::isa('Sidef::Types::Block::Code')) {
foreach my $item (@{$self}) {
if ($obj->run($item->get_value)) {
return Sidef::Types::Bool::Bool->true;
Expand Down
4 changes: 2 additions & 2 deletions lib/Sidef/Types/Hash/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package Sidef::Types::Hash::Hash {

if (@pairs == 1) {

# Block to hash
if (ref($pairs[0]) eq 'Sidef::Types::Block::Code') {
# Any object to hash
if ($pairs[0]->can('to_hash')) {
return $pairs[0]->to_hash;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ package Sidef::Types::Number::Number {
sub of {
my ($self, $obj) = @_;

if (ref($obj) eq 'Sidef::Types::Block::Code') {
if ($obj->SUPER::isa('Sidef::Types::Block::Code')) {
return Sidef::Types::Array::Array->new(map { $obj->run(__PACKAGE__->new($_)) } 1 .. $self->get_value);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,11 @@ package Sidef::Types::String::String {
sub sub {
my ($self, $regex, $str) = @_;

ref($str) eq 'Sidef::Types::Block::Code'
&& return $self->esub($regex, $str);

$str //= __PACKAGE__->new('');

$str->SUPER::isa('Sidef::Types::Block::Code')
&& return $self->esub($regex, $str);

if (ref($regex) eq 'Sidef::Types::Regex::Regex') {
$regex->match($self)->{matched} or return $self;
}
Expand All @@ -467,11 +467,11 @@ package Sidef::Types::String::String {
sub gsub {
my ($self, $regex, $str) = @_;

ref($str) eq 'Sidef::Types::Block::Code'
&& return $self->gesub($regex, $str);

$str //= __PACKAGE__->new('');

$str->SUPER::isa('Sidef::Types::Block::Code')
&& return $self->gesub($regex, $str);

if (ref($regex) eq 'Sidef::Types::Regex::Regex') {
$regex->match($self)->{matched} or return $self;
}
Expand All @@ -498,7 +498,7 @@ package Sidef::Types::String::String {
$regex->match($self)->{matched} or return $self;
}

if (ref($code) eq 'Sidef::Types::Block::Code') {
if ($code->SUPER::isa('Sidef::Types::Block::Code')) {
return __PACKAGE__->new($self->get_value =~ s{$search}{$code->run(_get_captures($self->get_value))}er);
}

Expand All @@ -515,7 +515,7 @@ package Sidef::Types::String::String {
$regex->match($self)->{matched} or return $self;
}

if (ref($code) eq 'Sidef::Types::Block::Code') {
if ($code->SUPER::isa('Sidef::Types::Block::Code')) {
my $value = $self->get_value;
return __PACKAGE__->new($value =~ s{$search}{$code->run(_get_captures($value))}ger);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Variable/LazyMethod.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package Sidef::Variable::LazyMethod {
my ($method) = ($AUTOLOAD =~ /^.*[^:]::(.*)$/);
my $call = $self->{method};

if (ref($call) eq 'Sidef::Types::Block::Code') {
if ($call->SUPER::isa('Sidef::Types::Block::Code')) {
if ($method eq 'call') {
return $call->call($self->{obj}, @{$self->{args}}, @args);
}
Expand Down

0 comments on commit 376d7bf

Please sign in to comment.