Commit f0a16e67 authored by Paul's avatar Paul
Browse files

Check if eval can run before computing

parent 17bc98d0
...@@ -72,6 +72,8 @@ struct instruction ...@@ -72,6 +72,8 @@ struct instruction
static void static void
replace(instruction_ref ins, operation o, const shape& r, std::vector<instruction_ref> args); replace(instruction_ref ins, operation o, const shape& r, std::vector<instruction_ref> args);
bool can_eval() const;
argument eval() const; argument eval() const;
void finalize(context& ctx); void finalize(context& ctx);
......
...@@ -162,22 +162,36 @@ void instruction::replace_argument(instruction_ref old, instruction_ref new_ins) ...@@ -162,22 +162,36 @@ void instruction::replace_argument(instruction_ref old, instruction_ref new_ins)
old->remove_output(*this); old->remove_output(*this);
} }
bool instruction::can_eval() const
{
if(op.name() == "@literal")
{
return true;
}
else if (is_context_free(op))
{
return std::all_of(this->inputs().begin(), this->inputs().end(), [](auto arg) {
return arg->can_eval();
});
}
else
{
return false;
}
}
argument instruction::eval() const argument instruction::eval() const
{ {
if(op.name() == "@literal") if(op.name() == "@literal")
{ {
return this->get_literal().get_argument(); return this->get_literal().get_argument();
} }
if(is_context_free(op)) if(is_context_free(op) and this->can_eval())
{ {
std::vector<argument> args; std::vector<argument> args;
for(auto&& arg : this->inputs()) std::transform(this->inputs().begin(), this->inputs().end(), std::back_inserter(args), [](auto arg) {
{ return arg->eval();
argument a = arg->eval(); });
if(a.empty())
return {};
args.push_back(a);
}
return op.compute(result, args); return op.compute(result, args);
} }
return {}; return {};
......
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