limes 3.1.0
Composable Calculus Expressions for C++20
Loading...
Searching...
No Matches
to_string.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <sstream>
5#include "nodes/binary.hpp"
6
7namespace limes::expr {
8
9// Free function to_string for any expression node
10// Uses ADL to call the node's to_string() method
11template<typename E>
12 requires is_expr_node_v<E>
13[[nodiscard]] std::string to_string(E const& expr) {
14 return expr.to_string();
15}
16
17// Pretty print an expression (indented format)
18namespace detail {
19
20template<typename E>
21void pretty_print_impl(std::ostream& os, E const& expr, int indent) {
22 std::string padding(indent * 2, ' ');
23 os << padding << expr.to_string();
24}
25
26} // namespace detail
27
28template<typename E>
29 requires is_expr_node_v<E>
30[[nodiscard]] std::string pretty_print(E const& expr) {
31 std::ostringstream oss;
32 detail::pretty_print_impl(oss, expr, 0);
33 return oss.str();
34}
35
36// Stream output operator
37template<typename E>
38 requires is_expr_node_v<E>
39std::ostream& operator<<(std::ostream& os, E const& expr) {
40 return os << expr.to_string();
41}
42
43} // namespace limes::expr
void pretty_print_impl(std::ostream &os, E const &expr, int indent)
Definition to_string.hpp:21
Expression layer for composable calculus.
Definition analysis.hpp:7
std::string to_string(E const &expr)
Definition to_string.hpp:13
std::string pretty_print(E const &expr)
Definition to_string.hpp:30
std::ostream & operator<<(std::ostream &os, E const &expr)
Definition to_string.hpp:39