Create a multipurpose equal

This commit is contained in:
madmaurice 2021-04-02 20:52:57 +02:00
parent 31e8c61d5e
commit 42f0e3bc99

View file

@ -104,6 +104,33 @@ sub lisp_format {
}
}
sub lisp_equal {
my ($a, $b) = @_;
if ( ref($a) eq "ARRAY" && ref($b) eq "ARRAY" )
{
return 0 unless scalar(@$a) == scalar(@$b);
for(my $i = 0; $i < @$a; $i++)
{
return 0 unless lisp_equal($a->[$i], $b->[$i]);
}
return 1;
}
elsif ( ref($a) eq "KEYWORD" && ref($b) eq "KEYWORD" )
{
return $a->{value} eq $b->{value};
}
elsif ( ref($a) eq "" && ref($b) eq "" )
{
return $a == $b;
}
else
{
return 0;
}
}
my %stdctx = (
'+' => sub {
my $sum = 0;
@ -259,15 +286,14 @@ my %stdctx = (
return $v;
},
# TODO: this is tricky as it needs to recurse into sub lists
'equal' => sub {
my ($a, $b) = @_;
return @$a == @$b;
},
# Constants
't' => 1,
'f' => 0,
'nil' => undef,
# Multi-purpose
'equal' => \&lisp_equal,
);
sub parser {