Last active
March 30, 2017 16:33
-
-
Save deque-blog/39b66305b3c61ce78254dd25e73b6137 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::string print_op_infix(op<add_tag, std::pair<std::string, expression const*>> const& e) | |
{ | |
auto fst = [](auto const& e) { return e.first; }; | |
return boost::algorithm::join(e.rands() | transformed(fst), " + "); | |
} | |
std::string print_op_infix(op<mul_tag, std::pair<std::string, expression const*>> const& e) | |
{ | |
auto wrap_addition = [](auto const& sub_expr) { | |
if (get_as_add(sub_expr.second->get())) | |
return with_parens(sub_expr.first); | |
return sub_expr.first; | |
}; | |
return boost::algorithm::join(e.rands() | transformed(wrap_addition), " * "); | |
} | |
std::string print_infix(expression_r<std::pair<std::string, expression const*>> const& e) | |
{ | |
if (auto* o = get_as_add(e)) return print_op_infix(*o); | |
if (auto* o = get_as_mul(e)) return print_op_infix(*o); | |
if (auto* i = get_as_cst(e)) return std::to_string(*i); | |
if (auto* v = get_as_var(e)) return *v; | |
throw_missing_pattern_matching_clause(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment