aboutsummaryrefslogtreecommitdiff
path: root/php
diff options
context:
space:
mode:
Diffstat (limited to 'php')
-rw-r--r--php/core.php6
-rw-r--r--php/step2_eval.php1
-rw-r--r--php/step3_env.php9
-rw-r--r--php/step4_if_fn_do.php10
-rw-r--r--php/step5_tco.php9
-rw-r--r--php/step6_file.php49
-rw-r--r--php/step7_quote.php49
-rw-r--r--php/step8_macros.php51
-rw-r--r--php/step9_interop.php51
-rw-r--r--php/stepA_more.php52
10 files changed, 136 insertions, 151 deletions
diff --git a/php/core.php b/php/core.php
index 16d34f8..39a22ac 100644
--- a/php/core.php
+++ b/php/core.php
@@ -1,6 +1,8 @@
<?php
require_once 'types.php';
+require_once 'readline.php';
+require_once 'reader.php';
require_once 'printer.php';
// Error/Exception functions
@@ -169,11 +171,15 @@ $core_ns = array(
'false?'=> function ($a) { return _false_Q($a); },
'symbol'=> function () { return call_user_func_array('_symbol', func_get_args()); },
'symbol?'=> function ($a) { return _symbol_Q($a); },
+
'string?'=> function ($a) { return _string_Q($a); },
'pr-str'=> function () { return call_user_func_array('pr_str', func_get_args()); },
'str'=> function () { return call_user_func_array('str', func_get_args()); },
'prn'=> function () { return call_user_func_array('prn', func_get_args()); },
'println'=>function () { return call_user_func_array('println', func_get_args()); },
+ 'readline'=>function ($a) { return mal_readline($a); },
+ 'read-string'=>function ($a) { return read_str($a); },
+ 'slurp'=> function ($a) { return file_get_contents($a); },
'<'=> function ($a, $b) { return $a < $b; },
'<='=> function ($a, $b) { return $a <= $b; },
'>'=> function ($a, $b) { return $a > $b; },
diff --git a/php/step2_eval.php b/php/step2_eval.php
index 0ef184a..1cec7f7 100644
--- a/php/step2_eval.php
+++ b/php/step2_eval.php
@@ -55,6 +55,7 @@ function rep($str) {
global $repl_env;
return MAL_PRINT(MAL_EVAL(READ($str), $repl_env));
}
+
$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); };
diff --git a/php/step3_env.php b/php/step3_env.php
index 83ced32..3c46b04 100644
--- a/php/step3_env.php
+++ b/php/step3_env.php
@@ -72,12 +72,11 @@ function rep($str) {
global $repl_env;
return MAL_PRINT(MAL_EVAL(READ($str), $repl_env));
}
-function _ref($k, $v) { global $repl_env; $repl_env->set($k, $v); }
-_ref('+', function ($a, $b) { return intval($a + $b,10); });
-_ref('-', function ($a, $b) { return intval($a - $b,10); });
-_ref('*', function ($a, $b) { return intval($a * $b,10); });
-_ref('/', function ($a, $b) { return intval($a / $b,10); });
+$repl_env->set('+', function ($a, $b) { return intval($a + $b,10); });
+$repl_env->set('-', function ($a, $b) { return intval($a - $b,10); });
+$repl_env->set('*', function ($a, $b) { return intval($a * $b,10); });
+$repl_env->set('/', function ($a, $b) { return intval($a / $b,10); });
do {
try {
diff --git a/php/step4_if_fn_do.php b/php/step4_if_fn_do.php
index 25ca7c5..83734b1 100644
--- a/php/step4_if_fn_do.php
+++ b/php/step4_if_fn_do.php
@@ -90,11 +90,13 @@ function rep($str) {
global $repl_env;
return MAL_PRINT(MAL_EVAL(READ($str), $repl_env));
}
-function _ref($k, $v) { global $repl_env; $repl_env->set($k, $v); }
-// Import core functions
-foreach ($core_ns as $k=>$v) { _ref($k, $v); }
-// Defined using the language itself
+// core.php: defined using PHP
+foreach ($core_ns as $k=>$v) {
+ $repl_env->set($k, _function($v));
+}
+
+// core.mal: defined using the language itself
rep("(def! not (fn* (a) (if a false true)))");
do {
diff --git a/php/step5_tco.php b/php/step5_tco.php
index cd4787e..31e2980 100644
--- a/php/step5_tco.php
+++ b/php/step5_tco.php
@@ -99,14 +99,13 @@ 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); }
-// Defined using the language itself
+// core.mal: defined using the language itself
rep("(def! not (fn* (a) (if a false true)))");
do {
diff --git a/php/step6_file.php b/php/step6_file.php
index 95b3982..0a632c0 100644
--- a/php/step6_file.php
+++ b/php/step6_file.php
@@ -99,22 +99,16 @@ 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) \")\")))))");
@@ -122,21 +116,22 @@ 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);
+
?>
diff --git a/php/step7_quote.php b/php/step7_quote.php
index 8c407c6..8296c1a 100644
--- a/php/step7_quote.php
+++ b/php/step7_quote.php
@@ -122,22 +122,16 @@ 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) \")\")))))");
@@ -145,21 +139,22 @@ 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);
+
?>
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);
+
?>
diff --git a/php/step9_interop.php b/php/step9_interop.php
index 3debdc4..a46864c 100644
--- a/php/step9_interop.php
+++ b/php/step9_interop.php
@@ -149,44 +149,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);
+
?>
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);
+
?>