2014-04-22 06:47:36 +04:00
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
2014-05-11 01:03:25 +04:00
|
|
|
use File::Basename;
|
|
|
|
use lib dirname (__FILE__);
|
2014-04-24 06:46:57 +04:00
|
|
|
use readline qw(mal_readline);
|
2014-04-22 06:47:36 +04:00
|
|
|
use feature qw(switch);
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
use types qw($nil $true $false _sequential_Q _symbol_Q _list_Q);
|
|
|
|
use reader;
|
|
|
|
use printer;
|
|
|
|
use env;
|
|
|
|
use core qw($core_ns);
|
|
|
|
|
|
|
|
# read
|
|
|
|
sub READ {
|
|
|
|
my $str = shift;
|
|
|
|
return reader::read_str($str);
|
|
|
|
}
|
|
|
|
|
|
|
|
# eval
|
|
|
|
sub is_pair {
|
|
|
|
my ($x) = @_;
|
2014-04-24 06:46:57 +04:00
|
|
|
return _sequential_Q($x) && scalar(@{$x->{val}}) > 0;
|
2014-04-22 06:47:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sub quasiquote {
|
|
|
|
my ($ast) = @_;
|
|
|
|
if (!is_pair($ast)) {
|
|
|
|
return List->new([Symbol->new("quote"), $ast]);
|
2014-04-24 06:46:57 +04:00
|
|
|
} elsif (_symbol_Q($ast->nth(0)) && ${$ast->nth(0)} eq 'unquote') {
|
|
|
|
return $ast->nth(1);
|
|
|
|
} elsif (is_pair($ast->nth(0)) && _symbol_Q($ast->nth(0)->nth(0)) &&
|
|
|
|
${$ast->nth(0)->nth(0)} eq 'splice-unquote') {
|
2014-04-22 06:47:36 +04:00
|
|
|
return List->new([Symbol->new("concat"),
|
2014-04-24 06:46:57 +04:00
|
|
|
$ast->nth(0)->nth(1),
|
2014-04-22 06:47:36 +04:00
|
|
|
quasiquote($ast->rest())]);
|
|
|
|
} else {
|
|
|
|
return List->new([Symbol->new("cons"),
|
2014-04-24 06:46:57 +04:00
|
|
|
quasiquote($ast->nth(0)),
|
2014-04-22 06:47:36 +04:00
|
|
|
quasiquote($ast->rest())]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub is_macro_call {
|
|
|
|
my ($ast, $env) = @_;
|
|
|
|
if (_list_Q($ast) &&
|
2014-04-24 06:46:57 +04:00
|
|
|
_symbol_Q($ast->nth(0)) &&
|
|
|
|
$env->find(${$ast->nth(0)})) {
|
|
|
|
my ($f) = $env->get(${$ast->nth(0)});
|
2014-04-22 06:47:36 +04:00
|
|
|
if ((ref $f) =~ /^Function/) {
|
|
|
|
return $f->{ismacro};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub macroexpand {
|
|
|
|
my ($ast, $env) = @_;
|
|
|
|
while (is_macro_call($ast, $env)) {
|
2014-04-24 06:46:57 +04:00
|
|
|
my $mac = $env->get(${$ast->nth(0)});
|
2014-04-22 06:47:36 +04:00
|
|
|
$ast = $mac->apply($ast->rest());
|
|
|
|
}
|
|
|
|
return $ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub eval_ast {
|
|
|
|
my($ast, $env) = @_;
|
|
|
|
given (ref $ast) {
|
|
|
|
when (/^Symbol/) {
|
|
|
|
$env->get($$ast);
|
|
|
|
}
|
|
|
|
when (/^List/) {
|
2014-04-24 06:46:57 +04:00
|
|
|
my @lst = map {EVAL($_, $env)} @{$ast->{val}};
|
2014-04-22 06:47:36 +04:00
|
|
|
return List->new(\@lst);
|
|
|
|
}
|
2014-04-24 06:46:57 +04:00
|
|
|
when (/^Vector/) {
|
|
|
|
my @lst = map {EVAL($_, $env)} @{$ast->{val}};
|
|
|
|
return Vector->new(\@lst);
|
|
|
|
}
|
|
|
|
when (/^HashMap/) {
|
|
|
|
my $new_hm = {};
|
|
|
|
foreach my $k (keys($ast->{val})) {
|
|
|
|
$new_hm->{$k} = EVAL($ast->get($k), $env);
|
|
|
|
}
|
|
|
|
return HashMap->new($new_hm);
|
|
|
|
}
|
2014-04-22 06:47:36 +04:00
|
|
|
default {
|
|
|
|
return $ast;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub EVAL {
|
|
|
|
my($ast, $env) = @_;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
#print "EVAL: " . printer::_pr_str($ast) . "\n";
|
|
|
|
if (! _list_Q($ast)) {
|
|
|
|
return eval_ast($ast, $env);
|
|
|
|
}
|
|
|
|
|
|
|
|
# apply list
|
|
|
|
$ast = macroexpand($ast, $env);
|
|
|
|
if (! _list_Q($ast)) { return $ast; }
|
|
|
|
|
2014-04-24 06:46:57 +04:00
|
|
|
my ($a0, $a1, $a2, $a3) = @{$ast->{val}};
|
2014-04-22 06:47:36 +04:00
|
|
|
given ((ref $a0) =~ /^Symbol/ ? $$a0 : $a0) {
|
|
|
|
when (/^def!$/) {
|
|
|
|
my $res = EVAL($a2, $env);
|
|
|
|
return $env->set($$a1, $res);
|
|
|
|
}
|
|
|
|
when (/^let\*$/) {
|
|
|
|
my $let_env = Env->new($env);
|
2014-04-24 06:46:57 +04:00
|
|
|
for(my $i=0; $i < scalar(@{$a1->{val}}); $i+=2) {
|
|
|
|
$let_env->set(${$a1->nth($i)}, EVAL($a1->nth($i+1), $let_env));
|
2014-04-22 06:47:36 +04:00
|
|
|
}
|
2014-04-24 06:46:57 +04:00
|
|
|
$ast = $a2;
|
|
|
|
$env = $let_env;
|
2014-04-24 06:59:50 +04:00
|
|
|
# Continue loop (TCO)
|
2014-04-22 06:47:36 +04:00
|
|
|
}
|
|
|
|
when (/^quote$/) {
|
|
|
|
return $a1;
|
|
|
|
}
|
|
|
|
when (/^quasiquote$/) {
|
2014-04-24 06:59:50 +04:00
|
|
|
$ast = quasiquote($a1);
|
|
|
|
# Continue loop (TCO)
|
2014-04-22 06:47:36 +04:00
|
|
|
}
|
|
|
|
when (/^defmacro!$/) {
|
|
|
|
my $func = EVAL($a2, $env);
|
|
|
|
$func->{ismacro} = 1;
|
|
|
|
return $env->set($$a1, $func);
|
|
|
|
}
|
|
|
|
when (/^macroexpand$/) {
|
|
|
|
return macroexpand($a1, $env);
|
|
|
|
}
|
|
|
|
when (/^do$/) {
|
2014-04-24 06:46:57 +04:00
|
|
|
eval_ast($ast->slice(1, $#{$ast->{val}}-1), $env);
|
|
|
|
$ast = $ast->nth($#{$ast->{val}});
|
2014-04-24 06:59:50 +04:00
|
|
|
# Continue loop (TCO)
|
2014-04-22 06:47:36 +04:00
|
|
|
}
|
|
|
|
when (/^if$/) {
|
|
|
|
my $cond = EVAL($a1, $env);
|
|
|
|
if ($cond eq $nil || $cond eq $false) {
|
|
|
|
$ast = $a3 ? $a3 : $nil;
|
|
|
|
} else {
|
|
|
|
$ast = $a2;
|
|
|
|
}
|
2014-04-24 06:59:50 +04:00
|
|
|
# Continue loop (TCO)
|
2014-04-22 06:47:36 +04:00
|
|
|
}
|
|
|
|
when (/^fn\*$/) {
|
|
|
|
return Function->new(\&EVAL, $a2, $env, $a1);
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
my $el = eval_ast($ast, $env);
|
2014-04-24 06:46:57 +04:00
|
|
|
my $f = $el->nth(0);
|
2014-04-22 06:47:36 +04:00
|
|
|
if ((ref $f) =~ /^Function/) {
|
|
|
|
$ast = $f->{ast};
|
|
|
|
$env = $f->gen_env($el->rest());
|
2014-04-24 06:59:50 +04:00
|
|
|
# Continue loop (TCO)
|
2014-04-22 06:47:36 +04:00
|
|
|
} else {
|
|
|
|
return &{ $f }($el->rest());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} # TCO while loop
|
|
|
|
}
|
|
|
|
|
|
|
|
# print
|
|
|
|
sub PRINT {
|
|
|
|
my $exp = shift;
|
|
|
|
return printer::_pr_str($exp);
|
|
|
|
}
|
|
|
|
|
|
|
|
# repl
|
|
|
|
my $repl_env = Env->new();
|
|
|
|
sub REP {
|
|
|
|
my $str = shift;
|
|
|
|
return PRINT(EVAL(READ($str), $repl_env));
|
|
|
|
}
|
|
|
|
|
|
|
|
# core.pl: defined using perl
|
|
|
|
foreach my $n (%$core_ns) { $repl_env->set($n, $core_ns->{$n}); }
|
2014-04-24 06:46:57 +04:00
|
|
|
$repl_env->set('eval', sub { EVAL($_[0]->nth(0), $repl_env); });
|
2014-04-22 06:47:36 +04:00
|
|
|
my @_argv = map {String->new($_)} @ARGV[1..$#ARGV];
|
|
|
|
$repl_env->set('*ARGV*', List->new(\@_argv));
|
|
|
|
|
|
|
|
# core.mal: defined using the language itself
|
|
|
|
REP("(def! not (fn* (a) (if a false true)))");
|
|
|
|
REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
|
|
|
|
|
2014-04-24 06:46:57 +04:00
|
|
|
if (scalar(@ARGV) > 0) {
|
2014-04-22 06:47:36 +04:00
|
|
|
REP("(load-file \"" . $ARGV[0] . "\")");
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
while (1) {
|
2014-04-24 06:46:57 +04:00
|
|
|
my $line = mal_readline("user> ");
|
2014-04-22 06:47:36 +04:00
|
|
|
if (! defined $line) { last; }
|
2014-04-24 06:46:57 +04:00
|
|
|
do {
|
|
|
|
local $@;
|
|
|
|
my $ret;
|
|
|
|
eval {
|
|
|
|
use autodie; # always "throw" errors
|
|
|
|
print(REP($line), "\n");
|
|
|
|
1;
|
|
|
|
} or do {
|
|
|
|
my $err = $@;
|
|
|
|
given (ref $err) {
|
|
|
|
when (/^BlankException/) {
|
|
|
|
# ignore and continue
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
chomp $err;
|
|
|
|
print "Error: $err\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2014-04-22 06:47:36 +04:00
|
|
|
}
|