madlisp/src/Printer.php

39 lines
1.2 KiB
PHP
Raw Normal View History

2020-05-28 10:10:00 +00:00
<?php
namespace MadLisp;
class Printer
{
public function print(array $items): void
{
2020-05-31 04:34:24 +00:00
$strings = array_map(fn ($a) => $this->doPrint($a), $items);
print(implode(' ', $strings));
2020-05-28 10:10:00 +00:00
}
2020-05-31 04:34:24 +00:00
private function doPrint($a): string
2020-05-28 10:10:00 +00:00
{
if ($a instanceof Func) {
2020-05-31 04:34:24 +00:00
return '<function>';
2020-05-28 10:10:00 +00:00
} elseif ($a instanceof MList) {
2020-05-31 04:34:24 +00:00
return '(' . implode(' ', array_map(fn ($b) => $this->doPrint($b), $a->getData())) . ')';
} elseif ($a instanceof Vector) {
return '[' . implode(' ', array_map(fn ($b) => $this->doPrint($b), $a->getData())) . ']';
2020-05-28 10:10:00 +00:00
} elseif ($a instanceof Hash) {
2020-05-31 04:34:24 +00:00
return '{' . implode(' ', array_map(fn ($key, $val) => $this->doPrint($key) . ':' . $this->doPrint($val),
array_keys($a->getData()), array_values($a->getData()))) . '}';
2020-05-28 10:10:00 +00:00
} elseif ($a instanceof Symbol) {
2020-05-31 04:34:24 +00:00
return $a->getName();
2020-05-28 10:10:00 +00:00
} elseif ($a === true) {
2020-05-31 04:34:24 +00:00
return 'true';
2020-05-28 10:10:00 +00:00
} elseif ($a === false) {
2020-05-31 04:34:24 +00:00
return 'false';
2020-05-28 10:10:00 +00:00
} elseif ($a === null) {
2020-05-31 04:34:24 +00:00
return 'null';
2020-05-28 10:10:00 +00:00
} elseif (is_string($a)) {
2020-05-31 04:34:24 +00:00
return '"' . $a . '"';
2020-05-28 10:10:00 +00:00
} else {
2020-05-31 04:34:24 +00:00
return $a;
2020-05-28 10:10:00 +00:00
}
}
}