aboutsummaryrefslogtreecommitdiff
path: root/php/step6_file.php
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-27 18:28:56 -0500
committerJoel Martin <github@martintribe.org>2014-04-27 18:28:56 -0500
commit72599899e3d3ae1face46a81f01979b040d19c61 (patch)
tree95e163ffacaa7061a1ac90ccd8a1418656ebc5e3 /php/step6_file.php
parentd574187895c8519bffd5a0f8b4c1817e00103d91 (diff)
parentcc021efe10380039a13da5300990639203450634 (diff)
downloadmal-72599899e3d3ae1face46a81f01979b040d19c61.tar.gz
mal-72599899e3d3ae1face46a81f01979b040d19c61.zip
Merge branch 'master' into gh-pages
Diffstat (limited to 'php/step6_file.php')
-rw-r--r--php/step6_file.php78
1 files changed, 39 insertions, 39 deletions
diff --git a/php/step6_file.php b/php/step6_file.php
index 965ff88..1e83c28 100644
--- a/php/step6_file.php
+++ b/php/step6_file.php
@@ -56,11 +56,13 @@ function MAL_EVAL($ast, $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);
+ $ast = $ast[2];
+ $env = $let_env;
+ break; // Continue loop (TCO)
case "do":
eval_ast($ast->slice(1, -1), $env);
$ast = $ast[count($ast)-1];
- break;
+ break; // Continue loop (TCO)
case "if":
$cond = MAL_EVAL($ast[1], $env);
if ($cond === NULL || $cond === false) {
@@ -69,19 +71,18 @@ function MAL_EVAL($ast, $env) {
} else {
$ast = $ast[2];
}
- break;
+ break; // Continue loop (TCO)
case "fn*":
return _function('MAL_EVAL', 'native',
- _hash_map('exp', $ast[2],
- 'env', $env,
- 'params', $ast[1]));
+ $ast[2], $env, $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);
+ $ast = $f->ast;
+ $env = $f->gen_env($args);
+ // Continue loop (TCO)
} else {
return $f->apply($args);
}
@@ -101,44 +102,43 @@ function rep($str) {
global $repl_env;
return MAL_PRINT(MAL_EVAL(READ($str), $repl_env));
}
-function _ref($k, $v) {
- global $repl_env;
+
+// core.php: defined using PHP
+foreach ($core_ns as $k=>$v) {
$repl_env->set($k, _function($v));
}
-// Import core functions
-foreach ($core_ns as $k=>$v) { _ref($k, $v); }
-
-_ref('read-string', 'read_str');
-_ref('eval', function($ast) {
+$repl_env->set('eval', _function(function($ast) {
global $repl_env; return MAL_EVAL($ast, $repl_env);
-});
-_ref('slurp', function($f) {
- return file_get_contents($f);
-});
+}));
+$_argv = _list();
+for ($i=2; $i < count($argv); $i++) {
+ $_argv->append($argv[$i]);
+}
+$repl_env->set('*ARGV*', $_argv);
-// Defined using the language itself
+// core.mal: defined using the language itself
rep("(def! not (fn* (a) (if a false true)))");
rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
if (count($argv) > 1) {
- for ($i=1; $i < count($argv); $i++) {
- rep('(load-file "' . $argv[$i] . '")');
- }
-} else {
- do {
- try {
- $line = mal_readline("user> ");
- if ($line === NULL) { break; }
- if ($line !== "") {
- print(rep($line));
- }
- } catch (BlankException $e) {
- continue;
- } catch (Exception $e) {
- echo "Error: " . $e->getMessage() . "\n";
- echo $e->getTraceAsString() . "\n";
- }
- } while (true);
+ rep('(load-file "' . $argv[1] . '")');
+ exit(0);
}
-?>
+// repl loop
+do {
+ try {
+ $line = mal_readline("user> ");
+ if ($line === NULL) { break; }
+ if ($line !== "") {
+ print(rep($line));
+ }
+ } catch (BlankException $e) {
+ continue;
+ } catch (Exception $e) {
+ echo "Error: " . $e->getMessage() . "\n";
+ echo $e->getTraceAsString() . "\n";
+ }
+} while (true);
+
+?>