aboutsummaryrefslogtreecommitdiff
path: root/php/step8_macros.php
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-16 23:57:50 -0500
committerJoel Martin <github@martintribe.org>2014-04-16 23:57:50 -0500
commit8cb5cda46cf3aef847ae3926dc53a5e5f87fe261 (patch)
tree13e5b2878f19ee24272ead8a92a9cb84b33ad0e5 /php/step8_macros.php
parenta05f7822b10ed4cdd61ed8384299a003baf1c1c6 (diff)
downloadmal-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/step8_macros.php')
-rw-r--r--php/step8_macros.php51
1 files changed, 24 insertions, 27 deletions
diff --git a/php/step8_macros.php b/php/step8_macros.php
index c6c7173..4723ffd 100644
--- a/php/step8_macros.php
+++ b/php/step8_macros.php
@@ -147,44 +147,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('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))))))))");
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);
+
?>