Merge pull request #408 from asarhaddon/haskell-style
[jackhill/mal.git] / php / step9_try.php
1 <?php
2
3 require_once 'readline.php';
4 require_once 'types.php';
5 require_once 'reader.php';
6 require_once 'printer.php';
7 require_once 'env.php';
8 require_once 'core.php';
9
10 // read
11 function READ($str) {
12 return read_str($str);
13 }
14
15 // eval
16 function is_pair($x) {
17 return _sequential_Q($x) and count($x) > 0;
18 }
19
20 function quasiquote($ast) {
21 if (!is_pair($ast)) {
22 return _list(_symbol("quote"), $ast);
23 } elseif (_symbol_Q($ast[0]) && $ast[0]->value === 'unquote') {
24 return $ast[1];
25 } elseif (is_pair($ast[0]) && _symbol_Q($ast[0][0]) &&
26 $ast[0][0]->value === 'splice-unquote') {
27 return _list(_symbol("concat"), $ast[0][1],
28 quasiquote($ast->slice(1)));
29 } else {
30 return _list(_symbol("cons"), quasiquote($ast[0]),
31 quasiquote($ast->slice(1)));
32 }
33 }
34
35 function is_macro_call($ast, $env) {
36 return is_pair($ast) &&
37 _symbol_Q($ast[0]) &&
38 $env->find($ast[0]) &&
39 $env->get($ast[0])->ismacro;
40 }
41
42 function macroexpand($ast, $env) {
43 while (is_macro_call($ast, $env)) {
44 $mac = $env->get($ast[0]);
45 $args = array_slice($ast->getArrayCopy(),1);
46 $ast = $mac->apply($args);
47 }
48 return $ast;
49 }
50
51 function eval_ast($ast, $env) {
52 if (_symbol_Q($ast)) {
53 return $env->get($ast);
54 } elseif (_sequential_Q($ast)) {
55 if (_list_Q($ast)) {
56 $el = _list();
57 } else {
58 $el = _vector();
59 }
60 foreach ($ast as $a) { $el[] = MAL_EVAL($a, $env); }
61 return $el;
62 } elseif (_hash_map_Q($ast)) {
63 $new_hm = _hash_map();
64 foreach (array_keys($ast->getArrayCopy()) as $key) {
65 $new_hm[$key] = MAL_EVAL($ast[$key], $env);
66 }
67 return $new_hm;
68 } else {
69 return $ast;
70 }
71 }
72
73 function MAL_EVAL($ast, $env) {
74 while (true) {
75
76 #echo "MAL_EVAL: " . _pr_str($ast) . "\n";
77 if (!_list_Q($ast)) {
78 return eval_ast($ast, $env);
79 }
80
81 // apply list
82 $ast = macroexpand($ast, $env);
83 if (!_list_Q($ast)) {
84 return eval_ast($ast, $env);
85 }
86 if ($ast->count() === 0) {
87 return $ast;
88 }
89
90 $a0 = $ast[0];
91 $a0v = (_symbol_Q($a0) ? $a0->value : $a0);
92 switch ($a0v) {
93 case "def!":
94 $res = MAL_EVAL($ast[2], $env);
95 return $env->set($ast[1], $res);
96 case "let*":
97 $a1 = $ast[1];
98 $let_env = new Env($env);
99 for ($i=0; $i < count($a1); $i+=2) {
100 $let_env->set($a1[$i], MAL_EVAL($a1[$i+1], $let_env));
101 }
102 $ast = $ast[2];
103 $env = $let_env;
104 break; // Continue loop (TCO)
105 case "quote":
106 return $ast[1];
107 case "quasiquote":
108 $ast = quasiquote($ast[1]);
109 break; // Continue loop (TCO)
110 case "defmacro!":
111 $func = MAL_EVAL($ast[2], $env);
112 $func->ismacro = true;
113 return $env->set($ast[1], $func);
114 case "macroexpand":
115 return macroexpand($ast[1], $env);
116 case "try*":
117 $a1 = $ast[1];
118 $a2 = $ast[2];
119 if ($a2[0]->value === "catch*") {
120 try {
121 return MAL_EVAL($a1, $env);
122 } catch (_Error $e) {
123 $catch_env = new Env($env, array($a2[1]),
124 array($e->obj));
125 return MAL_EVAL($a2[2], $catch_env);
126 } catch (Exception $e) {
127 $catch_env = new Env($env, array($a2[1]),
128 array($e->getMessage()));
129 return MAL_EVAL($a2[2], $catch_env);
130 }
131 } else {
132 return MAL_EVAL($a1, $env);
133 }
134 case "do":
135 eval_ast($ast->slice(1, -1), $env);
136 $ast = $ast[count($ast)-1];
137 break; // Continue loop (TCO)
138 case "if":
139 $cond = MAL_EVAL($ast[1], $env);
140 if ($cond === NULL || $cond === false) {
141 if (count($ast) === 4) { $ast = $ast[3]; }
142 else { $ast = NULL; }
143 } else {
144 $ast = $ast[2];
145 }
146 break; // Continue loop (TCO)
147 case "fn*":
148 return _function('MAL_EVAL', 'native',
149 $ast[2], $env, $ast[1]);
150 default:
151 $el = eval_ast($ast, $env);
152 $f = $el[0];
153 $args = array_slice($el->getArrayCopy(), 1);
154 if ($f->type === 'native') {
155 $ast = $f->ast;
156 $env = $f->gen_env($args);
157 // Continue loop (TCO)
158 } else {
159 return $f->apply($args);
160 }
161 }
162
163 }
164 }
165
166 // print
167 function MAL_PRINT($exp) {
168 return _pr_str($exp, True);
169 }
170
171 // repl
172 $repl_env = new Env(NULL);
173 function rep($str) {
174 global $repl_env;
175 return MAL_PRINT(MAL_EVAL(READ($str), $repl_env));
176 }
177
178 // core.php: defined using PHP
179 foreach ($core_ns as $k=>$v) {
180 $repl_env->set(_symbol($k), _function($v));
181 }
182 $repl_env->set(_symbol('eval'), _function(function($ast) {
183 global $repl_env; return MAL_EVAL($ast, $repl_env);
184 }));
185 $_argv = _list();
186 for ($i=2; $i < count($argv); $i++) {
187 $_argv->append($argv[$i]);
188 }
189 $repl_env->set(_symbol('*ARGV*'), $_argv);
190
191 // core.mal: defined using the language itself
192 rep("(def! not (fn* (a) (if a false true)))");
193 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
194 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)))))))");
195 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))))))))");
196
197 if (count($argv) > 1) {
198 rep('(load-file "' . $argv[1] . '")');
199 exit(0);
200 }
201
202 // repl loop
203 do {
204 try {
205 $line = mal_readline("user> ");
206 if ($line === NULL) { break; }
207 if ($line !== "") {
208 print(rep($line) . "\n");
209 }
210 } catch (BlankException $e) {
211 continue;
212 } catch (_Error $e) {
213 echo "Error: " . _pr_str($e->obj, True) . "\n";
214 } catch (Exception $e) {
215 echo "Error: " . $e->getMessage() . "\n";
216 echo $e->getTraceAsString() . "\n";
217 }
218 } while (true);
219
220 ?>