Skip to content

Commit

Permalink
Cleanup configurator
Browse files Browse the repository at this point in the history
Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Jun 2, 2024
1 parent 9ca298f commit e870a17
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 72 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ foreach(EACH IN LISTS ${PROJECT_NAME}_TEST_CPP)
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_${FILENAME})
endforeach()

file(GLOB ${PROJECT_NAME}_TEST_SH ${CMAKE_CURRENT_SOURCE_DIR}/test/*.sh)

foreach(EACH IN LISTS ${PROJECT_NAME}_TEST_SH)
get_filename_component(FILENAME ${EACH} NAME_WE)
add_test(
NAME ${FILENAME}
COMMAND ${${PROJECT_NAME}_MEMORY_CHECK_COMMAND}
${${PROJECT_NAME}_MEMORY_CHECK_OPTIONS}
${EACH}
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/meevax)
endforeach()

# ---- Additional Targets ------------------------------------------------------

execute_process(COMMAND nproc OUTPUT_VARIABLE ${PROJECT_NAME}_NPROC)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Procedures for each standard are provided by the following R7RS-style libraries:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cd build
make package
sudo apt install build/meevax_0.5.190_amd64.deb
sudo apt install build/meevax_0.5.191_amd64.deb
```

or
Expand Down Expand Up @@ -122,9 +122,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|-------------|-------------
| `all` | Build shared-library `libmeevax.0.5.190.so` and executable `meevax`
| `all` | Build shared-library `libmeevax.0.5.191.so` and executable `meevax`
| `test` | Test executable `meevax`
| `package` | Generate debian package `meevax_0.5.190_amd64.deb`
| `package` | Generate debian package `meevax_0.5.191_amd64.deb`
| `install` | Copy files into `/usr/local` directly

## Usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.190
0.5.191
84 changes: 21 additions & 63 deletions include/meevax/kernel/configurator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ inline namespace kernel
{
std::regex const pattern;

std::function<object (std::function<object ()> const&)> build;
std::function<auto (std::function<auto () -> object> const&) -> void> evaluate;

template <typename S, typename F>
explicit option(S&& s, F&& f)
: pattern { std::forward<decltype(s)>(s) }
, build { std::forward<decltype(f)>(f) }
: pattern { std::forward<decltype(s)>(s) }
, evaluate { std::forward<decltype(f)>(f) }
{}
};

Expand All @@ -60,88 +60,59 @@ inline namespace kernel

auto configure(std::vector<std::string> const& args) -> void
{
static auto const pattern = std::regex(R"(--(\w[-\w]+)(=(.*))?|-([\w]+))");
auto const static pattern = std::regex(R"(--(\w[-\w]+)(=(.*))?|-([\w]+))");

auto const options = std::vector<option>
auto const options = std::array<option, 6>
{
option("(i|interactive)", [this](auto)
{
let const f = make<procedure>(__func__, [this](let const&)
{
interactive = true;
return unspecified;
});

return list(f);
interactive = true;
}),

option("(e|evaluate)", [](auto read)
option("(e|evaluate)", [this](auto read)
{
return read();
static_cast<Environment &>(*this).evaluate(read());
}),

option("(h|help)", [](auto)
{
let static const f = make<procedure>(__func__, [](let const&)
{
std::cout << help() << std::endl;
throw EXIT_SUCCESS;
});

return list(f);
std::cout << help() << std::endl;
throw EXIT_SUCCESS;
}),

option("(l|load)", [this](auto read)
{
let const f = make<procedure>(__func__, [this](let const& xs)
{
static_cast<Environment &>(*this).load(car(xs).as<string>());
return unspecified;
});

return list(f, read());
static_cast<Environment &>(*this).load(read().template as<string>());
}),

option("(v|version)", [](auto)
{
let static const f = make<procedure>(__func__, [](let const&)
{
std::cout << version() << std::endl;
throw EXIT_SUCCESS;
});

return list(f);
std::cout << version() << std::endl;
throw EXIT_SUCCESS;
}),

option("(w|write)", [](auto read)
{
let static const f = make<procedure>(__func__, [](let const& xs)
{
std::cout << car(xs) << std::endl;
});

return list(f, read());
std::cout << read() << std::endl;
}),
};

auto search = [&](auto&& name) -> decltype(auto)
auto evaluator = [&](auto&& name) -> decltype(auto)
{
if (auto iter = std::find_if(options.begin(), options.end(), [&](auto&& option)
{
return std::regex_match(name, option.pattern);
});
iter != options.end())
{
return *iter;
return iter->evaluate;
}
else
{
throw error(make<string>("unknown option"), make<symbol>(name));
}
};

std::vector<object> expressions {};

for (auto iter = std::next(args.begin()); iter != args.end(); ++iter)
{
static std::regex const pattern { R"(--(\w[-\w]+)(?:=(.*))?|-([\w]+))" };
Expand All @@ -165,39 +136,26 @@ inline namespace kernel
{
for (auto str3 = result.str(3); not str3.empty(); str3.erase(0, 1))
{
expressions.push_back(search(str3.substr(0, 1)).build(read));
std::invoke(evaluator(str3.substr(0, 1)), read);
}
}
else if (result.length(2))
{
auto read = [result]()
std::invoke(evaluator(result.str(1)), [result]()
{
return input_string_port(result.str(2)).read();
};

expressions.push_back(search(result.str(1)).build(read));
});
}
else if (result.length(1))
{
expressions.push_back(search(result.str(1)).build(read));
std::invoke(evaluator(result.str(1)), read);
}
}
else
{
let const f = make<procedure>(__func__, [iter](let const&)
{
Environment().load(*iter);
return unspecified;
});

expressions.push_back(list(f));
Environment().load(*iter);
}
}

for (let const& expression : expressions)
{
static_cast<Environment &>(*this).evaluate(expression);
}
}
};
} // namespace kernel
Expand Down
4 changes: 2 additions & 2 deletions include/meevax/kernel/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ inline namespace kernel

std::vector<detail> details {};

mutable std::string brief {};
mutable std::string explanation {};

using pair::pair;

Expand Down Expand Up @@ -108,7 +108,7 @@ inline namespace kernel
}
catch (std::exception const& exception)
{
std::cerr << error(make<string>(exception.what())) << std::endl;
error(make<string>(exception.what())).report(std::cerr);
return EXIT_FAILURE;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/kernel/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ inline namespace kernel
{
try
{
if (brief.empty())
if (explanation.empty())
{
auto output = std::stringstream();

Expand All @@ -64,10 +64,10 @@ inline namespace kernel
output << ": " << irritants();
}

brief = output.str();
explanation = output.str();
}

return brief.c_str();
return explanation.c_str();
}
catch (...)
{
Expand Down
8 changes: 8 additions & 0 deletions test/option.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

meevax=$1

$meevax -e '(import (scheme base))' -e '(+ 1 2 3)'
$meevax -h
$meevax -v
$meevax -w '(+ 1 2 3)'

0 comments on commit e870a17

Please sign in to comment.