2014-04-20 00:12:13 +04:00
|
|
|
package printer;
|
|
|
|
use strict;
|
2014-04-22 05:08:18 +04:00
|
|
|
use warnings FATAL => qw(all);
|
2014-10-10 08:48:47 +04:00
|
|
|
no if $] >= 5.018, warnings => "experimental::smartmatch";
|
2014-04-21 08:45:58 +04:00
|
|
|
use feature qw(switch);
|
2014-04-20 00:12:13 +04:00
|
|
|
use Exporter 'import';
|
|
|
|
our @EXPORT_OK = qw( _pr_str );
|
|
|
|
|
|
|
|
use types qw($nil $true $false);
|
|
|
|
|
2014-04-24 06:46:57 +04:00
|
|
|
use Data::Dumper;
|
|
|
|
|
2014-04-20 00:12:13 +04:00
|
|
|
sub _pr_str {
|
2014-04-21 08:45:58 +04:00
|
|
|
my($obj, $print_readably) = @_;
|
|
|
|
my($_r) = (defined $print_readably) ? $print_readably : 1;
|
2014-04-20 00:12:13 +04:00
|
|
|
given (ref $obj) {
|
|
|
|
when(/^List/) {
|
2014-04-24 06:46:57 +04:00
|
|
|
return '(' . join(' ', map {_pr_str($_, $_r)} @{$obj->{val}}) . ')';
|
2014-04-20 00:12:13 +04:00
|
|
|
}
|
|
|
|
when(/^Vector/) {
|
2014-04-24 06:46:57 +04:00
|
|
|
return '[' . join(' ', map {_pr_str($_, $_r)} @{$obj->{val}}) . ']';
|
2014-04-20 00:12:13 +04:00
|
|
|
}
|
|
|
|
when(/^HashMap/) {
|
|
|
|
my @elems = ();
|
2014-04-24 06:46:57 +04:00
|
|
|
foreach my $key (keys $obj->{val}) {
|
2014-04-21 08:45:58 +04:00
|
|
|
push(@elems, _pr_str(String->new($key), $_r));
|
2014-04-24 06:46:57 +04:00
|
|
|
push(@elems, _pr_str($obj->{val}->{$key}, $_r));
|
2014-04-20 00:12:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return '{' . join(' ', @elems) . '}';
|
|
|
|
}
|
2014-04-21 08:45:58 +04:00
|
|
|
when(/^String/) {
|
2014-12-19 05:33:49 +03:00
|
|
|
if ($$obj =~ /^\x{029e}/) {
|
|
|
|
return ':' . substr($$obj,1);
|
|
|
|
} elsif ($_r) {
|
2014-04-21 08:45:58 +04:00
|
|
|
my $str = $$obj;
|
|
|
|
$str =~ s/\\/\\\\/g;
|
|
|
|
$str =~ s/"/\\"/g;
|
2014-04-22 05:48:16 +04:00
|
|
|
$str =~ s/\n/\\n/g;
|
2014-04-21 08:45:58 +04:00
|
|
|
return '"' . $str . '"';
|
|
|
|
} else {
|
|
|
|
return $$obj;
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 05:08:18 +04:00
|
|
|
when(/^Function/) {
|
|
|
|
return '<fn* ' . _pr_str($obj->{params}) .
|
|
|
|
' ' . _pr_str($obj->{ast}) . '>';
|
|
|
|
}
|
2014-04-24 06:46:57 +04:00
|
|
|
when(/^Atom/) {
|
|
|
|
return '(atom ' . _pr_str($obj->{val}) . ")";
|
|
|
|
}
|
2014-04-21 06:50:52 +04:00
|
|
|
when(/^CODE/) { return '<builtin_fn* ' . $obj . '>'; }
|
2014-04-20 00:12:13 +04:00
|
|
|
default { return $$obj; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|