diff --git a/lib/Minilisp.pm b/lib/Minilisp.pm index 8fa1294..63db6a6 100644 --- a/lib/Minilisp.pm +++ b/lib/Minilisp.pm @@ -34,26 +34,42 @@ sub tokenize { $str =~ s/^\s+//; $str =~ s/\s+$//; + my $debug = sub {}; + + $debug = sub { + my $msg = shift; + my $strcpy = $str; + $strcpy =~ s/^/ /mg; + print "-- $msg\n$strcpy\n"; + } if !!$ENV{LEXER_DEBUG}; + + $debug->("Begin parsing"); + while ($str) { if ($str =~ s/^;.*\n//) { + $debug->("Comment"); # Comment. do nothing } elsif ($str =~ s/^\(//) { + $debug->("Left parenthesis"); push @tokens, { type => LPAREN }; } elsif($str =~ s/^\)//) { + $debug->("Right parenthesis"); push @tokens, { type => RPAREN }; } elsif($str =~ s/^'\(//) # short notation for lists { + $debug->("Short list"); push @tokens, { type => LIST }, { type => LPAREN }; } elsif($str =~ s/^'([^\s()"]+)//) { + $debug->("Keyword $1"); push @tokens, { type => KEYWORD, value => $1, @@ -61,6 +77,7 @@ sub tokenize { } elsif($str =~ s/^"(([^"\\]|\\.)+)"//) { + $debug->("String '$1'"); my $value = $1; my %special_chars = ( @@ -86,6 +103,7 @@ sub tokenize { } elsif($ident =~ /^-?([0-9]+|[0-9]*\.[0-9]*)$/) { + $debug->("Number '$1'"); if($ident =~ s/^-//) { $ident = 0 - $ident; @@ -98,6 +116,7 @@ sub tokenize { } else { + $debug->("Identifier '$1'"); push @tokens, { type => IDENT, value => $ident,