Commit 254f187e authored by Paul's avatar Paul
Browse files

Validate on eval

parent c4bc243a
......@@ -61,6 +61,18 @@ struct instruction
return std::addressof(i) == std::addressof(*ref);
}
bool valid() const
{
return std::all_of(output.begin(), output.end(), [&](instruction_ref i)
{
return std::find(i->arguments.begin(), i->arguments.end(), *this) != i->arguments.end();
}) &&
std::all_of(arguments.begin(), arguments.end(), [&](instruction_ref i)
{
return std::find(i->output.begin(), i->output.end(), *this) != i->output.end();
});
}
friend bool operator==(instruction_ref ref, const instruction& i) { return i == ref; }
friend bool operator!=(const instruction& i, instruction_ref ref) { return !(i == ref); }
......
......@@ -63,6 +63,8 @@ struct program
instruction_ref begin();
instruction_ref end();
instruction_ref validate() const;
private:
std::unique_ptr<program_impl> impl;
};
......
......@@ -72,8 +72,16 @@ bool program::has_instruction(instruction_ref ins) const
instruction_ref program::begin() { return impl->instructions.begin(); }
instruction_ref program::end() { return impl->instructions.end(); }
instruction_ref program::validate() const
{
return std::find_if(impl->instructions.begin(), impl->instructions.end(), [](const instruction& i) {
return i.valid();
});
}
literal program::eval(std::unordered_map<std::string, argument> params) const
{
assert(this->validate() != impl->instructions.end());
std::unordered_map<const instruction*, argument> results;
argument result;
for(auto& ins : impl->instructions)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment