madlisp/src/Lisp.php

255 lines
6.8 KiB
PHP
Raw Normal View History

2020-05-28 04:55:58 +00:00
<?php
namespace MadLisp;
use Closure;
class Lisp
{
public function tokenize(string $a): array
{
$tokens = [];
$current = '';
$string = false;
$parens = 0;
$addCurrent = function () use (&$tokens, &$current) {
if ($current !== '') {
$tokens[] = $current;
$current = '';
}
};
for ($i = 0; $i < strlen($a); $i++) {
$c = substr($a, $i, 1);
if ($string) {
// Inside string, add all characters
$current .= $c;
// Stop at "
if ($c == '"') {
$addCurrent();
$string = false;
}
} else {
// Not inside string
if ($c == '"') {
// Start of string
$addCurrent();
$current .= $c;
$string = true;
} elseif ($c == ' ' || $c == "\t" || $c == "\n" || $c == "\r") {
// Whitespace is ignored
$addCurrent();
} elseif ($c == '(') {
// Start of list
$addCurrent();
$tokens[] = '(';
$parens++;
} elseif ($c == ')') {
// End of list
if ($parens == 0) {
throw new MadLispException("unexpected closing parenthesis");
}
$addCurrent();
$tokens[] = ')';
$parens--;
} else {
// All other characters
$current .= $c;
}
}
}
// Add last also
$addCurrent();
// Check for errors
if ($parens != 0) {
throw new MadLispException("missing closing parenthesis");
} elseif ($string) {
throw new MadLispException("unterminated string");
}
return $tokens;
}
public function parse(array $tokens): array
{
$result = [];
$index = 0;
while ($index < count($tokens)) {
$result[] = $this->readForm($tokens, $index);
}
return $result;
}
public function read(string $code): array
{
$tokens = $this->tokenize($code);
$expressions = $this->parse($tokens);
return $expressions;
}
public function eval($expr, Env $env)
{
if ($expr instanceof MList && $expr->count() > 0) {
2020-05-28 06:59:36 +00:00
$first = $expr->get(0);
if ($first instanceof Symbol) {
// Special built-in features
if ($first->getName() == 'env') {
return $env;
} elseif ($first->getName() == 'quote') {
if ($expr->count() != 2) {
throw new MadLispException("quote requires exactly 1 argument");
}
2020-05-28 04:55:58 +00:00
2020-05-28 06:59:36 +00:00
return $expr->get(1);
}
2020-05-28 04:55:58 +00:00
2020-05-28 06:59:36 +00:00
// Normal symbol, fetch from env
$first = $env->get($first->getName());
}
if (!($first instanceof Closure)) {
throw new MadLispException("first argument of list is not function");
2020-05-28 04:55:58 +00:00
}
2020-05-28 06:59:36 +00:00
$args = array_slice($expr->getData(), 1);
// Evaluate args
$args = array_map(fn ($a) => $this->eval($a, $env), $args);
// Call func and return result
return $first(...$args);
2020-05-28 04:55:58 +00:00
} elseif ($expr instanceof Hash) {
// Hash: return new hash with all items evaluated
$items = [];
foreach ($expr->getData() as $key => $val) {
$items[$key] = $this->eval($val, $env);
}
return new Hash($items);
} elseif ($expr instanceof Symbol) {
2020-05-28 06:59:36 +00:00
return $env->get($expr->getName());
2020-05-28 04:55:58 +00:00
}
2020-05-28 06:59:36 +00:00
// Return the expression unchanged
2020-05-28 04:55:58 +00:00
return $expr;
}
2020-05-28 07:40:54 +00:00
public function print(array $items): void
2020-05-28 04:55:58 +00:00
{
2020-05-28 07:40:54 +00:00
for ($i = 0; $i < count($items); $i++) {
if ($i > 0) {
print(' ');
}
$this->doPrint($items[$i]);
}
}
public function rep(string $input, Env $env): void
{
$expressions = $this->read($input);
2020-05-28 04:55:58 +00:00
2020-05-28 07:40:54 +00:00
$results = array_map(fn ($expr) => $this->eval($expr, $env), $expressions);
$this->print($results);
}
private function doPrint($a): void
{
2020-05-28 04:55:58 +00:00
if ($a instanceof Closure) {
2020-05-28 07:40:54 +00:00
print('<function>');
2020-05-28 04:55:58 +00:00
} elseif ($a instanceof MList) {
2020-05-28 07:40:54 +00:00
print('(');
for ($i = 0; $i < $a->count(); $i++) {
if ($i > 0) {
print(' ');
}
$this->doPrint($a->get($i));
2020-05-28 04:55:58 +00:00
}
2020-05-28 07:40:54 +00:00
print(')');
2020-05-28 04:55:58 +00:00
} elseif ($a instanceof Hash) {
2020-05-28 07:40:54 +00:00
print('{');
$keys = array_keys($a->getData());
for ($i = 0; $i < count($keys); $i++) {
if ($i > 0) {
print(' ');
}
$this->doPrint($keys[$i]);
print(':');
$this->doPrint($a->get($keys[$i]));
2020-05-28 04:55:58 +00:00
}
2020-05-28 07:40:54 +00:00
print('}');
2020-05-28 04:55:58 +00:00
} elseif ($a instanceof Symbol) {
2020-05-28 07:40:54 +00:00
print($a->getName());
2020-05-28 04:55:58 +00:00
} elseif ($a === true) {
2020-05-28 07:40:54 +00:00
print('true');
2020-05-28 04:55:58 +00:00
} elseif ($a === false) {
2020-05-28 07:40:54 +00:00
print('false');
2020-05-28 04:55:58 +00:00
} elseif ($a === null) {
2020-05-28 07:40:54 +00:00
print('null');
2020-05-28 04:55:58 +00:00
} elseif (is_string($a)) {
2020-05-28 07:40:54 +00:00
print('"' . $a . '"');
} else {
print($a);
2020-05-28 04:55:58 +00:00
}
}
private function readForm(array $tokens, int &$index)
{
if ($tokens[$index] == '(') {
return $this->readList($tokens, $index);
} else {
return $this->readAtom($tokens, $index);
}
}
private function readList(array $tokens, int &$index): MList
{
$result = [];
// start tag
$index++;
while ($tokens[$index] != ')') {
$result[] = $this->readForm($tokens, $index);
}
// end tag
$index++;
return new MList($result);
}
private function readAtom(array $tokens, int &$index)
{
$a = $tokens[$index++];
if ($a === 'true') {
return true;
} elseif ($a === 'false') {
return false;
} elseif ($a === 'null') {
return null;
} elseif (substr($a, 0, 1) === '"') {
// string
return substr($a, 1, strlen($a) - 2);
} elseif (is_numeric($a)) {
if (filter_var($a, FILTER_VALIDATE_INT) !== false) {
return intval($a);
} else {
return floatval($a);
}
} else {
return new Symbol($a);
}
}
}