From 2f6409ce7282fb63d65ac1a1c7bc005da612c77b Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Thu, 4 Jun 2020 18:00:30 +0700 Subject: [PATCH] special forms and, or --- src/Evaller.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Evaller.php b/src/Evaller.php index f0194cb..1cc7fa8 100644 --- a/src/Evaller.php +++ b/src/Evaller.php @@ -14,7 +14,20 @@ class Evaller // Handle special forms if ($ast->get(0) instanceof Symbol) { - if ($ast->get(0)->getName() == 'def') { + if ($ast->get(0)->getName() == 'and') { + if ($ast->count() == 1) { + return true; + } + + for ($i = 1; $i < $ast->count(); $i++) { + $value = $this->eval($ast->get($i), $env); + if ($value == false) { + return $value; + } + } + + return $value; + } elseif ($ast->get(0)->getName() == 'def') { if ($ast->count() != 3) { throw new MadLispException("def requires exactly 2 arguments"); } @@ -113,6 +126,19 @@ class Evaller } return $this->eval($ast->get(2), $newEnv); + } elseif ($ast->get(0)->getName() == 'or') { + if ($ast->count() == 1) { + return false; + } + + for ($i = 1; $i < $ast->count(); $i++) { + $value = $this->eval($ast->get($i), $env); + if ($value == true) { + return $value; + } + } + + return $value; } elseif ($ast->get(0)->getName() == 'quote') { if ($ast->count() != 2) { throw new MadLispException("quote requires exactly 1 argument");