From ed59a6a04eacfcea263dd3e23fba60f205ba6c3a Mon Sep 17 00:00:00 2001 From: MadMaurice Date: Sun, 4 Apr 2021 01:44:28 +0200 Subject: [PATCH] Improve macro parsing --- lib/Minilisp.pm | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/Minilisp.pm b/lib/Minilisp.pm index 70e16b4..418901f 100644 --- a/lib/Minilisp.pm +++ b/lib/Minilisp.pm @@ -420,7 +420,10 @@ sub parser_call { if (defined $macros{$name}) { shift @$ts; - return $macros{$name}->($ts); + my $parsed = $macros{$name}->($ts); + my $rpar = shift @$ts; + die "Expected ) after macro $name" unless $rpar->{type} == RPAREN; + return $parsed; } } @@ -476,8 +479,8 @@ sub macro_let { $pctx->{$ident->{value}} = $assignment; } - $tok = shift @$ts; - die "Expected ) after parameter list in let" unless $tok->{type} == RPAREN; + die "Expected ) after parameter list in let" unless $ts->[0]->{type} == RPAREN; + shift @$ts; my $inner = macro_progn($ts); @@ -510,8 +513,7 @@ sub macro_lambda { my $body = parser_expr($ts); - $tok = shift @$ts; - die "Expected ) after lambda" unless $tok->{type} == RPAREN; + die "Expected ) after lambda" unless $ts->[0]->{type} == RPAREN; return sub { my $octx = shift; @@ -542,9 +544,7 @@ sub macro_if { my $fbranch = parser_expr($ts); - my $tok = shift @$ts; - - die "Expected ) after else expression" unless $tok->{type} == RPAREN; + die "Expected ) after else expression" unless $ts->[0]->{type} == RPAREN; return sub { my $ctx = shift; @@ -569,8 +569,6 @@ sub macro_progn { push @steps, parser_expr($ts); } - shift @$ts; # Remove RPAREN - return sub { my $ctx = shift;