Commit 5f8d091d authored by Paul's avatar Paul
Browse files

Use stream operator

parent 301b84e3
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <rtg/builtin.hpp> #include <rtg/builtin.hpp>
#include <rtg/instruction_ref.hpp> #include <rtg/instruction_ref.hpp>
#include <algorithm> #include <algorithm>
#include <iostream>
namespace rtg { namespace rtg {
...@@ -47,8 +48,7 @@ struct program ...@@ -47,8 +48,7 @@ struct program
literal eval(std::unordered_map<std::string, argument> params) const; literal eval(std::unordered_map<std::string, argument> params) const;
// TODO: Change to stream operator friend std::ostream& operator<<(std::ostream& os, const program& p);
void print() const;
bool has_instruction(instruction_ref ins) const; bool has_instruction(instruction_ref ins) const;
......
...@@ -304,9 +304,9 @@ int main(int argc, char const* argv[]) ...@@ -304,9 +304,9 @@ int main(int argc, char const* argv[])
} }
catch(...) catch(...)
{ {
parser.prog.print(); std::cout << parser.prog << std::endl;
throw; throw;
} }
parser.prog.print(); std::cout << parser.prog << std::endl;
} }
} }
...@@ -87,12 +87,12 @@ literal program::eval(std::unordered_map<std::string, argument> params) const ...@@ -87,12 +87,12 @@ literal program::eval(std::unordered_map<std::string, argument> params) const
return literal{result.get_shape(), result.data()}; return literal{result.get_shape(), result.data()};
} }
void program::print() const std::ostream& operator<<(std::ostream& os, const program& p)
{ {
std::unordered_map<const instruction*, std::string> names; std::unordered_map<const instruction*, std::string> names;
int count = 0; int count = 0;
for(auto& ins : impl->instructions) for(auto& ins : p.impl->instructions)
{ {
std::string var_name = "@" + std::to_string(count); std::string var_name = "@" + std::to_string(count);
if(starts_with(ins.op.name(), "@param")) if(starts_with(ins.op.name(), "@param"))
...@@ -100,16 +100,16 @@ void program::print() const ...@@ -100,16 +100,16 @@ void program::print() const
var_name = ins.op.name().substr(7); var_name = ins.op.name().substr(7);
} }
std::cout << var_name << " = "; os << var_name << " = ";
std::cout << ins.op.name(); os << ins.op.name();
if(ins.op.name() == "@literal") if(ins.op.name() == "@literal")
{ {
if(ins.lit.get_shape().elements() > 10) if(ins.lit.get_shape().elements() > 10)
std::cout << "{ ... }"; os << "{ ... }";
else else
std::cout << "{" << ins.lit << "}"; os << "{" << ins.lit << "}";
} }
if(!ins.arguments.empty()) if(!ins.arguments.empty())
...@@ -117,20 +117,21 @@ void program::print() const ...@@ -117,20 +117,21 @@ void program::print() const
char delim = '('; char delim = '(';
for(auto&& arg : ins.arguments) for(auto&& arg : ins.arguments)
{ {
assert(this->has_instruction(arg) && "Instruction not found"); assert(p.has_instruction(arg) && "Instruction not found");
std::cout << delim << names.at(std::addressof(*arg)); os << delim << names.at(std::addressof(*arg));
delim = ','; delim = ',';
} }
std::cout << ")"; os << ")";
} }
std::cout << " -> " << ins.result; os << " -> " << ins.result;
std::cout << std::endl; os << std::endl;
names.emplace(std::addressof(ins), var_name); names.emplace(std::addressof(ins), var_name);
count++; count++;
} }
return os;
} }
} // namespace rtg } // namespace rtg
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