forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step2_eval.pl
52 lines (43 loc) · 1.25 KB
/
step2_eval.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use v6;
use lib IO::Path.new($?FILE).dirname;
use reader;
use printer;
use types;
sub read ($str) {
return read_str($str);
}
sub eval_ast ($ast, $env) {
given $ast {
when MalSymbol { $env{$ast.val} || die X::MalNotFound.new(name => $ast.val) }
when MalList { MalList([$ast.map({ eval($_, $env) })]) }
when MalVector { MalVector([$ast.map({ eval($_, $env) })]) }
when MalHashMap { MalHashMap($ast.kv.map({ $^a => eval($^b, $env) }).Hash) }
default { $ast // $NIL }
}
}
sub eval ($ast, $env) {
return eval_ast($ast, $env) if $ast !~~ MalList;
return $ast if !$ast.elems;
my ($func, @args) = eval_ast($ast, $env).val;
my $arglist = MalList(@args);
return $func.apply($arglist);
}
sub print ($exp) {
return pr_str($exp, True);
}
my $repl_env;
sub rep ($str) {
return print(eval(read($str), $repl_env));
}
sub MAIN {
$repl_env<+> = MalCode({ MalNumber($^a[0].val + $^a[1].val) });
$repl_env<-> = MalCode({ MalNumber($^a[0].val - $^a[1].val) });
$repl_env<*> = MalCode({ MalNumber($^a[0].val * $^a[1].val) });
$repl_env</> = MalCode({ MalNumber(($^a[0].val / $^a[1].val).Int) });
while (my $line = prompt 'user> ').defined {
say rep($line);
CATCH {
when X::MalException { .Str.say }
}
}
}