madlisp/lisp.php

196 lines
4.5 KiB
PHP
Raw Normal View History

2020-05-26 08:43:59 +00:00
<?php
2020-05-27 09:27:47 +00:00
require_once('classes.php');
2020-05-26 08:43:59 +00:00
function ml_tokenize(string $a): array
{
$tokens = [];
$current = '';
$string = false;
$parens = 0;
2020-05-27 11:54:39 +00:00
$addCurrent = function () use (&$tokens, &$current) {
if ($current !== '') {
$tokens[] = $current;
2020-05-26 08:43:59 +00:00
$current = '';
}
};
for ($i = 0; $i < strlen($a); $i++) {
$c = substr($a, $i, 1);
2020-05-27 11:54:39 +00:00
if ($string) {
// Inside string, add all characters
$current .= $c;
// Stop at "
if ($c == '"') {
$addCurrent();
2020-05-26 08:43:59 +00:00
$string = false;
}
2020-05-27 11:54:39 +00:00
} else {
// Not inside string
if ($c == '"') {
// Start of string
$addCurrent();
2020-05-26 08:43:59 +00:00
$current .= $c;
2020-05-27 11:54:39 +00:00
$string = true;
} elseif ($c == ' ' || $c == "\t" || $c == "\n" || $c == "\r") {
// Whitespace is ignored
2020-05-26 08:43:59 +00:00
$addCurrent();
2020-05-27 11:54:39 +00:00
} 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;
2020-05-26 08:43:59 +00:00
}
}
}
// Add last also
$addCurrent();
// Check for errors
2020-05-27 09:27:47 +00:00
if ($parens != 0) {
2020-05-26 08:43:59 +00:00
throw new MadLispException("missing closing parenthesis");
} elseif ($string) {
throw new MadLispException("unterminated string");
}
return $tokens;
}
2020-05-27 09:27:47 +00:00
function ml_read_form(array $tokens, int &$index)
{
2020-05-27 11:54:39 +00:00
if ($tokens[$index] == '(') {
2020-05-27 09:27:47 +00:00
return ml_read_list($tokens, $index);
} else {
return ml_read_atom($tokens, $index);
}
}
2020-05-27 11:54:39 +00:00
function ml_read_list(array $tokens, int &$index): MLList
2020-05-26 08:43:59 +00:00
{
$result = [];
2020-05-27 09:27:47 +00:00
// start tag
$index++;
2020-05-26 08:43:59 +00:00
2020-05-27 11:54:39 +00:00
while ($tokens[$index] != ')') {
2020-05-27 09:27:47 +00:00
$result[] = ml_read_form($tokens, $index);
2020-05-26 08:43:59 +00:00
}
2020-05-27 09:27:47 +00:00
// end tag
$index++;
2020-05-27 11:54:39 +00:00
return new MLList($result);
2020-05-26 08:43:59 +00:00
}
2020-05-27 09:27:47 +00:00
function ml_read_atom(array $tokens, int &$index)
2020-05-26 08:43:59 +00:00
{
2020-05-27 09:27:47 +00:00
$a = $tokens[$index++];
2020-05-27 11:54:39 +00:00
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);
2020-05-27 09:27:47 +00:00
} else {
2020-05-27 11:54:39 +00:00
return floatval($a);
2020-05-27 09:27:47 +00:00
}
} else {
2020-05-27 11:54:39 +00:00
return new MLSymbol($a);
2020-05-27 09:27:47 +00:00
}
}
2020-05-26 08:43:59 +00:00
2020-05-27 09:27:47 +00:00
function ml_parse(array $tokens): array
{
$result = [];
2020-05-26 08:43:59 +00:00
$index = 0;
2020-05-27 09:27:47 +00:00
while ($index < count($tokens)) {
$result[] = ml_read_form($tokens, $index);
}
return $result;
}
function ml_read(string $code): array
{
$tokens = ml_tokenize($code);
$expressions = ml_parse($tokens);
2020-05-26 08:43:59 +00:00
return $expressions;
}
2020-05-27 11:54:39 +00:00
function ml_eval($expr, MLEnv $env)
2020-05-26 08:43:59 +00:00
{
2020-05-27 11:54:39 +00:00
if ($expr instanceof MLList && $expr->count() > 0) {
// Evaluate list contents
$results = array_map(fn ($a) => ml_eval($a, $env), $expr->getData());
if ($results[0] instanceof Closure) {
// If the first item is a function, call it
$args = array_slice($results, 1);
return ($results[0])(...$args);
} else {
// Otherwise return new list with evaluated contents
return new MLList($results);
2020-05-27 09:27:47 +00:00
}
2020-05-27 11:54:39 +00:00
} elseif ($expr instanceof MLSymbol) {
return $env->get($expr->name());
2020-05-27 09:27:47 +00:00
}
return $expr;
2020-05-26 08:43:59 +00:00
}
2020-05-27 09:27:47 +00:00
function ml_print($a): string
2020-05-26 08:43:59 +00:00
{
2020-05-27 09:27:47 +00:00
if ($a instanceof Closure) {
return '<function>';
2020-05-27 11:54:39 +00:00
} elseif ($a instanceof MLList) {
return '(' . implode(' ', array_map('ml_print', $a->getData())) . ')';
} elseif ($a instanceof MLSymbol) {
return $a->name();
2020-05-27 09:27:47 +00:00
} elseif ($a === true) {
return 'true';
} elseif ($a === false) {
return 'false';
} elseif ($a === null) {
return 'null';
} elseif (is_string($a)) {
return '"' . $a . '"';
} else {
return $a;
}
2020-05-26 08:43:59 +00:00
}
2020-05-27 11:54:39 +00:00
function ml_rep(string $input, MLEnv $env): string
2020-05-26 08:43:59 +00:00
{
2020-05-27 09:27:47 +00:00
$expressions = ml_read($input);
$results = array_map(fn ($expr) => ml_eval($expr, $env), $expressions);
return implode(" ", array_map('ml_print', $results));
2020-05-26 08:43:59 +00:00
}