From 1e76d4599a13b7003a8857728805b3de495f6897 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Tue, 28 Mar 2023 22:51:28 +0200 Subject: [PATCH] Meta+Env: Fix count on nil value --- CMakeLists.txt | 4 ++++ src/functions.cpp | 22 +++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a6fd20..cf5cc68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,3 +95,7 @@ add_dependencies(test2 ${PROJECT}) add_custom_target(test3 COMMAND env STEP=step_env MAL_IMPL=js ../vendor/mal/runtest.py --deferrable --optional ../vendor/mal/tests/step3_env.mal -- ./${PROJECT}) add_dependencies(test3 ${PROJECT}) + +add_custom_target(test4 + COMMAND env STEP=step_env MAL_IMPL=js ../vendor/mal/runtest.py --deferrable --optional ../vendor/mal/tests/step4_if_fn_do.mal -- ./${PROJECT}) +add_dependencies(test4 ${PROJECT}) diff --git a/src/functions.cpp b/src/functions.cpp index c7c97ab..ec8fb55 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -233,20 +233,24 @@ void GlobalEnvironment::isEmpty() void GlobalEnvironment::count() { auto count = [this](std::list nodes) -> ASTNodePtr { - if (nodes.size() > 1) { + if (nodes.size() != 1) { Error::the().addError(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); return nullptr; } size_t result = 0; - - for (auto node : nodes) { - if (!is(node.get())) { - Error::the().addError(format("wrong argument type: list, '{}'", node)); - return nullptr; - } - - result = std::static_pointer_cast(node)->size(); + if (is(nodes.front().get()) && std::static_pointer_cast(nodes.front())->state() == Value::Nil) { + // result = 0 + } + else if (!is(nodes.front().get())) { + result = std::static_pointer_cast(nodes.front())->size(); + } + else if (!is(nodes.front().get())) { + result = std::static_pointer_cast(nodes.front())->size(); + } + else { + Error::the().addError(format("wrong argument type: list, '{}'", nodes)); + return nullptr; } // FIXME: Add numeric_limits check for implicit cast: size_t > int64_t