madlisp/lisp.php

216 lines
4.9 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
2020-05-27 09:27:47 +00:00
// types
2020-05-26 08:43:59 +00:00
define("SYMBOL", "SYMBOL");
define("STRING", "STRING");
define("NUMBER", "NUMBER");
define("START", "START");
define("END", "END");
2020-05-27 09:27:47 +00:00
// special sign for symbols
define("MAGIC", "§");
2020-05-26 08:43:59 +00:00
function ml_tokenize(string $a): array
{
$tokens = [];
$current = '';
$string = false;
$parens = 0;
2020-05-27 09:27:47 +00:00
$addCurrent = function ($string = false) use (&$tokens, &$current) {
if ($current !== '' || $string) {
if ($string) {
$tokens[] = [STRING, $current];
} elseif ($current == 'true') {
$tokens[] = [true];
} elseif ($current == 'false') {
$tokens[] = [false];
} elseif ($current == 'null') {
$tokens[] = [null];
} elseif (is_numeric($current)) {
$tokens[] = [NUMBER, $current];
} else {
$tokens[] = [SYMBOL, $current];
}
2020-05-26 08:43:59 +00:00
$current = '';
}
};
for ($i = 0; $i < strlen($a); $i++) {
$c = substr($a, $i, 1);
if ($c == '"') {
if ($string) {
// End of string
$addCurrent(true);
$string = false;
} else {
// Start of string
$string = true;
}
} elseif ($c == ' ' || $c == "\t" || $c == "\n" || $c == "\r") {
if ($string) {
// Include whitespace only inside strings
$current .= $c;
} else {
$addCurrent();
}
} elseif ($c == '(') {
$addCurrent();
$tokens[] = [START];
$parens++;
} elseif ($c == ')') {
if ($parens == 0) {
throw new MadLispException("unexpected closing parenthesis");
}
$addCurrent();
$tokens[] = [END];
$parens--;
} else {
// All other characters are included normally
$current .= $c;
}
}
// 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)
{
$a = $tokens[$index];
if ($a[0] == START) {
return ml_read_list($tokens, $index);
} else {
return ml_read_atom($tokens, $index);
}
}
function ml_read_list(array $tokens, int &$index): array
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 09:27:47 +00:00
while ($tokens[$index][0] != END) {
$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-26 08:43:59 +00:00
return $result;
}
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++];
if ($a[0] == STRING) {
return $a[1];
} elseif ($a[0] == SYMBOL) {
return MAGIC . $a[1];
} elseif ($a[0] == NUMBER) {
if (filter_var($a[1], FILTER_VALIDATE_INT) !== false) {
return intval($a[1]);
} else {
return floatval($a[1]);
}
} else {
return $a[0];
}
}
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_is_symbol($a)
{
return substr($a, 0, strlen(MAGIC)) === MAGIC;
}
function ml_strip_symbol($a)
{
return substr($a, strlen(MAGIC));
}
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 09:27:47 +00:00
function ml_eval($expr, Env $env)
2020-05-26 08:43:59 +00:00
{
2020-05-27 09:27:47 +00:00
if (is_array($expr)) {
// Evaluate list items
$expr = array_map(fn ($a) => ml_eval($a, $env), $expr);
// If the first item is a function, call it
$fn = $expr[0] ?? null;
if ($fn && $fn instanceof Closure) {
$args = array_slice($expr, 1);
return $fn(...$args);
}
} elseif (ml_is_symbol($expr)) {
return $env->get(ml_strip_symbol($expr));
}
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>';
} elseif (is_array($a)) {
return '(' . implode(' ', array_map('ml_print', $a)) . ')';
} elseif ($a === true) {
return 'true';
} elseif ($a === false) {
return 'false';
} elseif ($a === null) {
return 'null';
} elseif (ml_is_symbol($a)) {
return ml_strip_symbol($a);
} elseif (is_string($a)) {
return '"' . $a . '"';
} else {
return $a;
}
2020-05-26 08:43:59 +00:00
}
2020-05-27 09:27:47 +00:00
function ml_rep(string $input, Env $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
}