2 use warnings FATAL
=> qw(all);
3 no if $] >= 5.018, warnings
=> "experimental::smartmatch";
5 use lib dirname
(__FILE__
);
6 use readline qw(mal_readline set_rl_mode);
7 use feature
qw(switch);
10 use types
qw($nil $true $false _sequential_Q _symbol_Q _list_Q);
14 use core qw($core_ns);
19 return reader::read_str($str);
25 return _sequential_Q($x) && scalar(@{$x->{val}}) > 0;
31 return List->new([Symbol->new("quote"), $ast]);
32 } elsif (_symbol_Q($ast->nth(0)) && ${$ast->nth(0)} eq 'unquote') {
34 } elsif (is_pair($ast->nth(0)) && _symbol_Q($ast->nth(0)->nth(0)) &&
35 ${$ast->nth(0)->nth(0)} eq 'splice-unquote') {
36 return List->new([Symbol->new("concat"),
38 quasiquote($ast->rest())]);
40 return List->new([Symbol->new("cons"),
41 quasiquote($ast->nth(0)),
42 quasiquote($ast->rest())]);
49 _symbol_Q($ast->nth(0)) &&
50 $env->find($ast->nth(0))) {
51 my ($f) = $env->get($ast->nth(0));
52 if ((ref $f) =~ /^Function/) {
61 while (is_macro_call($ast, $env)) {
62 my $mac = $env->get($ast->nth(0));
63 $ast = $mac->apply($ast->rest());
76 my @lst = map {EVAL($_, $env)} @{$ast->{val}};
77 return List->new(\@lst);
80 my @lst = map {EVAL($_, $env)} @{$ast->{val}};
81 return Vector->new(\@lst);
85 foreach my $k (keys( %{ $ast->{val} })) {
86 $new_hm->{$k} = EVAL($ast->get($k), $env);
88 return HashMap->new($new_hm);
101 #print "EVAL: " . printer::_pr_str($ast) . "\n";
102 if (! _list_Q($ast)) {
103 return eval_ast($ast, $env);
107 $ast = macroexpand($ast, $env);
108 if (! _list_Q($ast)) {
109 return eval_ast($ast, $env);
112 my ($a0, $a1, $a2, $a3) = @{$ast->{val}};
113 given ((ref $a0) =~ /^Symbol/ ? $$a0 : $a0) {
115 my $res = EVAL($a2, $env);
116 return $env->set($a1, $res);
119 my $let_env = Env->new($env);
120 for(my $i=0; $i < scalar(@{$a1->{val}}); $i+=2) {
121 $let_env->set($a1->nth($i), EVAL($a1->nth($i+1), $let_env));
125 # Continue loop (TCO)
130 when (/^quasiquote$/) {
131 $ast = quasiquote($a1);
132 # Continue loop (TCO)
134 when (/^defmacro!$/) {
135 my $func = EVAL($a2, $env);
136 $func->{ismacro} = 1;
137 return $env->set($a1, $func);
139 when (/^macroexpand$/) {
140 return macroexpand($a1, $env);
143 eval_ast($ast->slice(1, $#{$ast->{val}}-1), $env);
144 $ast = $ast->nth($#{$ast->{val}});
145 # Continue loop (TCO)
148 my $cond = EVAL($a1, $env);
149 if ($cond eq $nil || $cond eq $false) {
150 $ast = $a3 ? $a3 : $nil;
154 # Continue loop (TCO)
157 return Function->new(\&EVAL, $a2, $env, $a1);
160 my $el = eval_ast($ast, $env);
162 if ((ref $f) =~ /^Function/) {
164 $env = $f->gen_env($el->rest());
165 # Continue loop (TCO)
167 return &{ $f }($el->rest());
178 return printer::_pr_str($exp);
182 my $repl_env = Env->new();
185 return PRINT(EVAL(READ($str), $repl_env));
188 # core.pl: defined using perl
189 foreach my $n (%$core_ns) {
190 $repl_env->set(Symbol->new($n), $core_ns->{$n});
192 $repl_env->set(Symbol->new('eval'), sub { EVAL($_[0]->nth(0), $repl_env); });
193 my @_argv = map {String->new($_)} @ARGV[1..$#ARGV];
194 $repl_env->set(Symbol->new('*ARGV*'), List->new(\@_argv));
196 # core.mal: defined using the language itself
197 REP("(def! not (fn* (a) (if a false true)))");
198 REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
199 REP("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))");
200 REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))");
203 if (scalar(@ARGV) > 0 && $ARGV[0] eq "--raw") {
207 if (scalar(@ARGV) > 0) {
208 REP("(load-file \"" . $ARGV[0] . "\")");
212 my $line = mal_readline("user> ");
213 if (! defined $line) { last; }
218 use autodie; # always "throw" errors
219 print(REP($line), "\n");
224 when (/^BlankException/) {
225 # ignore and continue
229 print "Error: $err\n";