やさしい Lisp の作り方 by Java and by C#を参考に書いてみる。
READ-PRINT-LOOPまでのものを動かしてみる
$ perl moosp.pl
Moosp> (1 2 3 4)
(1 2 3 4)
Moosp> (e r t)
(E R T)
Moosp> (2 . 3)
(2 . 3)
Moosp> (defun plus (a b) (+ a b))
(DEFUN PLUS (A B) (+ A B))
コード
package Moosp;
use Moose;
use Moosp::Env;
use Moosp::Reader;
use Term::ReadLine;
our $VERSION = 0.01;
sub run {
my $self = shift;
my $term = new Term::ReadLine 'Simple LISP REPL';
my $prompt = "Moosp> ";
my $OUT = $term->OUT || \*STDOUT;
while ( defined ($_ = $term->readline($prompt)) ) {
my $reader = Moosp::Reader->new();
my $sexp = $reader->readFromString($_);
warn $@ if $@;
print $OUT $sexp->serialize(), "\n" unless $@;
}
}
__PACKAGE__->meta->make_immutable;
no Moose;
1; # End of Moosp
moosp.pl
#!/usr/bin/env perl
use lib "./lib";
use Moosp;
Moosp->new->run();