Skip to content

Commit

Permalink
- Simplified the internal API -- the global _is_*type* methods are …
Browse files Browse the repository at this point in the history
…no longer available.
  • Loading branch information
trizen committed Jul 8, 2015
1 parent 85aa02d commit b4da2e2
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 110 deletions.
70 changes: 12 additions & 58 deletions lib/Sidef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,8 @@ package Sidef {
our $SPACES_INCR = 4; # the number of spaces incrementor

{
my %types = (
bool => {class => {'Sidef::Types::Bool::Bool' => 1}},
code => {class => {'Sidef::Types::Block::Code' => 1}},
hash => {class => {'Sidef::Types::Hash::Hash' => 1}},
number => {
class => {
'Sidef::Types::Number::Number' => 1,
'Sidef::Types::Number::Complex' => 1,
'Sidef::Types::Byte::Byte' => 1,
},
type => 'SCALAR',
},
var_ref => {class => {'Sidef::Variable::Ref' => 1}},
file => {
class => {'Sidef::Types::Glob::File' => 1},
type => 'SCALAR',
},
fh => {class => {'Sidef::Types::Glob::FileHandle' => 1}},
dir => {
class => {'Sidef::Types::Glob::Dir' => 1},
type => 'SCALAR',
},
regex => {class => {'Sidef::Types::Regex::Regex' => 1}},
pair => {
class => {'Sidef::Types::Array::Pair' => 1},
type => 'ARRAY',
},
string => {
class => {
'Sidef::Types::String::String' => 1,
'Sidef::Types::Char::Char' => 1,
},
type => 'SCALAR',
},
array => {
class => {
'Sidef::Types::Array::Array' => 1,
'Sidef::Types::Array::Range' => 1,
'Sidef::Types::Char::Chars' => 1,
'Sidef::Types::Byte::Bytes' => 1,
},
type => 'ARRAY',
},
);

no strict 'refs';

foreach my $type (keys %types) {
*{__PACKAGE__ . '::' . '_is_' . $type} = sub {
exists($types{$type}{class}{ref($_[1])}) ? 1 : 0;
};
}

foreach my $method (['!=', 1], ['==', 0]) {

*{__PACKAGE__ . '::' . $method->[0]} = sub {
Expand Down Expand Up @@ -93,20 +42,20 @@ package Sidef {

*__add_method__ = \&def_method;

sub method {
my ($self, $method, @args) = @_;
Sidef::Variable::LazyMethod->new(obj => $self, method => $method, args => \@args);
}

sub METHODS {
my ($self) = @_;

my %alias;
my %methods;
my $ref = ref($self);
foreach my $method (grep { $_ !~ /^[(_]/ and defined(&{$ref . '::' . $_}) } keys %{$ref . '::'}) {
$methods{$method} = ($alias{\&{$ref . '::' . $method}} //=
Sidef::Variable::LazyMethod->new(obj => $self, method => \&{$ref . '::' . $method}));
$methods{$method} = (
$alias{\&{$ref . '::' . $method}} //=
Sidef::Variable::LazyMethod->new(
obj => $self,
method => \&{$ref . '::' . $method}
)
);
}

Sidef::Types::Hash::Hash->new(%methods);
Expand All @@ -117,6 +66,11 @@ package Sidef {
bless {}, __PACKAGE__;
}

sub method {
my ($self, $method, @args) = @_;
Sidef::Variable::LazyMethod->new(obj => $self, method => $method, args => \@args);
}

sub super_join {
my ($self, @args) = @_;
$self->new(
Expand Down
2 changes: 0 additions & 2 deletions lib/Sidef/Sys/Sys.pm
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ package Sidef::Sys::Sys {
my ($self, $text) = @_;

if (defined($text)) {
$self->_is_string($text) || return;
$text->print;
}

Expand All @@ -157,7 +156,6 @@ package Sidef::Sys::Sys {

if (@vars) {
foreach my $var_ref (@vars) {
$self->_is_var_ref($var_ref) || return;
my $var = $var_ref->get_var;
print "$var->{name}: ";
chomp(my $input = <STDIN>);
Expand Down
33 changes: 17 additions & 16 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -654,16 +654,16 @@ package Sidef::Types::Array::Array {
&& return $self->new;

my @array;
if ($self->_is_string($obj)) {
my $method = $obj->get_value;
if ($obj->can('run')) {
for (my $i = 1 ; $i <= $offset ; $i += 2) {
my $x = $self->[$i - 1]->get_value;
push @array, $x->$method($self->[$i]->get_value);
push @array, $obj->run($self->[$i - 1]->get_value, $self->[$i]->get_value);
}
}
elsif ($obj->can('run')) {
else {
my $method = $obj->get_value;
for (my $i = 1 ; $i <= $offset ; $i += 2) {
push @array, $obj->run($self->[$i - 1]->get_value, $self->[$i]->get_value);
my $x = $self->[$i - 1]->get_value;
push @array, $x->$method($self->[$i]->get_value);
}
}

Expand Down Expand Up @@ -711,17 +711,18 @@ package Sidef::Types::Array::Array {
sub reduce {
my ($self, $obj) = @_;

if ($self->_is_string($obj)) {
return $self->reduce_operator($obj->get_value);
}
if ($obj->can('run')) {
(my $offset = $#{$self}) >= 0 || return;

(my $offset = $#{$self}) >= 0 || return;
my $x = $self->[0]->get_value;
foreach my $i (1 .. $offset) {
$x = $obj->run($x, $self->[$i]->get_value);
}

my $x = $self->[0]->get_value;
foreach my $i (1 .. $offset) {
$x = $obj->run($x, $self->[$i]->get_value);
return $x;
}
$x;

$self->reduce_operator($obj->get_value);
}

*inject = \&reduce;
Expand Down Expand Up @@ -1288,7 +1289,7 @@ package Sidef::Types::Array::Array {
*{__PACKAGE__ . '::' . '='} = sub {
my ($self, $arg) = @_;
if ($self->_is_array($arg)) {
if ($arg->isa('ARRAY')) {
my @values = map { $_->get_value } @{$arg};
foreach my $i (0 .. $#{$self}) {
Expand All @@ -1309,7 +1310,7 @@ package Sidef::Types::Array::Array {
*{__PACKAGE__ . '::' . '+='} = sub {
my ($self, $arg) = @_;
if ($self->_is_array($arg)) {
if ($arg->isa('ARRAY')) {
my @values = map { $_->get_value } @{$arg};
foreach my $i (0 .. $#{$self}) {
Expand Down
1 change: 0 additions & 1 deletion lib/Sidef/Types/Glob/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ package Sidef::Types::Glob::File {

sub compare {
my ($self, $file) = @_;
$self->_is_file($file) || return;
if (@_ == 3) {
($self, $file) = ($file, $_[2]);
}
Expand Down
3 changes: 0 additions & 3 deletions lib/Sidef/Types/Glob/FileHandle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ package Sidef::Types::Glob::FileHandle {

sub compare {
my ($self, $fh) = @_;
$self->_is_fh($fh) || return;
state $x = require File::Compare;
Sidef::Types::Number::Number->new(File::Compare::compare($self->{fh}, $fh->{fh}));
}
Expand Down Expand Up @@ -144,7 +143,6 @@ package Sidef::Types::Glob::FileHandle {
my ($self, $var_ref) = @_;

if (defined $var_ref) {
$self->_is_var_ref($var_ref) || return;
my $line = CORE::readline($self->{fh});
$var_ref->get_var->set_value(Sidef::Types::String::String->new($line // return Sidef::Types::Bool::Bool->false));
return Sidef::Types::Bool::Bool->true;
Expand Down Expand Up @@ -301,7 +299,6 @@ package Sidef::Types::Glob::FileHandle {

sub read_to {
my ($self, $var_ref) = @_;
$self->_is_var_ref($var_ref) || return;
$var_ref->get_var->set_value(Sidef::Types::String::String->new(unpack 'A*', scalar CORE::readline($self->{fh})));
$self;
}
Expand Down
3 changes: 0 additions & 3 deletions lib/Sidef/Types/Hash/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ package Sidef::Types::Hash::Hash {

sub concat {
my ($self, $obj) = @_;
$self->_is_hash($obj) || return;

my @list;
while (my ($key, $val) = each %{$self->{data}}) {
Expand All @@ -229,8 +228,6 @@ package Sidef::Types::Hash::Hash {
sub merge_values {
my ($self, $obj) = @_;

$self->_is_hash($obj) || return;

while (my ($key, undef) = each %{$self->{data}}) {
if (exists $obj->{data}{$key}) {
$self->{data}{$key} = $obj->{data}{$key};
Expand Down
9 changes: 2 additions & 7 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ package Sidef::Types::Number::Number {
sub array_to {
my ($self, $num, $step) = @_;

$step = defined($step) ? $self->_is_number($step) ? ($step->get_value) : return : 1;
$step = defined($step) ? $step->get_value : 1;

my $array = Sidef::Types::Array::Array->new();
for (my $i = $self->get_value ; $i <= $num->get_value ; $i += $step) {
Expand Down Expand Up @@ -612,12 +612,7 @@ package Sidef::Types::Number::Number {
sub shift_right {
my ($self, $num, $base) = @_;

$self->new(
$self->get_value->copy->brsft(
$num->get_value,
defined($base) ? $self->_is_number($base) ? $base->get_value : return : ()
)
);
$self->new($self->get_value->copy->brsft($num->get_value, (defined($base) ? $base->get_value : ())));
}

*shiftRight = \&shift_right;
Expand Down
41 changes: 21 additions & 20 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ package Sidef::Types::String::String {
sub subtract {
my ($self, $obj) = @_;

if ($self->_is_regex($obj)) {
if (ref($obj) eq 'Sidef::Types::Regex::Regex') {

$obj->match($self)->to_bool or return $self;

Expand Down Expand Up @@ -115,7 +115,7 @@ package Sidef::Types::String::String {
sub match {
my ($self, $regex, @rest) = @_;
(
$self->_is_regex($regex) ? $regex : do {
ref($regex) eq 'Sidef::Types::Regex::Regex' ? $regex : do {
state $x = require Scalar::Util;
$cache{Scalar::Util::refaddr($regex)} //= Sidef::Types::Regex::Regex->new($regex);
}
Expand All @@ -131,7 +131,7 @@ package Sidef::Types::String::String {
sub gmatch {
my ($self, $regex, @rest) = @_;
(
$self->_is_regex($regex) ? $regex : do {
ref($regex) eq 'Sidef::Types::Regex::Regex' ? $regex : do {
state $x = require Scalar::Util;
$cache{Scalar::Util::refaddr($regex)} //=
Sidef::Types::Regex::Regex->new($regex);
Expand Down Expand Up @@ -428,7 +428,7 @@ package Sidef::Types::String::String {
sub sprintf {
my ($self, @arguments) = @_;

if (@arguments == 1 and $self->_is_array($arguments[0])) {
if (@arguments == 1 and $arguments[0]->isa('ARRAY')) {
@arguments = map { $_->get_value } @{$arguments[0]};
}

Expand All @@ -443,7 +443,7 @@ package Sidef::Types::String::String {
sub _string_or_regex {
my ($self, $obj) = @_;

if ($self->_is_regex($obj)) {
if (ref($obj) eq 'Sidef::Types::Regex::Regex') {
return $obj->{regex};
}

Expand All @@ -453,12 +453,12 @@ package Sidef::Types::String::String {
sub sub {
my ($self, $regex, $str) = @_;

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

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

if ($self->_is_regex($regex)) {
if (ref($regex) eq 'Sidef::Types::Regex::Regex') {
$regex->match($self)->{matched} or return $self;
}

Expand All @@ -473,12 +473,12 @@ package Sidef::Types::String::String {
sub gsub {
my ($self, $regex, $str) = @_;

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

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

if ($self->_is_regex($regex)) {
if (ref($regex) eq 'Sidef::Types::Regex::Regex') {
$regex->match($self)->{matched} or return $self;
}

Expand All @@ -500,15 +500,15 @@ package Sidef::Types::String::String {
$code //= __PACKAGE__->new('');
my $search = $self->_string_or_regex($regex);

if ($self->_is_regex($regex)) {
if (ref($regex) eq 'Sidef::Types::Regex::Regex') {
$regex->match($self)->{matched} or return $self;
}

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

__PACKAGE__->new($self->get_value =~ s{$search}{$code->run(_get_captures($self->get_value))}er);
__PACKAGE__->new($self->get_value =~ s{$search}{$code->get_value}eer);
}

sub gesub {
Expand All @@ -517,16 +517,17 @@ package Sidef::Types::String::String {
$code //= __PACKAGE__->new('');
my $search = $self->_string_or_regex($regex);

if ($self->_is_regex($regex)) {
if (ref($regex) eq 'Sidef::Types::Regex::Regex') {
$regex->match($self)->{matched} or return $self;
}

if ($self->_is_string($code)) {
return __PACKAGE__->new($self->get_value =~ s{$search}{$code->get_value}geer);
if (ref($code) eq 'Sidef::Types::Block::Code') {
my $value = $self->get_value;
return __PACKAGE__->new($value =~ s{$search}{$code->run(_get_captures($value))}ger);
}

my $value = $self->get_value;
__PACKAGE__->new($value =~ s{$search}{$code->run(_get_captures($value))}ger);
my $value = $code->get_value;
__PACKAGE__->new($self->get_value =~ s{$search}{$value}geer);
}

sub glob {
Expand Down Expand Up @@ -559,7 +560,7 @@ package Sidef::Types::String::String {
split(' ', $self->get_value, $size));
}

if ($self->_is_number($sep)) {
if (ref($sep) eq 'Sidef::Types::Number::Number') {
return
Sidef::Types::Array::Array->new(map { __PACKAGE__->new($_) } unpack '(a' . $sep->get_value . ')*',
$self->get_value);
Expand Down Expand Up @@ -702,7 +703,7 @@ package Sidef::Types::String::String {
sub translit {
my ($self, $orig, $repl, $modes) = @_;

$self->_is_array($orig) && return $self->trans($orig, $repl);
$orig->isa('ARRAY') && return $self->trans($orig, $repl);
$self->new(
eval qq{"\Q${\$self->get_value}\E"=~tr/}
. $orig->get_value =~ s{([/\\])}{\\$1}gr . "/"
Expand Down

0 comments on commit b4da2e2

Please sign in to comment.