diff --git a/lib/Minilisp.pm b/lib/Minilisp.pm index 51c12a2..fde6a86 100644 --- a/lib/Minilisp.pm +++ b/lib/Minilisp.pm @@ -441,6 +441,16 @@ sub parser_expr { } } +sub parser_identifier { + my $ts = shift; + + die "Expected identifier" unless $ts->[0]->{type} == IDENT; + + my $tok = shift @$ts; + + return $tok->{value}; +} + sub parser_list { my $ts = shift; @@ -513,8 +523,7 @@ sub macro_let { $tok = shift @$ts; die "Expected pair in parameter list in let" unless $tok->{type} == LPAREN; - my $ident = shift @$ts; - die "Expected identifier in pair in parameter list in let" unless $ident->{type} == IDENT; + my $ident = parser_identifier($ts); my $assignment; if ($ts->[0]->{type} == RPAREN) @@ -529,7 +538,7 @@ sub macro_let { $tok = shift @$ts; die "Expected ) after parameter pair" unless $tok->{type} == RPAREN; - $variables->{$ident->{value}} = $assignment; + $variables->{$ident} = $assignment; } die "Expected ) after parameter list in let" unless $ts->[0]->{type} == RPAREN; @@ -556,9 +565,8 @@ sub macro_lambda { my @param_list; while ($ts->[0]->{type} != RPAREN) { - $tok = shift @$ts; - die "Expected only identifier in parameter list" unless $tok->{type} == IDENT; - push @param_list, $tok->{value}; + my $ident = parser_identifier($ts); + push @param_list, $ident; } $tok = shift @$ts; @@ -640,8 +648,7 @@ $macros{progn} = \¯o_progn; sub macro_defun { my $ts = shift; - my $ident = shift @$ts; - die "Expected identifier in defun" unless $ident->{type} == IDENT; + my $ident = parser_identifier($ts); my $body = macro_lambda($ts); @@ -652,10 +659,10 @@ sub macro_defun { return $fn->(@_); }; my $ictx = ctx_create($ctx, { - '$ident->{value}' => $self, + '$ident' => $self, }); $fn = $body->($ictx); - ctx_set($ctx, $ident->{value}, $fn); + ctx_set($ctx, $ident, $fn); return $fn; } }