diff --git a/lib/Minilisp.pm b/lib/Minilisp.pm index e07b925..72fbc6f 100644 --- a/lib/Minilisp.pm +++ b/lib/Minilisp.pm @@ -80,6 +80,18 @@ sub tokenize { return @tokens; } +sub lisp_format { + my $e = shift; + if (ref($e) eq "ARRAY") + { + return "(" . join(" ", map { lisp_format($_) } @$e) . ")"; + } + else + { + return "$e"; + } +} + my %stdctx = ( '+' => sub { my $sum = 0; @@ -103,7 +115,12 @@ my %stdctx = ( }, 'write-line' => sub { my $e = shift; - print "$e\n"; + print lisp_format($e) . "\n"; + return undef; + }, + 'write' => sub { + my $e = shift; + print lisp_format($e); return undef; }, 'null' => sub { my ($a) = @_; return ! defined $a; }, @@ -169,7 +186,15 @@ my %stdctx = ( # string length 'length' => sub { my ($a) = @_; - return length($a); + + if (ref($a) eq "ARRAY") + { + return scalar(@$a); + } + else + { + return length($a); + } }, # Bitwise operations @@ -199,6 +224,12 @@ my %stdctx = ( return ~$v; }, + # Lists + 'list' => sub { return [ @_ ]; }, + 'first' => sub { return (shift)->[0]; }, + 'second' => sub { return (shift)->[1]; }, + 'nth' => sub { my ($idx,$list) = @_; return $list->[$idx]; }, + # Constants 't' => 1, 'f' => 0,