Browse Source

Meta+Env: Fix count on nil value

master
Riyyi 2 years ago
parent
commit
1e76d4599a
  1. 4
      CMakeLists.txt
  2. 22
      src/functions.cpp

4
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})

22
src/functions.cpp

@ -233,20 +233,24 @@ void GlobalEnvironment::isEmpty()
void GlobalEnvironment::count()
{
auto count = [this](std::list<ASTNodePtr> 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<List>(node.get())) {
Error::the().addError(format("wrong argument type: list, '{}'", node));
return nullptr;
}
result = std::static_pointer_cast<List>(node)->size();
if (is<Value>(nodes.front().get()) && std::static_pointer_cast<Value>(nodes.front())->state() == Value::Nil) {
// result = 0
}
else if (!is<List>(nodes.front().get())) {
result = std::static_pointer_cast<List>(nodes.front())->size();
}
else if (!is<Vector>(nodes.front().get())) {
result = std::static_pointer_cast<Vector>(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

Loading…
Cancel
Save