Skip to content

Commit

Permalink
- Improved the Sidef.METHODS() method; now it returns an HASH with th…
Browse files Browse the repository at this point in the history
…e method names as keys and LazyMethod objects as values.

- Fixed a minor parsing issue for strings like: obj.method[0].method;
  • Loading branch information
trizen committed Jul 7, 2015
1 parent fad8e99 commit aaa3382
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/Sidef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ package Sidef {
sub METHODS {
my ($self) = @_;

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

Sidef::Types::Array::Array->new(map { Sidef::Types::Array::Array->new(@{$_}) } values %aliases);
Sidef::Types::Hash::Hash->new(%methods);
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,8 @@ package Sidef::Parser {
my ($ind) = $self->parse_expr(code => $opt{code});
push @{$struct{$self->{class}}[-1]{ind}}, $ind;
}

redo;
}

if (/\G(?!\h*[=-]>)/ && /\G(?=$self->{operators_re})/o) {
Expand Down
5 changes: 3 additions & 2 deletions lib/Sidef/Variable/LazyMethod.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ package Sidef::Variable::LazyMethod {
my ($self, @args) = @_;

my ($method) = ($AUTOLOAD =~ /^.*[^:]::(.*)$/);
my $call = ref($self->{method}) eq 'CODE' ? $self->{method} : $self->{method}->get_value;

if ($method eq 'call') {
return $self->{obj}->${$self->{method}}(@{$self->{args}}, @args);
return $self->{obj}->$call(@{$self->{args}}, @args);
}

$self->{obj}->${$self->{method}}(@{$self->{args}})->$method(@args);
$self->{obj}->$call(@{$self->{args}})->$method(@args);
}

};
Expand Down

0 comments on commit aaa3382

Please sign in to comment.