aboutsummaryrefslogtreecommitdiff
path: root/perl/printer.pm
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-19 15:12:13 -0500
committerJoel Martin <github@martintribe.org>2014-04-19 15:12:13 -0500
commitb5dedee06b3a8fbec962fe23b0d474d7cf037a3d (patch)
tree78517cde6624ebd3303627d62c19f48d199e6193 /perl/printer.pm
parent9e5b215158a40ad983cbc22464761524845dd9bf (diff)
downloadmal-b5dedee06b3a8fbec962fe23b0d474d7cf037a3d.tar.gz
mal-b5dedee06b3a8fbec962fe23b0d474d7cf037a3d.zip
Perl: add step1_read_print, types.
Diffstat (limited to 'perl/printer.pm')
-rw-r--r--perl/printer.pm33
1 files changed, 33 insertions, 0 deletions
diff --git a/perl/printer.pm b/perl/printer.pm
new file mode 100644
index 0000000..9741eda
--- /dev/null
+++ b/perl/printer.pm
@@ -0,0 +1,33 @@
+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 . '"'; }
+ default { return $$obj; }
+ }
+}
+
+1;