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';
12 return read_str($str);
16 function eval_ast($ast, $env) {
17 if (_symbol_Q($ast)) {
18 return $env->get($ast->value
);
19 } elseif (_sequential_Q($ast)) {
25 foreach ($ast as $a) { $el[] = MAL_EVAL($a, $env); }
27 } elseif (_hash_map_Q($ast)) {
28 $new_hm = _hash_map();
29 foreach (array_keys($ast->getArrayCopy()) as $key) {
30 $new_hm[$key] = MAL_EVAL($ast[$key], $env);
38 function MAL_EVAL($ast, $env) {
41 #echo "MAL_EVAL: " . _pr_str($ast) . "\n";
43 return eval_ast($ast, $env);
48 $a0v = (_symbol_Q($a0) ?
$a0->value
: $a0);
51 $res = MAL_EVAL($ast[2], $env);
52 return $env->set($ast[1]->value
, $res);
55 $let_env = new Env($env);
56 for ($i=0; $i < count($a1); $i+
=2) {
57 $let_env->set($a1[$i]->value
, MAL_EVAL($a1[$i+
1], $let_env));
59 return MAL_EVAL($ast[2], $let_env);
61 eval_ast($ast->slice(1, -1), $env);
62 $ast = $ast[count($ast)-1];
65 $cond = MAL_EVAL($ast[1], $env);
66 if ($cond === NULL ||
$cond === false) {
67 if (count($ast) === 4) { $ast = $ast[3]; }
74 return _function('MAL_EVAL', 'native',
75 _hash_map('exp', $ast[2],
79 $el = eval_ast($ast, $env);
81 $args = array_slice($el->getArrayCopy(), 1);
82 if ($f->type
=== 'native') {
83 $ast = $f->meta
['exp'];
84 $env = new Env($f->meta
['env'], $f->meta
['params'], $args);
86 return $f->apply($args);
94 function MAL_PRINT($exp) {
95 return _pr_str($exp, True) . "\n";
99 $repl_env = new Env(NULL);
102 return MAL_PRINT(MAL_EVAL(READ($str), $repl_env));
104 function _ref($k, $v) {
106 $repl_env->set($k, _function($v));
108 // Import core functions
109 foreach ($core_ns as $k=>$v) { _ref($k, $v); }
111 _ref('read-string', 'read_str');
112 _ref('eval', function($ast) {
113 global $repl_env; return MAL_EVAL($ast, $repl_env);
115 _ref('slurp', function($f) {
116 return file_get_contents($f);
119 // Defined using the language itself
120 rep("(def! not (fn* (a) (if a false true)))");
121 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
123 if (count($argv) > 1) {
124 for ($i=1; $i < count($argv); $i++
) {
125 rep('(load-file "' . $argv[$i] . '")');
130 $line = mal_readline("user> ");
131 if ($line === NULL) { break; }
135 } catch (BlankException
$e) {
137 } catch (Exception
$e) {
138 echo "Error: " . $e->getMessage() . "\n";
139 echo $e->getTraceAsString() . "\n";