aboutsummaryrefslogtreecommitdiff
path: root/php/step9_interop.php
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-02 22:23:37 -0500
committerJoel Martin <github@martintribe.org>2014-04-02 22:23:37 -0500
commitea81a8087bcd7953b083a2be9db447f75e7ebf56 (patch)
tree6cf47a2dbd55d42efc4a901eaabdec952f40ce89 /php/step9_interop.php
parent1617910ad342a55762f3ddabb975849d843cff85 (diff)
downloadmal-ea81a8087bcd7953b083a2be9db447f75e7ebf56.tar.gz
mal-ea81a8087bcd7953b083a2be9db447f75e7ebf56.zip
All: split types into types, env, printer, core.
- types: low-level mapping to the implementation language. - core: functions on types that are exposed directly to mal. - printer: implementation called by pr-str, str, prn, println. - env: the environment implementation - Also, unindent all TCO while loops so that the diff of step4 and step5 are minimized.
Diffstat (limited to 'php/step9_interop.php')
-rw-r--r--php/step9_interop.php165
1 files changed, 85 insertions, 80 deletions
diff --git a/php/step9_interop.php b/php/step9_interop.php
index 26e89f0..a699109 100644
--- a/php/step9_interop.php
+++ b/php/step9_interop.php
@@ -3,6 +3,9 @@
require_once 'readline.php';
require_once 'types.php';
require_once 'reader.php';
+require_once 'printer.php';
+require_once 'env.php';
+require_once 'core.php';
// read
function READ($str) {
@@ -11,27 +14,27 @@ function READ($str) {
// eval
function is_pair($x) {
- return sequential_Q($x) and count($x) > 0;
+ return _sequential_Q($x) and count($x) > 0;
}
function quasiquote($ast) {
if (!is_pair($ast)) {
- return new_list(new_symbol("quote"), $ast);
- } elseif (symbol_Q($ast[0]) && $ast[0]->value === 'unquote') {
+ return _list(_symbol("quote"), $ast);
+ } elseif (_symbol_Q($ast[0]) && $ast[0]->value === 'unquote') {
return $ast[1];
- } elseif (is_pair($ast[0]) && symbol_Q($ast[0][0]) &&
+ } elseif (is_pair($ast[0]) && _symbol_Q($ast[0][0]) &&
$ast[0][0]->value === 'splice-unquote') {
- return new_list(new_symbol("concat"), $ast[0][1],
- quasiquote($ast->slice(1)));
+ return _list(_symbol("concat"), $ast[0][1],
+ quasiquote($ast->slice(1)));
} else {
- return new_list(new_symbol("cons"), quasiquote($ast[0]),
- quasiquote($ast->slice(1)));
+ return _list(_symbol("cons"), quasiquote($ast[0]),
+ quasiquote($ast->slice(1)));
}
}
function is_macro_call($ast, $env) {
return is_pair($ast) &&
- symbol_Q($ast[0]) &&
+ _symbol_Q($ast[0]) &&
$env->find($ast[0]->value) &&
$env->get($ast[0]->value)->ismacro;
}
@@ -46,18 +49,18 @@ function macroexpand($ast, $env) {
}
function eval_ast($ast, $env) {
- if (symbol_Q($ast)) {
+ if (_symbol_Q($ast)) {
return $env->get($ast->value);
- } elseif (list_Q($ast) || vector_Q($ast)) {
- if (list_Q($ast)) {
- $el = new_list();
+ } elseif (_sequential_Q($ast)) {
+ if (_list_Q($ast)) {
+ $el = _list();
} else {
- $el = new_vector();
+ $el = _vector();
}
foreach ($ast as $a) { $el[] = MAL_EVAL($a, $env); }
return $el;
- } elseif (hash_map_Q($ast)) {
- $new_hm = new_hash_map();
+ } elseif (_hash_map_Q($ast)) {
+ $new_hm = _hash_map();
foreach (array_keys($ast->getArrayCopy()) as $key) {
$new_hm[$key] = MAL_EVAL($ast[$key], $env);
}
@@ -69,69 +72,71 @@ function eval_ast($ast, $env) {
function MAL_EVAL($ast, $env) {
while (true) {
- #echo "MAL_EVAL: " . _pr_str($ast) . "\n";
- if (!list_Q($ast)) {
- return eval_ast($ast, $env);
- }
- // apply list
- $ast = macroexpand($ast, $env);
- if (!list_Q($ast)) { return $ast; }
-
- $a0 = $ast[0];
- $a0v = (symbol_Q($a0) ? $a0->value : $a0);
- switch ($a0v) {
- case "def!":
- $res = MAL_EVAL($ast[2], $env);
- return $env->set($ast[1]->value, $res);
- case "let*":
- $a1 = $ast[1];
- $let_env = new Env($env);
- for ($i=0; $i < count($a1); $i+=2) {
- $let_env->set($a1[$i]->value, MAL_EVAL($a1[$i+1], $let_env));
- }
- return MAL_EVAL($ast[2], $let_env);
- case "quote":
- return $ast[1];
- case "quasiquote":
- return MAL_EVAL(quasiquote($ast[1]), $env);
- case "defmacro!":
- $func = MAL_EVAL($ast[2], $env);
- $func->ismacro = true;
- return $env->set($ast[1]->value, $func);
- case "macroexpand":
- return macroexpand($ast[1], $env);
- case "php*":
- return eval($ast[1]);
- case "do":
- eval_ast($ast->slice(1, -1), $env);
- $ast = $ast[count($ast)-1];
- break;
- case "if":
- $cond = MAL_EVAL($ast[1], $env);
- if ($cond === NULL || $cond === false) {
- if (count($ast) === 4) { $ast = $ast[3]; }
- else { $ast = NULL; }
- } else {
- $ast = $ast[2];
- }
- break;
- case "fn*":
- return new_function('MAL_EVAL', 'native',
- new_hash_map('exp', $ast[2],
- 'env', $env,
- 'params', $ast[1]));
- default:
- $el = eval_ast($ast, $env);
- $f = $el[0];
- $args = array_slice($el->getArrayCopy(), 1);
- if ($f->type === 'native') {
- $ast = $f->meta['exp'];
- $env = new Env($f->meta['env'], $f->meta['params'], $args);
- } else {
- return $f->apply($args);
- }
+ #echo "MAL_EVAL: " . _pr_str($ast) . "\n";
+ if (!_list_Q($ast)) {
+ return eval_ast($ast, $env);
+ }
+
+ // apply list
+ $ast = macroexpand($ast, $env);
+ if (!_list_Q($ast)) { return $ast; }
+
+ $a0 = $ast[0];
+ $a0v = (_symbol_Q($a0) ? $a0->value : $a0);
+ switch ($a0v) {
+ case "def!":
+ $res = MAL_EVAL($ast[2], $env);
+ return $env->set($ast[1]->value, $res);
+ case "let*":
+ $a1 = $ast[1];
+ $let_env = new Env($env);
+ for ($i=0; $i < count($a1); $i+=2) {
+ $let_env->set($a1[$i]->value, MAL_EVAL($a1[$i+1], $let_env));
}
+ return MAL_EVAL($ast[2], $let_env);
+ case "quote":
+ return $ast[1];
+ case "quasiquote":
+ return MAL_EVAL(quasiquote($ast[1]), $env);
+ case "defmacro!":
+ $func = MAL_EVAL($ast[2], $env);
+ $func->ismacro = true;
+ return $env->set($ast[1]->value, $func);
+ case "macroexpand":
+ return macroexpand($ast[1], $env);
+ case "php*":
+ return eval($ast[1]);
+ case "do":
+ eval_ast($ast->slice(1, -1), $env);
+ $ast = $ast[count($ast)-1];
+ break;
+ case "if":
+ $cond = MAL_EVAL($ast[1], $env);
+ if ($cond === NULL || $cond === false) {
+ if (count($ast) === 4) { $ast = $ast[3]; }
+ else { $ast = NULL; }
+ } else {
+ $ast = $ast[2];
+ }
+ break;
+ case "fn*":
+ return _function('MAL_EVAL', 'native',
+ _hash_map('exp', $ast[2],
+ 'env', $env,
+ 'params', $ast[1]));
+ default:
+ $el = eval_ast($ast, $env);
+ $f = $el[0];
+ $args = array_slice($el->getArrayCopy(), 1);
+ if ($f->type === 'native') {
+ $ast = $f->meta['exp'];
+ $env = new Env($f->meta['env'], $f->meta['params'], $args);
+ } else {
+ return $f->apply($args);
+ }
+ }
+
}
}
@@ -148,10 +153,10 @@ function rep($str) {
}
function _ref($k, $v) {
global $repl_env;
- $repl_env->set($k, new_function($v));
+ $repl_env->set($k, _function($v));
}
-// Import types functions
-foreach ($types_ns as $k=>$v) { _ref($k, $v); }
+// Import core functions
+foreach ($core_ns as $k=>$v) { _ref($k, $v); }
_ref('read-string', 'read_str');
_ref('eval', function($ast) {