From debda2e60260ae16f5be1549a59652e4de1039a3 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Sat, 30 May 2020 19:19:37 +0700 Subject: [PATCH] add do --- bootstrap.php | 5 +++++ src/Evaller.php | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/bootstrap.php b/bootstrap.php index 1a30955..8f0e37d 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -22,5 +22,10 @@ function ml_get_lisp(): array return $results[count($results) - 1]; }); + $env->set('print', function (...$args) use ($printer) { + $printer->print($args); + return null; + }); + return [$lisp, $env]; } diff --git a/src/Evaller.php b/src/Evaller.php index 727a3dc..aad14b9 100644 --- a/src/Evaller.php +++ b/src/Evaller.php @@ -60,6 +60,16 @@ class Evaller $value = $this->doEval($ast->get(2), $env); return $env->set($ast->get(1)->getName(), $value); + } 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; } elseif ($ast->get(0)->getName() == 'if') { if ($ast->count() < 3 || $ast->count() > 4) { throw new MadLispException("if requires 2 or 3 arguments");