Skip to content

Commit

Permalink
- Added dump support for user-defined classes.
Browse files Browse the repository at this point in the history
Example:
	class Example(a, b) { };
	var obj = Example(1,2);
	say Example;		# prints: "Example"
	say obj;		# prints: "Example(a: 1, b: 2)"

- Fixed the Hash.dump() method for one-key hashes.
  • Loading branch information
trizen committed Jun 24, 2015
1 parent 421882c commit 758ab9c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ package Sidef::Parser {
call => [
{
method => ref($obj) eq 'Sidef::Variable::ClassInit'
? 'init'
? 'new'
: 'call',
(%{$arg} ? (arg => [$arg]) : ())
}
Expand Down
9 changes: 6 additions & 3 deletions lib/Sidef/Types/Hash/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ package Sidef::Types::Hash::Hash {
# Sort the keys case insensitively
my @keys = sort { lc($a) cmp lc($b) } CORE::keys(%{$self->{data}});

Sidef::Types::String::String->new(
my $str = Sidef::Types::String::String->new(
"Hash.new(" . (
@keys
? (
Expand All @@ -379,12 +379,15 @@ package Sidef::Types::Hash::Hash {
. (eval { $val->can('dump') } ? ${$val->dump} : $val)
} @keys
)
. (@keys > 1 ? ("\n" . (' ' x ($Sidef::SPACES -= $Sidef::SPACES_INCR))) : '')
. (@keys > 1 ? ("\n" . (' ' x ($Sidef::SPACES - $Sidef::SPACES_INCR))) : '')
)
: do { $Sidef::SPACES -= $Sidef::SPACES_INCR; "" }
: ""
)
. ")"
);

$Sidef::SPACES -= $Sidef::SPACES_INCR;
$str;
}

{
Expand Down
16 changes: 15 additions & 1 deletion lib/Sidef/Variable/Class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package Sidef::Variable::Class {
use overload q{""} => sub {
eval {
local $SIG{__WARN__} = sub { };
$_[0]->to_s;
$_[0]->to_s // $_[0]->dump;
} // $_[0];
},
q{bool} => sub {
Expand Down Expand Up @@ -53,6 +53,20 @@ package Sidef::Variable::Class {

*is_an = \&is_a;

sub dump {
my ($self) = @_;
Sidef::Types::String::String->new(
$self->{name} . '(' . join(
", ",
map {
my $val = $self->{__VARS__}{$_};
"$_: " . (eval { $val->can('dump') } ? ${$val->dump} : $val)
} @{$self->{__PARAMS__}}
)
. ')'
);
}

sub DESTROY { }

sub AUTOLOAD {
Expand Down
18 changes: 15 additions & 3 deletions lib/Sidef/Variable/ClassInit.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Sidef::Variable::ClassInit {

use 5.014;
use overload q{""} => \&dump;

sub __new__ {
my (undef, %opt) = @_;
Expand Down Expand Up @@ -84,15 +85,25 @@ package Sidef::Variable::ClassInit {

*is_an = \&is_a;

sub dump {
my ($self) = @_;
Sidef::Types::String::String->new($self->{name});
}

sub init {
my ($self, @args) = @_;

my $class = Sidef::Variable::Class->__new__($self->{name});

# The class parameters
my @names = map { $_->{name} } @{$self->{__VARS__}};
my @default_values = map { $_->{value} } @{$self->{__VARS__}};

# Init the class variables
@{$class->{__VARS__}}{map { $_->{name} } @{$self->{__VARS__}}} = @default_values;
@{$class->{__VARS__}}{@names} = @default_values;

# Save the names of class parameters
$class->{__PARAMS__} = [@names];

# Set the class arguments
foreach my $i (0 .. $#{$self->{__VARS__}}) {
Expand Down Expand Up @@ -131,7 +142,7 @@ package Sidef::Variable::ClassInit {
$var->set_value($default_values[$i]);
}

# Add 'var' defined variables
# Add 'def' defined variables
foreach my $var (@{$self->{__DEF_VARS__}}) {
$class->{__VARS__}{$var->{name}} = $var->get_value;
}
Expand All @@ -154,7 +165,8 @@ package Sidef::Variable::ClassInit {
$class;
}

*new = \&init;
*call = \&init;
*new = \&init;

{
no strict 'refs';
Expand Down
2 changes: 1 addition & 1 deletion scripts/problem_of_apollonius.sf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/ruby

class Circle(x,y,r) {
method to_s { "circle(#{x}, #{y}, #{r})" };
method to_s { "Circle(#{x}, #{y}, #{r})" };
}

func solve_apollonius(c is Array, s is Array) -> Circle {
Expand Down

0 comments on commit 758ab9c

Please sign in to comment.