diff options
| author | Joel Martin <github@martintribe.org> | 2014-04-16 23:57:50 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-04-16 23:57:50 -0500 |
| commit | 8cb5cda46cf3aef847ae3926dc53a5e5f87fe261 (patch) | |
| tree | 13e5b2878f19ee24272ead8a92a9cb84b33ad0e5 /php/stepA_more.php | |
| parent | a05f7822b10ed4cdd61ed8384299a003baf1c1c6 (diff) | |
| download | mal-8cb5cda46cf3aef847ae3926dc53a5e5f87fe261.tar.gz mal-8cb5cda46cf3aef847ae3926dc53a5e5f87fe261.zip | |
All: move some fns to core. Major cleanup.
- Don't import/require core until step4.
- Define cond/or macros from step8
Diffstat (limited to 'php/stepA_more.php')
| -rw-r--r-- | php/stepA_more.php | 52 |
1 files changed, 23 insertions, 29 deletions
diff --git a/php/stepA_more.php b/php/stepA_more.php index 7478ee8..c599986 100644 --- a/php/stepA_more.php +++ b/php/stepA_more.php @@ -167,47 +167,41 @@ 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('readline', 'mal_readline'); -_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); -}); +})); -// 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) \")\")))))"); rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"); rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"); -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); + exit(0); } +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); + ?> |
