1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-13 11:23:59 +03:00
mal/perl/printer.pm

35 lines
874 B
Perl
Raw Normal View History

2014-04-20 00:12:13 +04:00
package printer;
use feature qw(switch);
use strict;
use warnings;
use Exporter 'import';
our @EXPORT_OK = qw( _pr_str );
use types qw($nil $true $false);
sub _pr_str {
my($obj) = @_;
given (ref $obj) {
when(/^List/) {
return '(' . join(' ', map {_pr_str($_)} @$obj) . ')';
}
when(/^Vector/) {
return '[' . join(' ', map {_pr_str($_)} @$obj) . ']';
}
when(/^HashMap/) {
my @elems = ();
foreach my $key (keys %$obj) {
push(@elems, _pr_str(String->new($key)));
push(@elems, _pr_str($obj->{$key}));
}
return '{' . join(' ', @elems) . '}';
}
when(/^String/) { return '"' . $$obj . '"'; }
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;