2014-03-25 01:32:24 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'readline.php';
|
|
|
|
require_once 'types.php';
|
|
|
|
require_once 'reader.php';
|
2014-04-03 07:23:37 +04:00
|
|
|
require_once 'printer.php';
|
|
|
|
require_once 'env.php';
|
|
|
|
require_once 'core.php';
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
// read
|
|
|
|
function READ($str) {
|
|
|
|
return read_str($str);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eval
|
|
|
|
function eval_ast($ast, $env) {
|
2014-04-03 07:23:37 +04:00
|
|
|
if (_symbol_Q($ast)) {
|
2014-12-19 05:33:49 +03:00
|
|
|
return $env->get($ast);
|
2014-04-03 07:23:37 +04:00
|
|
|
} elseif (_sequential_Q($ast)) {
|
|
|
|
if (_list_Q($ast)) {
|
|
|
|
$el = _list();
|
2014-03-25 01:32:24 +04:00
|
|
|
} else {
|
2014-04-03 07:23:37 +04:00
|
|
|
$el = _vector();
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
foreach ($ast as $a) { $el[] = MAL_EVAL($a, $env); }
|
|
|
|
return $el;
|
2014-04-03 07:23:37 +04:00
|
|
|
} elseif (_hash_map_Q($ast)) {
|
|
|
|
$new_hm = _hash_map();
|
2014-03-25 01:32:24 +04:00
|
|
|
foreach (array_keys($ast->getArrayCopy()) as $key) {
|
|
|
|
$new_hm[$key] = MAL_EVAL($ast[$key], $env);
|
|
|
|
}
|
|
|
|
return $new_hm;
|
|
|
|
} else {
|
|
|
|
return $ast;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function MAL_EVAL($ast, $env) {
|
|
|
|
#echo "MAL_EVAL: " . _pr_str($ast) . "\n";
|
2014-04-03 07:23:37 +04:00
|
|
|
if (!_list_Q($ast)) {
|
2014-03-25 01:32:24 +04:00
|
|
|
return eval_ast($ast, $env);
|
|
|
|
}
|
2016-03-30 15:56:24 +03:00
|
|
|
if ($ast->count() === 0) {
|
|
|
|
return $ast;
|
|
|
|
}
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
// apply list
|
|
|
|
$a0 = $ast[0];
|
2014-04-03 07:23:37 +04:00
|
|
|
$a0v = (_symbol_Q($a0) ? $a0->value : $a0);
|
2014-03-25 01:32:24 +04:00
|
|
|
switch ($a0v) {
|
|
|
|
case "def!":
|
|
|
|
$res = MAL_EVAL($ast[2], $env);
|
2014-12-19 05:33:49 +03:00
|
|
|
return $env->set($ast[1], $res);
|
2014-03-25 01:32:24 +04:00
|
|
|
case "let*":
|
|
|
|
$a1 = $ast[1];
|
|
|
|
$let_env = new Env($env);
|
|
|
|
for ($i=0; $i < count($a1); $i+=2) {
|
2014-12-19 05:33:49 +03:00
|
|
|
$let_env->set($a1[$i], MAL_EVAL($a1[$i+1], $let_env));
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
return MAL_EVAL($ast[2], $let_env);
|
|
|
|
case "do":
|
|
|
|
#$el = eval_ast(array_slice($ast->getArrayCopy(), 1), $env);
|
|
|
|
$el = eval_ast($ast->slice(1), $env);
|
|
|
|
return $el[count($el)-1];
|
|
|
|
case "if":
|
|
|
|
$cond = MAL_EVAL($ast[1], $env);
|
|
|
|
if ($cond === NULL || $cond === false) {
|
|
|
|
if (count($ast) === 4) { return MAL_EVAL($ast[3], $env); }
|
|
|
|
else { return NULL; }
|
|
|
|
} else {
|
|
|
|
return MAL_EVAL($ast[2], $env);
|
|
|
|
}
|
|
|
|
case "fn*":
|
|
|
|
return function() use ($env, $ast) {
|
|
|
|
$fn_env = new Env($env, $ast[1], func_get_args());
|
|
|
|
return MAL_EVAL($ast[2], $fn_env);
|
|
|
|
};
|
|
|
|
default:
|
|
|
|
$el = eval_ast($ast, $env);
|
|
|
|
$f = $el[0];
|
|
|
|
return call_user_func_array($f, array_slice($el->getArrayCopy(), 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// print
|
|
|
|
function MAL_PRINT($exp) {
|
2015-03-07 18:04:07 +03:00
|
|
|
return _pr_str($exp, True);
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// repl
|
|
|
|
$repl_env = new Env(NULL);
|
|
|
|
function rep($str) {
|
|
|
|
global $repl_env;
|
|
|
|
return MAL_PRINT(MAL_EVAL(READ($str), $repl_env));
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:57:50 +04:00
|
|
|
// core.php: defined using PHP
|
|
|
|
foreach ($core_ns as $k=>$v) {
|
2014-12-19 05:33:49 +03:00
|
|
|
$repl_env->set(_symbol($k), _function($v));
|
2014-04-17 08:57:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// core.mal: defined using the language itself
|
2014-03-25 01:32:24 +04:00
|
|
|
rep("(def! not (fn* (a) (if a false true)))");
|
|
|
|
|
2014-04-19 22:04:09 +04:00
|
|
|
// repl loop
|
2014-03-25 01:32:24 +04:00
|
|
|
do {
|
|
|
|
try {
|
|
|
|
$line = mal_readline("user> ");
|
|
|
|
if ($line === NULL) { break; }
|
|
|
|
if ($line !== "") {
|
2015-03-07 18:04:07 +03:00
|
|
|
print(rep($line) . "\n");
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
} catch (BlankException $e) {
|
|
|
|
continue;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo "Error: " . $e->getMessage() . "\n";
|
|
|
|
echo $e->getTraceAsString() . "\n";
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
|
2014-04-19 22:04:09 +04:00
|
|
|
?>
|