$IN and $OUT special variables.

main
bog 2023-10-15 04:36:02 +02:00
parent 35996484b2
commit 61e4f761c1
2 changed files with 34 additions and 3 deletions

View File

@ -143,8 +143,15 @@ namespace sn
} }
else if (node->type() == NODE_VAR) else if (node->type() == NODE_VAR)
{ {
auto var = *m_sym.get(node->repr()); if (auto var = m_sym.get(node->repr());
return var.values; var)
{
return var->values;
}
else
{
throw run_error {"cannot find variable '" + node->repr() + "'"};
}
} }
else if (node->type() == NODE_ARRAY) else if (node->type() == NODE_ARRAY)
{ {
@ -280,12 +287,16 @@ namespace sn
auto block = *node->get(2); auto block = *node->get(2);
std::vector<std::string> target_values; std::vector<std::string> target_values;
std::vector<std::string> target_values_verbatim;
for (size_t i=0; i<target_node->size(); i++) for (size_t i=0; i<target_node->size(); i++)
{ {
auto t = *target_node->get(i); auto t = *target_node->get(i);
auto values = get_value(t); auto values = get_value(t);
std::copy(std::begin(values), std::end(values),
std::back_inserter(target_values_verbatim));
std::transform(std::begin(values), std::transform(std::begin(values),
std::end(values), std::end(values),
std::back_inserter(target_values), std::back_inserter(target_values),
@ -294,12 +305,16 @@ namespace sn
} }
std::vector<std::string> dep_values; std::vector<std::string> dep_values;
std::vector<std::string> dep_values_verbatim;
for (size_t i=0; i<deps->size(); i++) for (size_t i=0; i<deps->size(); i++)
{ {
auto d = *deps->get(i); auto d = *deps->get(i);
auto values = get_value(d); auto values = get_value(d);
std::copy(std::begin(values), std::end(values),
std::back_inserter(dep_values_verbatim));
std::transform(std::begin(values), std::end(values), std::transform(std::begin(values), std::end(values),
std::back_inserter(dep_values), std::back_inserter(dep_values),
std::bind(&Interpreter::format_path, this, std::bind(&Interpreter::format_path, this,
@ -313,6 +328,8 @@ namespace sn
m_dependencies[target].push_back(dep); m_dependencies[target].push_back(dep);
} }
m_sym.declare("IN", dep_values_verbatim);
m_sym.declare("OUT", target_values_verbatim);
m_scripts[target] = scripts(block); m_scripts[target] = scripts(block);
} }
} break; } break;

View File

@ -79,7 +79,7 @@ public:
void test_contains(std::string const& str, std::vector<std::string> vec) void test_contains(std::string const& str, std::vector<std::string> vec)
{ {
INFO("'" << str << "' not found"); INFO("'" << str << "' not found, got '" << vec[0] << "'");
REQUIRE(std::find(std::begin(vec), std::end(vec), str) REQUIRE(std::find(std::begin(vec), std::end(vec), str)
!= std::end(vec)); != std::end(vec));
} }
@ -319,3 +319,17 @@ TEST_CASE_METHOD(InterpreterTest, "Interpreter_replace_ext")
test_contains("x.h y.h z.d w t.h", res); test_contains("x.h y.h z.d w t.h", res);
} }
} }
TEST_CASE_METHOD(InterpreterTest, "Interpreter_in_out")
{
SECTION("on array")
{
std::stringstream ss;
ss << "x y z -> a b c {" << std::endl;
ss << "$IN $OUT" << std::endl;
ss << "}" << std::endl;
auto res = TEST_RUN(ss.str(), std::filesystem::path("a"));
test_contains("a b c x y z", res);
}
}