small optimization for evaller

This commit is contained in:
Pekka Laiho 2020-12-06 13:50:51 +07:00
parent 304e112f37
commit 16f2e615b1

View File

@ -37,23 +37,28 @@ class Evaller
} }
} }
// Return fast for optimization if not list // Return fast for optimization
if (!($ast instanceof MList)) { // Check two times: before and after macro expansion
return $this->evalAst($ast, $env); for ($check = 0; $check <= 1; $check++) {
} if (!($ast instanceof MList)) {
if ($ast instanceof Symbol || $ast instanceof Collection) {
return $this->evalAst($ast, $env);
} else {
// This is not evaluated so we can just return it.
return $ast;
}
}
// Perform macro expansion // Perform macro expansion on first iteration
$ast = $this->macroexpand($ast, $env); if ($check == 0) {
$ast = $this->macroexpand($ast, $env);
// After macro expansion we have to check for not-a-list again }
if (!($ast instanceof MList)) {
return $this->evalAst($ast, $env);
} }
$astData = $ast->getData(); $astData = $ast->getData();
$astLength = count($astData); $astLength = count($astData);
// Empty list // Empty list, return
if ($astLength == 0) { if ($astLength == 0) {
return $ast; return $ast;
} }