[yul-phaser] Add --prefix option

This commit is contained in:
Kamil Śliwak 2020-03-20 17:44:34 +01:00
parent 148b87c977
commit a66ceb11c6
3 changed files with 49 additions and 5 deletions

View File

@ -324,11 +324,14 @@ BOOST_AUTO_TEST_CASE(build_should_load_programs_from_files)
{
TemporaryDirectory tempDir;
vector<string> sources{"{}", "{{}}", "{{{}}}"};
ProgramFactory::Options options{/* inputFiles = */ {
tempDir.memberPath("program1.yul"),
tempDir.memberPath("program2.yul"),
tempDir.memberPath("program3.yul"),
}};
ProgramFactory::Options options{
/* inputFiles = */ {
tempDir.memberPath("program1.yul"),
tempDir.memberPath("program2.yul"),
tempDir.memberPath("program3.yul"),
},
/* prefix = */ "",
};
for (size_t i = 0; i < sources.size(); ++i)
{
@ -346,6 +349,31 @@ BOOST_AUTO_TEST_CASE(build_should_load_programs_from_files)
}
}
BOOST_AUTO_TEST_CASE(build_should_apply_prefix)
{
TemporaryDirectory tempDir;
ProgramFactory::Options options{
/* inputFiles = */ {tempDir.memberPath("program1.yul")},
/* prefix = */ "f",
};
CharStream nestedSource("{{{let x:= 1}}}", "");
Program nestedProgram = get<Program>(Program::load(nestedSource));
Program flatProgram = get<Program>(Program::load(nestedSource));
flatProgram.optimise(Chromosome("f").optimisationSteps());
assert(toString(nestedProgram) != toString(flatProgram));
{
ofstream tmpFile(options.inputFiles[0]);
tmpFile << nestedSource.source() << endl;
}
vector<Program> programs = ProgramFactory::build(options);
BOOST_TEST(programs.size() == 1);
BOOST_TEST(toString(programs[0]) == toString(flatProgram));
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()

View File

@ -285,6 +285,7 @@ ProgramFactory::Options ProgramFactory::Options::fromCommandLine(po::variables_m
{
return {
_arguments["input-files"].as<vector<string>>(),
_arguments["prefix"].as<string>(),
};
}
@ -300,6 +301,8 @@ vector<Program> ProgramFactory::build(Options const& _options)
cerr << get<ErrorList>(programOrErrors) << endl;
assertThrow(false, InvalidProgram, "Failed to load program " + path);
}
get<Program>(programOrErrors).optimise(Chromosome(_options.prefix).optimisationSteps());
inputPrograms.push_back(move(get<Program>(programOrErrors)));
}
@ -348,6 +351,18 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
generalDescription.add_options()
("help", "Show help message and exit.")
("input-files", po::value<vector<string>>()->required()->value_name("<PATH>"), "Input files.")
(
"prefix",
po::value<string>()->value_name("<CHROMOSOME>")->default_value(""),
"Initial optimisation steps automatically applied to every input program.\n"
"The result is treated as if it was the actual input, i.e. the steps are not considered "
"a part of the chromosomes and cannot be mutated. The values of relative metric values "
"are also relative to the fitness of a program with these steps applied rather than the "
"fitness of the original program.\n"
"Note that phaser always adds a 'hgo' prefix to ensure that chromosomes can "
"contain arbitrary optimisation steps. This implicit prefix cannot be changed or "
"or removed using this option. The value given here is applied after it."
)
("seed", po::value<uint32_t>()->value_name("<NUM>"), "Seed for the random number generator.")
(
"rounds",

View File

@ -169,6 +169,7 @@ public:
struct Options
{
std::vector<std::string> inputFiles;
std::string prefix;
static Options fromCommandLine(boost::program_options::variables_map const& _arguments);
};