Add rudimentary support for lists

This commit is contained in:
madmaurice 2021-04-02 20:01:12 +02:00
parent 009ec0d0cd
commit a6c609ff4c

View file

@ -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,