diff options
Diffstat (limited to 'php')
| -rw-r--r-- | php/Makefile | 2 | ||||
| -rw-r--r-- | php/step9_try.php (renamed from php/step9_interop.php) | 22 | ||||
| -rw-r--r-- | php/stepA_interop.php (renamed from php/stepA_more.php) | 0 | ||||
| -rw-r--r-- | php/tests/stepA_interop.mal | 25 |
4 files changed, 46 insertions, 3 deletions
diff --git a/php/Makefile b/php/Makefile index 9b91421..d9fd2d4 100644 --- a/php/Makefile +++ b/php/Makefile @@ -2,7 +2,7 @@ TESTS = SOURCES_BASE = readline.php types.php reader.php printer.php -SOURCES_LISP = env.php core.php stepA_more.php +SOURCES_LISP = env.php core.php stepA_interop.php SOURCES = $(SOURCES_BASE) $(SOURCES_LISP) .PHONY: stats tests $(TESTS) diff --git a/php/step9_interop.php b/php/step9_try.php index d4a59c7..9343dea 100644 --- a/php/step9_interop.php +++ b/php/step9_try.php @@ -108,8 +108,24 @@ function MAL_EVAL($ast, $env) { return $env->set($ast[1]->value, $func); case "macroexpand": return macroexpand($ast[1], $env); - case "php*": - return eval($ast[1]); + case "try*": + $a1 = $ast[1]; + $a2 = $ast[2]; + if ($a2[0]->value === "catch*") { + try { + return MAL_EVAL($a1, $env); + } catch (Error $e) { + $catch_env = new Env($env, array($a2[1]), + array($e->obj)); + return MAL_EVAL($a2[2], $catch_env); + } catch (Exception $e) { + $catch_env = new Env($env, array($a2[1]), + array($e->getMessage())); + return MAL_EVAL($a2[2], $catch_env); + } + } else { + return MAL_EVAL($a1, $env); + } case "do": eval_ast($ast->slice(1, -1), $env); $ast = $ast[count($ast)-1]; @@ -168,6 +184,7 @@ for ($i=2; $i < count($argv); $i++) { $repl_env->set('*ARGV*', $_argv); // core.mal: defined using the language itself +rep("(def! *host-language* \"php\")"); 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)))))))"); @@ -179,6 +196,7 @@ if (count($argv) > 1) { } // repl loop +rep("(println (str \"Mal [\" *host-language* \"]\"))"); do { try { $line = mal_readline("user> "); diff --git a/php/stepA_more.php b/php/stepA_interop.php index f38656f..f38656f 100644 --- a/php/stepA_more.php +++ b/php/stepA_interop.php diff --git a/php/tests/stepA_interop.mal b/php/tests/stepA_interop.mal new file mode 100644 index 0000000..15f8a94 --- /dev/null +++ b/php/tests/stepA_interop.mal @@ -0,0 +1,25 @@ +;; Testing basic php interop + +(php* "return 7;") +;=>7 + +(php* "return '7';") +;=>"7" + +(php* "return array(7,8,9);") +;=>(7 8 9) + +(php* "return array(\"abc\" => 789);") +;=>{"abc" 789} + +(php* "print \"hello\n\";") +; hello +;=>nil + +(php* "global $foo; $foo=8;") +(php* "global $foo; return $foo;") +;=>8 + +(php* "global $f; $f = function($v) { return 1+$v; };") +(php* "global $f; return array_map($f, array(1,2,3));") +;=>(2 3 4) |
