Skip to content
Snippets Groups Projects
Commit 201e00b0 authored by Adam Wardyński's avatar Adam Wardyński
Browse files

Some fiddling around to_string functino to use stringstream

parent 9531db83
Branches
No related merge requests found
...@@ -9,6 +9,9 @@ ...@@ -9,6 +9,9 @@
#include <libwccl/ops/formatters.h> #include <libwccl/ops/formatters.h>
#include <libwccl/ops/constant.h> #include <libwccl/ops/constant.h>
#include <sstream>
#include <boost/format.hpp>
namespace Wccl { namespace Wccl {
/** /**
...@@ -37,16 +40,7 @@ public: ...@@ -37,16 +40,7 @@ public:
* String representation of conditional operator in form of: * String representation of conditional operator in form of:
* "if cond_expr_string then iftrue_expr_string else iffalse_expr_string" * "if cond_expr_string then iftrue_expr_string else iffalse_expr_string"
*/ */
virtual std::string to_string(const Corpus2::Tagset& tagset) const { virtual std::string to_string(const Corpus2::Tagset& tagset) const;
std::string s(this->operator_name(tagset));
s.append(" ");
s.append(cond_expr_->to_string(tagset));
s.append(" then ");
s.append(iftrue_expr_->to_string(tagset));
s.append(" else ");
s.append(iffalse_expr_->to_string(tagset));
return s;
}
/** /**
* String representation of conditional operator in form of: * String representation of conditional operator in form of:
...@@ -54,16 +48,7 @@ public: ...@@ -54,16 +48,7 @@ public:
* This version does not require tagset, but may be inclomplete * This version does not require tagset, but may be inclomplete
* and/or contain internal info. * and/or contain internal info.
*/ */
virtual std::string to_raw_string() const { virtual std::string to_raw_string() const;
std::string s(this->raw_operator_name());
s.append(" ");
s.append(cond_expr_->to_raw_string());
s.append(" then ");
s.append(iftrue_expr_->to_raw_string());
s.append(" else ");
s.append(iffalse_expr_->to_raw_string());
return s;
}
virtual const std::string raw_operator_name() const { virtual const std::string raw_operator_name() const {
return "if"; return "if";
...@@ -118,14 +103,7 @@ public: ...@@ -118,14 +103,7 @@ public:
* String representation of conditional operator in form of: * String representation of conditional operator in form of:
* "? if_true_expr_string ? cond_expr_string" * "? if_true_expr_string ? cond_expr_string"
*/ */
virtual std::string to_string(const Corpus2::Tagset& tagset) const { virtual std::string to_string(const Corpus2::Tagset& tagset) const;
std::string s(this->operator_name(tagset));
s.append(" ");
s.append(this->iftrue_expr_->to_string(tagset));
s.append(" ? ");
s.append(this->cond_expr_->to_string(tagset));
return s;
}
/** /**
* String representation of conditional operator in form of: * String representation of conditional operator in form of:
...@@ -133,20 +111,59 @@ public: ...@@ -133,20 +111,59 @@ public:
* This version does not require tagset, but may be inclomplete * This version does not require tagset, but may be inclomplete
* and/or contain internal info. * and/or contain internal info.
*/ */
virtual std::string to_raw_string() const { virtual std::string to_raw_string() const;
std::string s(this->raw_operator_name());
s.append(" ");
s.append(this->iftrue_expr_->to_raw_string());
s.append(" ? ");
s.append(this->cond_expr_->to_raw_string());
return s;
}
virtual const std::string raw_operator_name() const { virtual const std::string raw_operator_name() const {
return "?"; return "?";
} }
}; };
template<class T>
std::string Conditional<T>::to_raw_string() const
{
std::stringstream ss;
ss << boost::format("%1% %2% then %3% else %4%")
% this->raw_operator_name()
% this->cond_expr_->to_raw_string()
% this->iftrue_expr_->to_raw_string()
% this->iffalse_expr_->to_raw_string();
return ss.str();
}
template<class T>
std::string Conditional<T>::to_string(const Corpus2::Tagset &tagset) const
{
std::stringstream ss;
ss << boost::format("%1% %2% then %3% else %4%")
% this->operator_name(tagset)
% this->cond_expr_->to_string(tagset)
% this->iftrue_expr_->to_string(tagset)
% this->iffalse_expr_->to_string(tagset);
return ss.str();
}
template<class T>
std::string ConditionalOp<T>::to_raw_string() const
{
std::stringstream ss;
ss << boost::format("%1% %2% ? %3%")
% this->raw_operator_name()
% this->iftrue_expr_->to_raw_string()
% this->cond_expr_->to_raw_string();
return ss.str();
}
template<class T>
std::string ConditionalOp<T>::to_string(const Corpus2::Tagset &tagset) const
{
std::stringstream ss;
ss << boost::format("%1% %2% ? %3%")
% this->operator_name(tagset)
% this->iftrue_expr_->to_string(tagset)
% this->cond_expr_->to_string(tagset);
return ss.str();
}
} /* end ns Wccl */ } /* end ns Wccl */
......
#include <libwccl/ops/formatters.h> #include <libwccl/ops/formatters.h>
#include <sstream>
namespace Wccl { namespace Wccl {
...@@ -10,11 +11,10 @@ std::string UnaryFunctionFormatter::to_raw_string( ...@@ -10,11 +11,10 @@ std::string UnaryFunctionFormatter::to_raw_string(
const char* open_bracket, const char* open_bracket,
const char* close_bracket) const char* close_bracket)
{ {
std::string s(f.raw_operator_name()); std::stringstream ss;
s.append(open_bracket); ss << f.raw_operator_name() << open_bracket << arg_expr.to_raw_string()
s.append(arg_expr.to_raw_string()); << close_bracket;
s.append(close_bracket); return ss.str();
return s;
} }
std::string UnaryFunctionFormatter::to_string( std::string UnaryFunctionFormatter::to_string(
...@@ -24,11 +24,10 @@ std::string UnaryFunctionFormatter::to_string( ...@@ -24,11 +24,10 @@ std::string UnaryFunctionFormatter::to_string(
const char* open_bracket, const char* open_bracket,
const char* close_bracket) const char* close_bracket)
{ {
std::string s(f.operator_name(tagset)); std::stringstream ss;
s.append(open_bracket); ss << f.operator_name(tagset) << open_bracket << arg_expr.to_string(tagset)
s.append(arg_expr.to_string(tagset)); << close_bracket;
s.append(close_bracket); return ss.str();
return s;
} }
// ----- BinaryFunctionFormatter ------ // ----- BinaryFunctionFormatter ------
...@@ -39,13 +38,10 @@ std::string BinaryFunctionFormatter::to_string( ...@@ -39,13 +38,10 @@ std::string BinaryFunctionFormatter::to_string(
const FunctionBase& arg1_expr, const FunctionBase& arg1_expr,
const FunctionBase& arg2_expr) const FunctionBase& arg2_expr)
{ {
std::string s(f.operator_name(tagset)); std::stringstream ss;
s.append("("); ss << f.operator_name(tagset) << "(" << arg1_expr.to_string(tagset)
s.append(arg1_expr.to_string(tagset)); << ", " << arg2_expr.to_string(tagset) << ")";
s.append(", "); return ss.str();
s.append(arg2_expr.to_string(tagset));
s.append(")");
return s;
} }
std::string BinaryFunctionFormatter::to_raw_string( std::string BinaryFunctionFormatter::to_raw_string(
...@@ -53,13 +49,10 @@ std::string BinaryFunctionFormatter::to_raw_string( ...@@ -53,13 +49,10 @@ std::string BinaryFunctionFormatter::to_raw_string(
const FunctionBase& arg1_expr, const FunctionBase& arg1_expr,
const FunctionBase& arg2_expr) const FunctionBase& arg2_expr)
{ {
std::string s(f.raw_operator_name()); std::stringstream ss;
s.append("("); ss << f.raw_operator_name() << "(" << arg1_expr.to_raw_string()
s.append(arg1_expr.to_raw_string()); << ", " << arg2_expr.to_raw_string() << ")";
s.append(", "); return ss.str();
s.append(arg2_expr.to_raw_string());
s.append(")");
return s;
} }
} /* end ns Wccl */ } /* end ns Wccl */
#include <libwccl/ops/logicalpredicate.h> #include <libwccl/ops/logicalpredicate.h>
#include <sstream>
namespace Wccl { namespace Wccl {
std::string LogicalPredicate::to_string(const Corpus2::Tagset& tagset) const std::string LogicalPredicate::to_string(const Corpus2::Tagset& tagset) const
{ {
std::string s(operator_name(tagset)); std::stringstream ss;
s.append("("); ss << operator_name(tagset) << "(";
BoolFunctionPtrVector::const_iterator it = expressions_->begin(); BoolFunctionPtrVector::const_iterator it = expressions_->begin();
while(it != expressions_->end()) { while(it != expressions_->end()) {
s.append((*it)->to_string(tagset)); ss << (*it)->to_string(tagset);
if(++it != expressions_->end()) { if(++it != expressions_->end()) {
s.append(", "); ss << ", ";
} }
} }
s.append(")"); ss << ")";
return s; return ss.str();
} }
std::string LogicalPredicate::to_raw_string() const std::string LogicalPredicate::to_raw_string() const
{ {
std::string s(raw_operator_name()); std::stringstream ss;
s.append("("); ss << raw_operator_name() << "(";
BoolFunctionPtrVector::const_iterator it = expressions_->begin(); BoolFunctionPtrVector::const_iterator it = expressions_->begin();
while(it != expressions_->end()) { while(it != expressions_->end()) {
s.append((*it)->to_raw_string()); ss << (*it)->to_raw_string();
if(++it != expressions_->end()) { if(++it != expressions_->end()) {
s.append(", "); ss << ", ";
} }
} }
s.append(")"); ss << ")";
return s; return ss.str();
} }
} /* end ns Wccl */ } /* end ns Wccl */
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