From 311d07454653ea3200849930df9af01239950fcc Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Thu, 28 May 2020 16:49:32 +0700 Subject: [PATCH] add if --- src/Lisp.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Lisp.php b/src/Lisp.php index 8c4b931..ee51467 100644 --- a/src/Lisp.php +++ b/src/Lisp.php @@ -111,6 +111,20 @@ class Lisp } return $expr->get(1); + } elseif ($first->getName() == 'if') { + if ($expr->count() != 4) { + throw new MadLispException("if requires exactly 3 arguments"); + } + + // Eval condition + $result = $this->eval($expr->get(1), $env); + + // Eval true or false branch and return it + if ($result == true) { + return $this->eval($expr->get(2), $env); + } else { + return $this->eval($expr->get(3), $env); + } } // Normal symbol, fetch from env