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)) { return $ast; }
110 my ($a0, $a1, $a2, $a3) = @{$ast->{val}};
111 given ((ref $a0) =~ /^Symbol/ ? $$a0 : $a0) {
113 my $res = EVAL($a2, $env);
114 return $env->set($a1, $res);
117 my $let_env = Env->new($env);
118 for(my $i=0; $i < scalar(@{$a1->{val}}); $i+=2) {
119 $let_env->set($a1->nth($i), EVAL($a1->nth($i+1), $let_env));
123 # Continue loop (TCO)
128 when (/^quasiquote$/) {
129 $ast = quasiquote($a1);
130 # Continue loop (TCO)
132 when (/^defmacro!$/) {
133 my $func = EVAL($a2, $env);
134 $func->{ismacro} = 1;
135 return $env->set($a1, $func);
137 when (/^macroexpand$/) {
138 return macroexpand($a1, $env);
141 eval_ast($ast->slice(1, $#{$ast->{val}}-1), $env);
142 $ast = $ast->nth($#{$ast->{val}});
143 # Continue loop (TCO)
146 my $cond = EVAL($a1, $env);
147 if ($cond eq $nil || $cond eq $false) {
148 $ast = $a3 ? $a3 : $nil;
152 # Continue loop (TCO)
155 return Function->new(\&EVAL, $a2, $env, $a1);
158 my $el = eval_ast($ast, $env);
160 if ((ref $f) =~ /^Function/) {
162 $env = $f->gen_env($el->rest());
163 # Continue loop (TCO)
165 return &{ $f }($el->rest());
176 return printer::_pr_str($exp);
180 my $repl_env = Env->new();
183 return PRINT(EVAL(READ($str), $repl_env));
186 # core.pl: defined using perl
187 foreach my $n (%$core_ns) {
188 $repl_env->set(Symbol->new($n), $core_ns->{$n});
190 $repl_env->set(Symbol->new('eval'), sub { EVAL($_[0]->nth(0), $repl_env); });
191 my @_argv = map {String->new($_)} @ARGV[1..$#ARGV];
192 $repl_env->set(Symbol->new('*ARGV*'), List->new(\@_argv));
194 # core.mal: defined using the language itself
195 REP("(def! not (fn* (a) (if a false true)))");
196 REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
197 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)))))))");
198 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))))))))");
201 if (scalar(@ARGV) > 0 && $ARGV[0] eq "--raw") {
205 if (scalar(@ARGV) > 0) {
206 REP("(load-file \"" . $ARGV[0] . "\")");
210 my $line = mal_readline("user> ");
211 if (! defined $line) { last; }
216 use autodie; # always "throw" errors
217 print(REP($line), "\n");
222 when (/^BlankException/) {
223 # ignore and continue
227 print "Error: $err\n";