From 42f0e3bc99d5ce45e644a243b2a30e4949e512ba Mon Sep 17 00:00:00 2001 From: MadMaurice Date: Fri, 2 Apr 2021 20:52:57 +0200 Subject: [PATCH] Create a multipurpose equal --- lib/Minilisp.pm | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/Minilisp.pm b/lib/Minilisp.pm index 67d93ca..52c5da8 100644 --- a/lib/Minilisp.pm +++ b/lib/Minilisp.pm @@ -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 {