aboutsummaryrefslogtreecommitdiff
path: root/php/printer.php
diff options
context:
space:
mode:
Diffstat (limited to 'php/printer.php')
-rw-r--r--php/printer.php53
1 files changed, 53 insertions, 0 deletions
diff --git a/php/printer.php b/php/printer.php
new file mode 100644
index 0000000..3839931
--- /dev/null
+++ b/php/printer.php
@@ -0,0 +1,53 @@
+<?php
+
+require_once 'types.php';
+
+function _pr_str($obj, $print_readably=True) {
+ if (_list_Q($obj)) {
+ $ret = array();
+ foreach ($obj as $e) {
+ array_push($ret, _pr_str($e, $print_readably));
+ }
+ return "(" . implode(" ", $ret) . ")";
+ } elseif (_vector_Q($obj)) {
+ $ret = array();
+ foreach ($obj as $e) {
+ array_push($ret, _pr_str($e, $print_readably));
+ }
+ return "[" . implode(" ", $ret) . "]";
+ } elseif (_hash_map_Q($obj)) {
+ $ret = array();
+ foreach (array_keys($obj->getArrayCopy()) as $k) {
+ $ret[] = _pr_str($k, $print_readably);
+ $ret[] = _pr_str($obj[$k], $print_readably);
+ }
+ return "{" . implode(" ", $ret) . "}";
+ } elseif (is_string($obj)) {
+ if ($print_readably) {
+ $obj = preg_replace('/"/', '\\"', preg_replace('/\\\\/', '\\\\\\\\', $obj));
+ return '"' . $obj . '"';
+ } else {
+ return $obj;
+ }
+ } elseif (is_integer($obj)) {
+ return $obj;
+ } elseif ($obj === NULL) {
+ return "nil";
+ } elseif ($obj === true) {
+ return "true";
+ } elseif ($obj === false) {
+ return "false";
+ } elseif (_symbol_Q($obj)) {
+ return $obj->value;
+ } elseif (_atom_Q($obj)) {
+ return "(atom " . _pr_str($obj->value, $print_readably) . ")";
+ } elseif (_function_Q($obj)) {
+ return "(fn* [...] ...)";
+ } elseif (is_callable($obj)) { // only step4 and below
+ return "#<function ...>";
+ } else {
+ throw new Exception("_pr_str unknown type: " . gettype($obj));
+ }
+}
+
+?>