Skip to content

Commit

Permalink
- Minor optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
trizen committed Jun 20, 2015
1 parent 049ba76 commit 46b5379
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
29 changes: 20 additions & 9 deletions lib/Sidef/Exec.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ package Sidef::Exec {

sub new {
bless {
lazy_ops => {
'||' => 1,
'&&' => 1,
':=' => 1,
'||=' => 1,
'&&=' => 1,
'\\\\' => 1,
'\\\\=' => 1,
},

# `1` for primary operators
# `0` for composed operators
lazy_ops => {
'||' => 1,
'&&' => 1,
':=' => 0,
'||=' => 0,
'&&=' => 0,
'\\\\' => 1,
'\\\\=' => 0,
},
},
__PACKAGE__;
}
Expand Down Expand Up @@ -308,6 +311,14 @@ package Sidef::Exec {
}
}

# if (exists $self->{lazy_ops}{$method}) {
# if ($self_obj->$method(@args) and $self->{lazy_ops}{$method}) {
# $self_obj = $self->execute(@args);
# }
# } else {
# $self_obj = $self_obj->$method(@args);
# }

$self_obj = $self_obj->$method(@args);
}
else {
Expand Down
30 changes: 12 additions & 18 deletions lib/Sidef/Object/Object.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,33 @@ package Sidef::Object::Object {

# Logical AND
*{__PACKAGE__ . '::' . '&&'} = sub {
my ($self, $code) = @_;
$self
? Sidef::Types::Block::Code->new($code)->run
: $self;
$_[0]
? Sidef::Types::Block::Code->new($_[1])->run
: $_[0];
};

# Logical OR
*{__PACKAGE__ . '::' . '||'} = sub {
my ($self, $code) = @_;
$self
? $self
: Sidef::Types::Block::Code->new($code)->run;
$_[0]
? $_[0]
: Sidef::Types::Block::Code->new($_[1])->run;
};

# Logical XOR
*{__PACKAGE__ . '::' . '^'} = sub {
my ($self, $val) = @_;
Sidef::Types::Bool::Bool->new($self xor $val);
Sidef::Types::Bool::Bool->new($_[0] xor $_[1]);
};

# Defined-OR
*{__PACKAGE__ . '::' . '\\\\'} = sub {
my ($self, $code) = @_;
ref($self) eq 'Sidef::Types::Nil::Nil'
? Sidef::Types::Block::Code->new($code)->run
: $self;
ref($_[0]) eq 'Sidef::Types::Nil::Nil'
? Sidef::Types::Block::Code->new($_[1])->run
: $_[0];
};

# Ternary operator (Obj ? TrueExpr : FalseExpr)
*{__PACKAGE__ . '::' . '?'} = sub {
my ($self, $code) = @_;
Sidef::Types::Bool::Ternary->new(code => $code, bool => !!$self);
Sidef::Types::Bool::Ternary->new(code => $_[1], bool => !!$_[0]);
};

# Smart match operator
Expand Down Expand Up @@ -157,10 +152,9 @@ package Sidef::Object::Object {

# Negation of smart match
*{__PACKAGE__ . '::' . '!~'} = sub {
my ($first, $second) = @_;
use 5.014;
state $method = '~~';
$first->$method($second)->not;
$_[0]->$method($_[1])->not;
};
};

Expand Down
19 changes: 9 additions & 10 deletions lib/Sidef/Variable/Variable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ package Sidef::Variable::Variable {

*{__PACKAGE__ . '::' . '='} = \&__set_value__;

*{__PACKAGE__ . '::' . '\\\\'} = sub {
my ($self, $code) = @_;

if ($self->_is_defined) {
return $self->get_value;
}

Sidef::Types::Block::Code->new($code)->run;
};

foreach my $operator (qw(:= \\\\=)) {
*{__PACKAGE__ . '::' . $operator} = sub {
my ($self, $code) = @_;
Expand All @@ -150,6 +140,15 @@ package Sidef::Variable::Variable {
};
}

# foreach my $operator(qw(&& ||)) {
# *{__PACKAGE__ . '::' . $operator . '='} = sub {
# my $value = $_[0]->get_value;
# $value->$operator
# ? $_[0]->__set_value__(Sidef::Types::Block::Code->new($_[1])->run)
# : $value;
# };
# }

foreach my $operator (qw(+ - % * / & | ^ ** && || << >> ÷)) {
*{__PACKAGE__ . '::' . $operator . '='} = sub {
$_[0]->__set_value__($_[0]->get_value->$operator($_[1]));
Expand Down
2 changes: 1 addition & 1 deletion scripts/virtual_machine.sf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class VM {
given(op)
when (PUSH) {
var val = code[pc++];
self.stack[++sp] = val;
stack[++sp] = val;
}
when (ADD) {
var a = stack[sp--];
Expand Down

0 comments on commit 46b5379

Please sign in to comment.