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; return @tokens;
} }
sub lisp_format {
my $e = shift;
if (ref($e) eq "ARRAY")
{
return "(" . join(" ", map { lisp_format($_) } @$e) . ")";
}
else
{
return "$e";
}
}
my %stdctx = ( my %stdctx = (
'+' => sub { '+' => sub {
my $sum = 0; my $sum = 0;
@ -103,7 +115,12 @@ my %stdctx = (
}, },
'write-line' => sub { 'write-line' => sub {
my $e = shift; my $e = shift;
print "$e\n"; print lisp_format($e) . "\n";
return undef;
},
'write' => sub {
my $e = shift;
print lisp_format($e);
return undef; return undef;
}, },
'null' => sub { my ($a) = @_; return ! defined $a; }, 'null' => sub { my ($a) = @_; return ! defined $a; },
@ -169,7 +186,15 @@ my %stdctx = (
# string length # string length
'length' => sub { 'length' => sub {
my ($a) = @_; my ($a) = @_;
if (ref($a) eq "ARRAY")
{
return scalar(@$a);
}
else
{
return length($a); return length($a);
}
}, },
# Bitwise operations # Bitwise operations
@ -199,6 +224,12 @@ my %stdctx = (
return ~$v; 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 # Constants
't' => 1, 't' => 1,
'f' => 0, 'f' => 0,