madlisp/src/Evaller.php

137 lines
4.3 KiB
PHP
Raw Normal View History

2020-05-28 10:10:00 +00:00
<?php
namespace MadLisp;
use Closure;
class Evaller
{
public function eval(array $expressions, Env $env): array
{
$results = [];
foreach ($expressions as $expr) {
$results[] = $this->doEval($expr, $env);
}
return $results;
}
2020-05-28 12:41:41 +00:00
public function evalAst($ast, Env $env)
2020-05-28 10:10:00 +00:00
{
2020-05-28 12:41:41 +00:00
if ($ast instanceof Symbol) {
// Lookup symbol from env
return $env->get($ast->getName());
} elseif ($ast instanceof MList) {
2020-05-28 13:30:37 +00:00
$results = [];
foreach ($ast->getData() as $val) {
$results[] = $this->doEval($val, $env);
}
2020-05-28 12:41:41 +00:00
return new MList($results);
2020-05-28 13:30:37 +00:00
} elseif ($ast instanceof Hash) {
$results = [];
foreach ($ast->getData() as $key => $val) {
$results[$key] = $this->doEval($val, $env);
}
return new Hash($results);
2020-05-28 12:41:41 +00:00
}
2020-05-28 10:10:00 +00:00
2020-05-28 12:41:41 +00:00
return $ast;
}
2020-05-28 10:10:00 +00:00
2020-05-28 12:41:41 +00:00
public function doEval($ast, Env $env)
{
2020-05-30 11:47:54 +00:00
// Not list or empty list
2020-05-28 12:41:41 +00:00
if (!($ast instanceof MList)) {
return $this->evalAst($ast, $env);
2020-05-30 11:47:54 +00:00
} elseif ($ast->count() == 0) {
2020-05-28 12:41:41 +00:00
return $ast;
}
2020-05-28 10:10:00 +00:00
2020-05-28 13:30:37 +00:00
// Handle special keywords
2020-05-30 11:47:54 +00:00
if ($ast->get(0) instanceof Symbol) {
if ($ast->get(0)->getName() == 'def') {
if ($ast->count() != 3) {
throw new MadLispException("def requires exactly 2 arguments");
}
if (!($ast->get(1) instanceof Symbol)) {
throw new MadLispException("first argument to def is not symbol");
}
$value = $this->doEval($ast->get(2), $env);
return $env->set($ast->get(1)->getName(), $value);
2020-05-30 12:19:37 +00:00
} elseif ($ast->get(0)->getName() == 'do') {
if ($ast->count() < 2) {
throw new MadLispException("do requires at least 1 argument");
}
for ($i = 1; $i < $ast->count(); $i++) {
$value = $this->evalAst($ast->get($i), $env);
}
return $value;
2020-05-30 12:02:41 +00:00
} elseif ($ast->get(0)->getName() == 'if') {
if ($ast->count() < 3 || $ast->count() > 4) {
throw new MadLispException("if requires 2 or 3 arguments");
}
$result = $this->doEval($ast->get(1), $env);
if ($result == true) {
return $this->doEval($ast->get(2), $env);
} elseif ($ast->count() == 4) {
return $this->doEval($ast->get(3), $env);
} else {
return null;
}
2020-05-30 11:47:54 +00:00
} elseif ($ast->get(0)->getName() == 'let') {
if ($ast->count() != 3) {
throw new MadLispException("let requires exactly 2 arguments");
}
if (!($ast->get(1) instanceof MList)) {
throw new MadLispException("first argument to let is not list");
}
$bindings = $ast->get(1)->getData();
if (count($bindings) % 2 == 1) {
throw new MadLispException("uneven number of bindings for let");
}
$newEnv = new Env($env);
for ($i = 0; $i < count($bindings) - 1; $i += 2) {
$key = $bindings[$i];
if (!($key instanceof Symbol)) {
throw new MadLispException("binding key for let is not symbol");
}
$val = $this->doEval($bindings[$i + 1], $newEnv);
$newEnv->set($key->getName(), $val);
}
return $this->doEval($ast->get(2), $newEnv);
} elseif ($ast->get(0)->getName() == 'quote') {
2020-05-28 13:30:37 +00:00
if ($ast->count() != 2) {
throw new MadLispException("quote requires exactly 1 argument");
}
return $ast->get(1);
}
}
2020-05-28 12:41:41 +00:00
// Get new evaluated list
$ast = $this->evalAst($ast, $env);
2020-05-28 10:10:00 +00:00
2020-05-30 11:47:54 +00:00
// Call first argument as function
$func = $ast->get(0);
if (!($func instanceof Closure)) {
2020-05-28 12:41:41 +00:00
throw new MadLispException("first item of list is not function");
2020-05-28 10:10:00 +00:00
}
2020-05-28 12:41:41 +00:00
$args = array_slice($ast->getData(), 1);
2020-05-30 11:47:54 +00:00
return $func(...$args);
2020-05-28 10:10:00 +00:00
}
}