Add rudimentary support for keywords

This commit is contained in:
madmaurice 2021-04-02 20:52:26 +02:00
parent 7f38e74e78
commit 31e8c61d5e

View file

@ -10,6 +10,7 @@ use constant {
IDENT => 3, IDENT => 3,
STRING => 4, STRING => 4,
NUMBER => 5, NUMBER => 5,
KEYWORD => 6,
}; };
sub tokenize { sub tokenize {
@ -33,6 +34,13 @@ sub tokenize {
{ {
push @tokens, { type => RPAREN }; push @tokens, { type => RPAREN };
} }
elsif($str =~ s/^'([^\s()"]+)//)
{
push @tokens, {
type => KEYWORD,
value => $1,
};
}
elsif($str =~ s/^"(([^"\\]|\\.)+)"//) elsif($str =~ s/^"(([^"\\]|\\.)+)"//)
{ {
my $value = $1; my $value = $1;
@ -86,6 +94,10 @@ sub lisp_format {
{ {
return "(" . join(" ", map { lisp_format($_) } @$e) . ")"; return "(" . join(" ", map { lisp_format($_) } @$e) . ")";
} }
elsif (ref($e) eq "KEYWORD")
{
return "'" . $e->{value};
}
else else
{ {
return "$e"; return "$e";
@ -305,6 +317,11 @@ sub parser_expr {
return $ctx->{$name}; return $ctx->{$name};
} }
} }
elsif($tok->{type} == KEYWORD)
{
my $k = bless { value => $tok->{value} }, "KEYWORD";
return sub { return $k; };
}
elsif($tok->{type} == STRING || $tok->{type} == NUMBER) elsif($tok->{type} == STRING || $tok->{type} == NUMBER)
{ {
return sub { return sub {