From c1224fb66f06c34e964dc0b5aa058aafd607f629 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Thu, 28 May 2020 20:30:37 +0700 Subject: [PATCH] quote and eval hash --- src/Evaller.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Evaller.php b/src/Evaller.php index 6851dbd..1fbf2bb 100644 --- a/src/Evaller.php +++ b/src/Evaller.php @@ -22,9 +22,17 @@ class Evaller // Lookup symbol from env return $env->get($ast->getName()); } elseif ($ast instanceof MList) { - // Eval contents and return new list - $results = array_map(fn ($a) => $this->doEval($a, $env), $ast->getData()); + $results = []; + foreach ($ast->getData() as $val) { + $results[] = $this->doEval($val, $env); + } return new MList($results); + } elseif ($ast instanceof Hash) { + $results = []; + foreach ($ast->getData() as $key => $val) { + $results[$key] = $this->doEval($val, $env); + } + return new Hash($results); } return $ast; @@ -42,6 +50,19 @@ class Evaller return $ast; } + $first = $ast->get(0); + + // Handle special keywords + if ($first instanceof Symbol) { + if ($first->getName() == 'quote') { + if ($ast->count() != 2) { + throw new MadLispException("quote requires exactly 1 argument"); + } + + return $ast->get(1); + } + } + // Get new evaluated list $ast = $this->evalAst($ast, $env);