Skip to content

Commit

Permalink
- Added the prefix ... operator which represents unimplemented code…
Browse files Browse the repository at this point in the history
…. (same as in perl>=5.12.0)

- UNIVERSAL::AUTOLOAD is a little bit faster now and produces nicer error messages.
- Updated the .pod files.
  • Loading branch information
trizen committed Dec 6, 2015
1 parent 423f190 commit b1604b3
Show file tree
Hide file tree
Showing 20 changed files with 261 additions and 13 deletions.
5 changes: 5 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ lib/Sidef/Meta/Assert.pm
lib/Sidef/Meta/Assert.pod
lib/Sidef/Meta/Error.pm
lib/Sidef/Meta/Error.pod
lib/Sidef/Meta/Unimplemented.pm
lib/Sidef/Meta/Unimplemented.pod
lib/Sidef/Meta/Warning.pm
lib/Sidef/Meta/Warning.pod
lib/Sidef/Module/Func.pm
Expand Down Expand Up @@ -50,6 +52,7 @@ lib/Sidef/Types/Array/Pair.pod
lib/Sidef/Types/Black/Hole.pm
lib/Sidef/Types/Block/Break.pm
lib/Sidef/Types/Block/Case.pm
lib/Sidef/Types/Block/Case.pod
lib/Sidef/Types/Block/Code.pm
lib/Sidef/Types/Block/Code.pod
lib/Sidef/Types/Block/CodeInit.pm
Expand Down Expand Up @@ -371,6 +374,7 @@ scripts/RosettaCode/convert_decimal_number_to_rational.sf
scripts/RosettaCode/copy_a_string.sf
scripts/RosettaCode/count_in_factors.sf
scripts/RosettaCode/count_occurrences_of_a_substring.sf
scripts/RosettaCode/count_the_coins.sf
scripts/RosettaCode/create_an_html_table.sf
scripts/RosettaCode/date_format.sf
scripts/RosettaCode/delete_a_file.sf
Expand Down Expand Up @@ -621,6 +625,7 @@ scripts/Tests/cramers_method.sf
scripts/Tests/cross_and_zip_metaoperators.sf
scripts/Tests/data_digging.sf
scripts/Tests/data_handle.sf
scripts/Tests/def_primitive_type.sf
scripts/Tests/default_param_values.sf
scripts/Tests/default_var_values.sf
scripts/Tests/echo.sf
Expand Down
3 changes: 3 additions & 0 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
"Sidef::Meta::Error" : {
"file" : "lib/Sidef/Meta/Error.pm"
},
"Sidef::Meta::Unimplemented" : {
"file" : "lib/Sidef/Meta/Unimplemented.pm"
},
"Sidef::Meta::Warning" : {
"file" : "lib/Sidef/Meta/Warning.pm"
},
Expand Down
2 changes: 2 additions & 0 deletions META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ provides:
file: lib/Sidef/Meta/Assert.pm
Sidef::Meta::Error:
file: lib/Sidef/Meta/Error.pm
Sidef::Meta::Unimplemented:
file: lib/Sidef/Meta/Unimplemented.pm
Sidef::Meta::Warning:
file: lib/Sidef/Meta/Warning.pm
Sidef::Module::Func:
Expand Down
23 changes: 18 additions & 5 deletions lib/Sidef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ package Sidef {
$type;
}

sub normalize_method {
my ($type) = @_;

if (index($type, 'Sidef::') == 0) {
$type = substr($type, rindex($type, '::', rindex($type, '::') - 2) + 2);
}
else {
$type =~ s/^_::main:://
or $type =~ s/^_:://;
}

$type =~ s/^.*\K::/./r;
}

};

#
Expand All @@ -51,7 +65,7 @@ package Sidef {
$self = ref($self) if ref($self);

index($self, 'Sidef::') == 0
or die("[AUTOLOAD] Undefined method: $AUTOLOAD");
or die("[AUTOLOAD] Undefined method `" . Sidef::normalize_method($AUTOLOAD) . q{'});

eval { require $self =~ s{::}{/}rg . '.pm' };

Expand All @@ -64,12 +78,11 @@ package Sidef {
}
}

my $func = \&{$AUTOLOAD};
if (defined(&$func)) {
return $func->($self, @args);
if (defined(&$AUTOLOAD)) {
return $AUTOLOAD->($self, @args);
}

die "[AUTOLOAD] Undefined method: $AUTOLOAD";
die("[AUTOLOAD] Undefined method `" . Sidef::normalize_method($AUTOLOAD) . q{'});
return;
};

Expand Down
8 changes: 8 additions & 0 deletions lib/Sidef.pod
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=cut

=head2 normalize_type

Sidef.normalize_type() -> I<Obj>

Return the

=cut
6 changes: 4 additions & 2 deletions lib/Sidef/Convert/Convert.pod
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ Return the

=cut

=head2 to_bool
=head2 to_b

Convert.to_bool() -> I<Obj>
Convert.to_b() -> I<Obj>

Return the

Aliases: I<to_bool>

=cut

=head2 to_byte
Expand Down
3 changes: 3 additions & 0 deletions lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,9 @@ HEADER
elsif ($ref eq 'Sidef::Perl::Perl') {
$code = $self->make_constant($ref, 'new', "Perl$refaddr");
}
elsif ($ref eq 'Sidef::Meta::Unimplemented') {
$code = qq{CORE::die "Unimplemented at " . } . $self->_dump_string($obj->{file}) . qq{. " line $obj->{line}\E\\n"};
}

# Array indices
if (exists $expr->{ind}) {
Expand Down
3 changes: 3 additions & 0 deletions lib/Sidef/Deparse/Sidef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ package Sidef::Deparse::Sidef {
elsif ($ref eq 'Sidef::Meta::Warning') {
$code = 'warn' . $self->deparse_args($obj->{arg});
}
elsif ($ref eq 'Sidef::Meta::Unimplemented') {
$code = '...';
}
elsif ($ref eq 'Sidef::Eval::Eval') {
$code = 'eval' . $self->deparse_args($obj->{expr});
}
Expand Down
9 changes: 9 additions & 0 deletions lib/Sidef/Meta/Unimplemented.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Sidef::Meta::Unimplemented {

sub new {
my (undef, %opt) = @_;
bless \%opt, __PACKAGE__;
}
};

1;
25 changes: 25 additions & 0 deletions lib/Sidef/Meta/Unimplemented.pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

=encoding utf8

=head1 NAME

Sidef::Meta::Unimplemented

=head1 DESCRIPTION

This object is ...

=head1 SYNOPSIS

var obj = Unimplemented.new(...);

=head1 METHODS


=head2 new

Unimplemented.new() -> I<Obj>

Return the

=cut
9 changes: 8 additions & 1 deletion lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,13 @@ package Sidef::Parser {
return Sidef::Types::Number::Number->new($1 =~ tr/_//dr);
}

# Prefix `...`
if (/\G\.\.\./gc) {
return
Sidef::Meta::Unimplemented->new(line => $self->{line},
file => $self->{file_name},);
}

# Implicit method call on special variable: _
if (/\G\./) {

Expand All @@ -1725,7 +1732,7 @@ package Sidef::Parser {
$self->fatal_error(
code => $_,
pos => pos($_),
error => "attempt to use an implicit method call on the uninitialized variable: \"_\"",
error => q{attempt to use an implicit method call on the uninitialized variable: "_"},
);
}

Expand Down
8 changes: 8 additions & 0 deletions lib/Sidef/Types/Array/Array.pod
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@ Return the

=cut

=head2 last_by

Array.last_by() -> I<Obj>

Return the

=cut

=head2 last_uniq

Array.last_uniq() -> I<Obj>
Expand Down
25 changes: 25 additions & 0 deletions lib/Sidef/Types/Block/Case.pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

=encoding utf8

=head1 NAME

Sidef::Types::Block::Case

=head1 DESCRIPTION

This object is ...

=head1 SYNOPSIS

var obj = Case.new(...);

=head1 METHODS


=head2 new

Case.new() -> I<Obj>

Return the

=cut
16 changes: 14 additions & 2 deletions lib/Sidef/Types/Bool/Bool.pod
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,24 @@ Aliases: I<flip>, I<toggle>, I<is_false>

=cut

=head2 to_bool
=head2 pick

Bool.to_bool() -> I<Obj>
Bool.pick() -> I<Obj>

Return the

Aliases: I<rand>

=cut

=head2 to_b

Bool.to_b() -> I<Obj>

Return the

Aliases: I<to_bool>

=cut

=head2 true
Expand Down
2 changes: 2 additions & 0 deletions lib/Sidef/Types/Number/Complex.pod
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@ Complex.polars() -> I<Obj>

Return the

Aliases: I<polar_parts>

=cut

=head2 rand
Expand Down
12 changes: 11 additions & 1 deletion lib/Sidef/Types/Number/Number.pod
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ Number.int() -> I<Obj>

Return the

Aliases: I<to_i>, I<as_int>
Aliases: I<to_i>, I<as_int>, I<to_int>

=cut

Expand Down Expand Up @@ -806,6 +806,16 @@ Aliases: I<...()>, I<..^()>, I<up_to>, I<upto>

=cut

=head2 to_n

Number.to_n() -> I<Obj>

Return the

Aliases: I<to_num>

=cut

=head2 to_s

Number.to_s() -> I<Obj>
Expand Down
30 changes: 28 additions & 2 deletions lib/Sidef/Types/String/String.pod
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,24 @@ Return the

=cut

=head2 each_num

String.each_num() -> I<Obj>

Return the

Aliases: I<each_number>

=cut

=head2 each_word

String.each_word() -> I<Obj>

Return the

=cut

=head2 encode

String.encode() -> I<Obj>
Expand Down Expand Up @@ -682,6 +700,16 @@ Aliases: I<call>

=cut

=head2 nums

String.nums() -> I<Obj>

Return the

Aliases: I<numbers>

=cut

=head2 oct

String.oct() -> I<Obj>
Expand Down Expand Up @@ -992,8 +1020,6 @@ String.words() -> I<Obj>

Return the

Aliases: I<each_word>

=cut

=head2 |
Expand Down
8 changes: 8 additions & 0 deletions lib/Sidef/Variable/NamedParam.pod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ var obj = NamedParam.new(...);
=head1 METHODS


=head2 dump

NamedParam.dump() -> I<Obj>

Return the

=cut

=head2 new

NamedParam.new() -> I<Obj>
Expand Down
22 changes: 22 additions & 0 deletions scripts/RosettaCode/count_the_coins.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Count_the_coins
#

func cc(_) { 0 }
func cc({ .is_neg }, *_) { 0 }
func cc({ .is_zero }, *_) { 1 }

func cc(amount, first, *rest) is cached {
cc(amount, rest...) + cc(amount - first, first, rest...);
}

func cc_optimized(amount, *rest) {
cc(amount, rest.sort_by{|v| -v }...);
}

var x = cc_optimized(100, 1, 5, 10, 25);
say "Ways to change $1 with common coins: #{x}";

assert_eq(x, 242);
Loading

0 comments on commit b1604b3

Please sign in to comment.