Browse Source

Meta+Env: Fix count on nil value

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

4
CMakeLists.txt

@ -95,3 +95,7 @@ add_dependencies(test2 ${PROJECT})
add_custom_target(test3 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}) 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_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})

20
src/functions.cpp

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

Loading…
Cancel
Save