mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 21:35:03 +00:00
50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
namespace MadLisp;
|
|
|
|
class Printer
|
|
{
|
|
public function print($ast, bool $readable = true): void
|
|
{
|
|
print($this->doPrint($ast, $readable));
|
|
}
|
|
|
|
private function doPrint($a, bool $readable): string
|
|
{
|
|
if ($a instanceof Func) {
|
|
return '<function>';
|
|
} elseif ($a instanceof MList) {
|
|
return '(' . implode(' ', array_map(fn ($b) => $this->doPrint($b, $readable), $a->getData())) . ')';
|
|
} elseif ($a instanceof Vector) {
|
|
return '[' . implode(' ', array_map(fn ($b) => $this->doPrint($b, $readable), $a->getData())) . ']';
|
|
} elseif ($a instanceof Hash) {
|
|
return '{' . implode(' ', array_map(fn ($key, $val) => $this->doPrint($key, $readable) . ':' . $this->doPrint($val, $readable),
|
|
array_keys($a->getData()), array_values($a->getData()))) . '}';
|
|
} elseif ($a instanceof Symbol) {
|
|
return $a->getName();
|
|
} elseif (is_object($a)) {
|
|
$class = get_class($a);
|
|
return "<object<$class>>";
|
|
} elseif (is_resource($a)) {
|
|
return '<resource>';
|
|
} elseif ($a === true) {
|
|
return 'true';
|
|
} elseif ($a === false) {
|
|
return 'false';
|
|
} elseif ($a === null) {
|
|
return 'null';
|
|
} elseif (is_string($a)) {
|
|
if ($readable) {
|
|
$a = str_replace("\\", "\\\\", $a);
|
|
$a = str_replace("\n", "\\n", $a);
|
|
$a = str_replace("\r", "\\r", $a);
|
|
$a = str_replace("\"", "\\\"", $a);
|
|
return '"' . $a . '"';
|
|
} else {
|
|
return $a;
|
|
}
|
|
} else {
|
|
return $a;
|
|
}
|
|
}
|
|
}
|