perl: add conj
[jackhill/mal.git] / perl / step8_macros.pl
1 use strict;
2 use warnings FATAL => qw(all);
3 no if $] >= 5.018, warnings => "experimental::smartmatch";
4 use File::Basename;
5 use lib dirname (__FILE__);
6 use readline qw(mal_readline set_rl_mode);
7 use feature qw(switch);
8 use Data::Dumper;
9
10 use types qw($nil $true $false _sequential_Q _symbol_Q _list_Q);
11 use reader;
12 use printer;
13 use env;
14 use core qw($core_ns);
15
16 # read
17 sub READ {
18 my $str = shift;
19 return reader::read_str($str);
20 }
21
22 # eval
23 sub is_pair {
24 my ($x) = @_;
25 return _sequential_Q($x) && scalar(@{$x->{val}}) > 0;
26 }
27
28 sub quasiquote {
29 my ($ast) = @_;
30 if (!is_pair($ast)) {
31 return List->new([Symbol->new("quote"), $ast]);
32 } elsif (_symbol_Q($ast->nth(0)) && ${$ast->nth(0)} eq 'unquote') {
33 return $ast->nth(1);
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"),
37 $ast->nth(0)->nth(1),
38 quasiquote($ast->rest())]);
39 } else {
40 return List->new([Symbol->new("cons"),
41 quasiquote($ast->nth(0)),
42 quasiquote($ast->rest())]);
43 }
44 }
45
46 sub is_macro_call {
47 my ($ast, $env) = @_;
48 if (_list_Q($ast) &&
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/) {
53 return $f->{ismacro};
54 }
55 }
56 return 0;
57 }
58
59 sub macroexpand {
60 my ($ast, $env) = @_;
61 while (is_macro_call($ast, $env)) {
62 my $mac = $env->get($ast->nth(0));
63 $ast = $mac->apply($ast->rest());
64 }
65 return $ast;
66 }
67
68
69 sub eval_ast {
70 my($ast, $env) = @_;
71 given (ref $ast) {
72 when (/^Symbol/) {
73 $env->get($ast);
74 }
75 when (/^List/) {
76 my @lst = map {EVAL($_, $env)} @{$ast->{val}};
77 return List->new(\@lst);
78 }
79 when (/^Vector/) {
80 my @lst = map {EVAL($_, $env)} @{$ast->{val}};
81 return Vector->new(\@lst);
82 }
83 when (/^HashMap/) {
84 my $new_hm = {};
85 foreach my $k (keys( %{ $ast->{val} })) {
86 $new_hm->{$k} = EVAL($ast->get($k), $env);
87 }
88 return HashMap->new($new_hm);
89 }
90 default {
91 return $ast;
92 }
93 }
94 }
95
96 sub EVAL {
97 my($ast, $env) = @_;
98
99 while (1) {
100
101 #print "EVAL: " . printer::_pr_str($ast) . "\n";
102 if (! _list_Q($ast)) {
103 return eval_ast($ast, $env);
104 }
105
106 # apply list
107 $ast = macroexpand($ast, $env);
108 if (! _list_Q($ast)) { return $ast; }
109
110 my ($a0, $a1, $a2, $a3) = @{$ast->{val}};
111 given ((ref $a0) =~ /^Symbol/ ? $$a0 : $a0) {
112 when (/^def!$/) {
113 my $res = EVAL($a2, $env);
114 return $env->set($a1, $res);
115 }
116 when (/^let\*$/) {
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));
120 }
121 $ast = $a2;
122 $env = $let_env;
123 # Continue loop (TCO)
124 }
125 when (/^quote$/) {
126 return $a1;
127 }
128 when (/^quasiquote$/) {
129 $ast = quasiquote($a1);
130 # Continue loop (TCO)
131 }
132 when (/^defmacro!$/) {
133 my $func = EVAL($a2, $env);
134 $func->{ismacro} = 1;
135 return $env->set($a1, $func);
136 }
137 when (/^macroexpand$/) {
138 return macroexpand($a1, $env);
139 }
140 when (/^do$/) {
141 eval_ast($ast->slice(1, $#{$ast->{val}}-1), $env);
142 $ast = $ast->nth($#{$ast->{val}});
143 # Continue loop (TCO)
144 }
145 when (/^if$/) {
146 my $cond = EVAL($a1, $env);
147 if ($cond eq $nil || $cond eq $false) {
148 $ast = $a3 ? $a3 : $nil;
149 } else {
150 $ast = $a2;
151 }
152 # Continue loop (TCO)
153 }
154 when (/^fn\*$/) {
155 return Function->new(\&EVAL, $a2, $env, $a1);
156 }
157 default {
158 my $el = eval_ast($ast, $env);
159 my $f = $el->nth(0);
160 if ((ref $f) =~ /^Function/) {
161 $ast = $f->{ast};
162 $env = $f->gen_env($el->rest());
163 # Continue loop (TCO)
164 } else {
165 return &{ $f }($el->rest());
166 }
167 }
168 }
169
170 } # TCO while loop
171 }
172
173 # print
174 sub PRINT {
175 my $exp = shift;
176 return printer::_pr_str($exp);
177 }
178
179 # repl
180 my $repl_env = Env->new();
181 sub REP {
182 my $str = shift;
183 return PRINT(EVAL(READ($str), $repl_env));
184 }
185
186 # core.pl: defined using perl
187 foreach my $n (%$core_ns) {
188 $repl_env->set(Symbol->new($n), $core_ns->{$n});
189 }
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));
193
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))))))))");
199
200
201 if (scalar(@ARGV) > 0 && $ARGV[0] eq "--raw") {
202 set_rl_mode("raw");
203 shift @ARGV;
204 }
205 if (scalar(@ARGV) > 0) {
206 REP("(load-file \"" . $ARGV[0] . "\")");
207 exit 0;
208 }
209 while (1) {
210 my $line = mal_readline("user> ");
211 if (! defined $line) { last; }
212 do {
213 local $@;
214 my $ret;
215 eval {
216 use autodie; # always "throw" errors
217 print(REP($line), "\n");
218 1;
219 } or do {
220 my $err = $@;
221 given (ref $err) {
222 when (/^BlankException/) {
223 # ignore and continue
224 }
225 default {
226 chomp $err;
227 print "Error: $err\n";
228 }
229 }
230 };
231 };
232 }