2014-03-24 16:32:24 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require_once 'readline.php';
|
|
|
|
|
require_once 'types.php';
|
|
|
|
|
require_once 'reader.php';
|
2014-04-02 22:23:37 -05:00
|
|
|
require_once 'printer.php';
|
2014-03-24 16:32:24 -05:00
|
|
|
|
|
|
|
|
// read
|
|
|
|
|
function READ($str) {
|
|
|
|
|
return read_str($str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eval
|
2022-01-10 00:15:40 +01:00
|
|
|
function MAL_EVAL($ast, $env) {
|
|
|
|
|
// echo "EVAL: " . _pr_str($ast) . "\n";
|
|
|
|
|
|
2014-04-02 22:23:37 -05:00
|
|
|
if (_symbol_Q($ast)) {
|
2014-03-24 16:32:24 -05:00
|
|
|
return $env[$ast->value];
|
2022-01-10 00:15:40 +01:00
|
|
|
} elseif (_vector_Q($ast)) {
|
2014-04-02 22:23:37 -05:00
|
|
|
$el = _vector();
|
2014-03-24 16:32:24 -05:00
|
|
|
foreach ($ast as $a) { $el[] = MAL_EVAL($a, $env); }
|
|
|
|
|
return $el;
|
2014-04-02 22:23:37 -05:00
|
|
|
} elseif (_hash_map_Q($ast)) {
|
|
|
|
|
$new_hm = _hash_map();
|
2014-03-24 16:32:24 -05:00
|
|
|
foreach (array_keys($ast->getArrayCopy()) as $key) {
|
|
|
|
|
$new_hm[$key] = MAL_EVAL($ast[$key], $env);
|
|
|
|
|
}
|
|
|
|
|
return $new_hm;
|
2022-01-10 00:15:40 +01:00
|
|
|
} elseif (!_list_Q($ast)) {
|
2014-03-24 16:32:24 -05:00
|
|
|
return $ast;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 17:56:04 -05:00
|
|
|
if ($ast->count() === 0) {
|
|
|
|
|
return $ast;
|
|
|
|
|
}
|
2014-03-24 16:32:24 -05:00
|
|
|
|
|
|
|
|
// apply list
|
2022-01-10 00:15:40 +01:00
|
|
|
$el = [];
|
|
|
|
|
foreach ($ast as $a) { $el[] = MAL_EVAL($a, $env); }
|
2014-03-24 16:32:24 -05:00
|
|
|
$f = $el[0];
|
2022-01-10 00:15:40 +01:00
|
|
|
$args = array_slice($el, 1);
|
|
|
|
|
return call_user_func_array($f, $args);
|
2014-03-24 16:32:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// print
|
|
|
|
|
function MAL_PRINT($exp) {
|
2015-03-07 09:04:07 -06:00
|
|
|
return _pr_str($exp, True);
|
2014-03-24 16:32:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// repl
|
|
|
|
|
$repl_env = array();
|
|
|
|
|
function rep($str) {
|
|
|
|
|
global $repl_env;
|
|
|
|
|
return MAL_PRINT(MAL_EVAL(READ($str), $repl_env));
|
|
|
|
|
}
|
2014-04-16 23:57:50 -05:00
|
|
|
|
2014-03-24 16:32:24 -05:00
|
|
|
$repl_env['+'] = function ($a, $b) { return intval($a + $b,10); };
|
|
|
|
|
$repl_env['-'] = function ($a, $b) { return intval($a - $b,10); };
|
|
|
|
|
$repl_env['*'] = function ($a, $b) { return intval($a * $b,10); };
|
|
|
|
|
$repl_env['/'] = function ($a, $b) { return intval($a / $b,10); };
|
|
|
|
|
|
2014-04-19 13:04:09 -05:00
|
|
|
// repl loop
|
2014-03-24 16:32:24 -05:00
|
|
|
do {
|
|
|
|
|
try {
|
|
|
|
|
$line = mal_readline("user> ");
|
|
|
|
|
if ($line === NULL) { break; }
|
|
|
|
|
if ($line !== "") {
|
2015-03-07 09:04:07 -06:00
|
|
|
print(rep($line) . "\n");
|
2014-03-24 16:32:24 -05:00
|
|
|
}
|
|
|
|
|
} catch (BlankException $e) {
|
|
|
|
|
continue;
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
echo "Error: " . $e->getMessage() . "\n";
|
|
|
|
|
echo $e->getTraceAsString() . "\n";
|
|
|
|
|
}
|
|
|
|
|
} while (true);
|
|
|
|
|
|
2014-04-19 13:04:09 -05:00
|
|
|
?>
|