Commit | Line | Data |
---|---|---|
60f2b363 JM |
1 | use strict; |
2 | use warnings FATAL => qw(all); | |
3 | use readline qw(readline); | |
4 | use feature qw(switch); | |
5 | use Data::Dumper; | |
6 | ||
7 | use types qw($nil $true $false); | |
8 | use reader; | |
9 | use printer; | |
10 | use env; | |
11 | use core qw($core_ns); | |
12 | ||
13 | # read | |
14 | sub READ { | |
15 | my $str = shift; | |
16 | return reader::read_str($str); | |
17 | } | |
18 | ||
19 | # eval | |
20 | sub eval_ast { | |
21 | my($ast, $env) = @_; | |
22 | given (ref $ast) { | |
23 | when (/^Symbol/) { | |
24 | $env->get($$ast); | |
25 | } | |
26 | when (/^List/) { | |
27 | my @lst = map {EVAL($_, $env)} @$ast; | |
28 | return List->new(\@lst); | |
29 | } | |
30 | default { | |
31 | return $ast; | |
32 | } | |
33 | } | |
34 | } | |
35 | ||
36 | sub EVAL { | |
37 | my($ast, $env) = @_; | |
38 | ||
39 | while (1) { | |
40 | ||
41 | #print "EVAL: " . printer::_pr_str($ast) . "\n"; | |
42 | if (! ((ref $ast) =~ /^List/)) { | |
43 | return eval_ast($ast, $env); | |
44 | } | |
45 | ||
46 | # apply list | |
47 | my ($a0, $a1, $a2, $a3) = @$ast; | |
48 | given ((ref $a0) =~ /^Symbol/ ? $$a0 : $a0) { | |
49 | when (/^def!$/) { | |
50 | my $res = EVAL($a2, $env); | |
51 | return $env->set($$a1, $res); | |
52 | } | |
53 | when (/^let\*$/) { | |
54 | my $let_env = Env->new($env); | |
55 | for(my $i=0; $i < scalar(@{$a1}); $i+=2) { | |
56 | $let_env->set(${$a1->[$i]}, EVAL($a1->[$i+1], $let_env)); | |
57 | } | |
58 | return EVAL($a2, $let_env); | |
59 | } | |
60 | when (/^do$/) { | |
61 | eval_ast($ast->slice(1, $#{$ast}-1), $env); | |
62 | $ast = $ast->[$#{$ast}]; | |
63 | } | |
64 | when (/^if$/) { | |
65 | my $cond = EVAL($a1, $env); | |
66 | if ($cond eq $nil || $cond eq $false) { | |
67 | $ast = $a3 ? $a3 : $nil; | |
68 | } else { | |
69 | $ast = $a2; | |
70 | } | |
71 | } | |
72 | when (/^fn\*$/) { | |
73 | return Function->new(\&EVAL, $a2, $env, $a1); | |
74 | } | |
75 | default { | |
76 | my $el = eval_ast($ast, $env); | |
77 | my $f = $el->[0]; | |
78 | if ((ref $f) =~ /^Function/) { | |
79 | $ast = $f->{ast}; | |
80 | $env = $f->gen_env($el->rest()); | |
81 | } else { | |
82 | return &{ $f }($el->rest()); | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
87 | } # TCO while loop | |
88 | } | |
89 | ||
90 | ||
91 | sub PRINT { | |
92 | my $exp = shift; | |
93 | return printer::_pr_str($exp); | |
94 | } | |
95 | ||
96 | # repl | |
97 | my $repl_env = Env->new(); | |
98 | sub REP { | |
99 | my $str = shift; | |
100 | return PRINT(EVAL(READ($str), $repl_env)); | |
101 | } | |
102 | ||
103 | # core.pl: defined using perl | |
104 | foreach my $n (%$core_ns) { $repl_env->set($n, $core_ns->{$n}); } | |
105 | ||
106 | # core.mal: defined using the language itself | |
107 | REP("(def! not (fn* (a) (if a false true)))"); | |
108 | ||
109 | while (1) { | |
110 | my $line = readline("user> "); | |
111 | if (! defined $line) { last; } | |
112 | eval { | |
113 | use autodie; # always "throw" errors | |
114 | print(REP($line), "\n"); | |
115 | 1; | |
116 | }; | |
117 | if (my $err = $@) { | |
118 | chomp $err; | |
119 | print "Error: $err\n"; | |
120 | } | |
121 | } |