From c0587aa4fb5ed70e07c0b795448f8652cc3af106 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sat, 19 Jul 2025 11:47:03 -0500 Subject: [PATCH] pretty printing -- copmlete for xo::ast::GeneralizedExpression --- include/xo/reader/envframestack.hpp | 7 ++- include/xo/reader/exprstate.hpp | 2 +- include/xo/reader/exprstatestack.hpp | 20 +++------ include/xo/reader/parserstatemachine.hpp | 9 ---- include/xo/reader/pretty_envframestack.hpp | 20 +++++++++ include/xo/reader/pretty_exprstatestack.hpp | 20 +++++++++ .../xo/reader/pretty_parserstatemachine.hpp | 18 ++++++++ src/reader/CMakeLists.txt | 6 ++- src/reader/envframestack.cpp | 41 +++++++++++++++++ src/reader/exprstate.cpp | 1 + src/reader/exprstatestack.cpp | 43 ++++++++++++++++++ src/reader/parserstatemachine.cpp | 3 ++ src/reader/pretty_parserstatemachine.cpp | 44 +++++++++++++++++++ src/reader/progress_xs.cpp | 1 + 14 files changed, 206 insertions(+), 29 deletions(-) create mode 100644 include/xo/reader/pretty_envframestack.hpp create mode 100644 include/xo/reader/pretty_exprstatestack.hpp create mode 100644 include/xo/reader/pretty_parserstatemachine.hpp create mode 100644 src/reader/pretty_parserstatemachine.cpp diff --git a/include/xo/reader/envframestack.hpp b/include/xo/reader/envframestack.hpp index 8084d584..2ec256fe 100644 --- a/include/xo/reader/envframestack.hpp +++ b/include/xo/reader/envframestack.hpp @@ -14,8 +14,10 @@ namespace xo { **/ class envframestack { public: - using LocalEnv = xo::ast::LocalEnv; - using Variable = xo::ast::Variable; + using LocalEnv = xo::ast::LocalEnv; + using Variable = xo::ast::Variable; + using ppstate = xo::print::ppstate; + using ppindentinfo = xo::print::ppindentinfo; public: envframestack() {} @@ -58,6 +60,7 @@ namespace xo { } void print (std::ostream & os) const; + bool pretty_print(const ppindentinfo & ppii) const; private: std::vector> stack_; diff --git a/include/xo/reader/exprstate.hpp b/include/xo/reader/exprstate.hpp index 83b459c8..0d9b7678 100644 --- a/include/xo/reader/exprstate.hpp +++ b/include/xo/reader/exprstate.hpp @@ -76,7 +76,7 @@ namespace xo { } class parserstatemachine; /* see parserstatemachine.hpp */ - class exprstatestack; + class exprstatestack; /* see exprstatestack.hpp */ class formal_arg; diff --git a/include/xo/reader/exprstatestack.hpp b/include/xo/reader/exprstatestack.hpp index ce7bdc1d..e8f51d45 100644 --- a/include/xo/reader/exprstatestack.hpp +++ b/include/xo/reader/exprstatestack.hpp @@ -15,6 +15,10 @@ namespace xo { * @brief A stack of exprstate objects **/ class exprstatestack { + public: + using ppstate = xo::print::ppstate; + using ppindentinfo = xo::print::ppindentinfo; + public: exprstatestack() {} @@ -45,6 +49,7 @@ namespace xo { } void print (std::ostream & os) const; + bool pretty_print(const ppindentinfo & ppii) const; private: std::vector> stack_; @@ -65,21 +70,6 @@ namespace xo { return os; } } /*namespace scm*/ - -#ifndef ppdetail_atomic - namespace print { - template <> - struct ppdetail { - static bool print_upto(ppstate * pps, const xo::scm::exprstatestack * x) { - return ppdetail_atomic::print_upto(pps, x); - } - static void print_pretty(ppstate * pps, const xo::scm::exprstatestack * x) { - ppdetail_atomic::print_pretty(pps, x); - } - }; - } -#endif - } /*namespace xo*/ diff --git a/include/xo/reader/parserstatemachine.hpp b/include/xo/reader/parserstatemachine.hpp index decc1e6c..490ac35c 100644 --- a/include/xo/reader/parserstatemachine.hpp +++ b/include/xo/reader/parserstatemachine.hpp @@ -87,13 +87,4 @@ namespace xo { return os; } } /*namespace scm*/ - -#ifndef ppdetail_atomic - namespace print { - PPDETAIL_ATOMIC(xo::scm::parserstatemachine); - } -#endif } /*namespace xo*/ - - -/* end parserstatemachine.hpp */ diff --git a/include/xo/reader/pretty_envframestack.hpp b/include/xo/reader/pretty_envframestack.hpp new file mode 100644 index 00000000..043b0eb2 --- /dev/null +++ b/include/xo/reader/pretty_envframestack.hpp @@ -0,0 +1,20 @@ +/* file pretty_envframestack.hpp + * + * author: Roland Conybeare, Jul 2025 + */ + +#pragma once + +#include "envframestack.hpp" +#include "xo/indentlog/print/pretty.hpp" + +namespace xo { + namespace print { + template <> + struct ppdetail { + static inline bool print_pretty(const ppindentinfo & ppii, const xo::scm::envframestack * p) { + return p->pretty_print(ppii); + } + }; + } /*namespace print*/ +} /*namespace xo*/ diff --git a/include/xo/reader/pretty_exprstatestack.hpp b/include/xo/reader/pretty_exprstatestack.hpp new file mode 100644 index 00000000..4c2b04f1 --- /dev/null +++ b/include/xo/reader/pretty_exprstatestack.hpp @@ -0,0 +1,20 @@ +/* file pretty_exprstatestack.hpp + * + * author: Roland Conybeare, Jul 2025 + */ + +#pragma once + +#include "exprstatestack.hpp" +#include "xo/indentlog/print/pretty.hpp" + +namespace xo { + namespace print { + template <> + struct ppdetail { + static inline bool print_pretty(const ppindentinfo & ppii, const xo::scm::exprstatestack * p) { + return p->pretty_print(ppii); + } + }; + } /*namespace print*/ +} /*namespace xo*/ diff --git a/include/xo/reader/pretty_parserstatemachine.hpp b/include/xo/reader/pretty_parserstatemachine.hpp new file mode 100644 index 00000000..512a6844 --- /dev/null +++ b/include/xo/reader/pretty_parserstatemachine.hpp @@ -0,0 +1,18 @@ +/* file pretty_parserstatemachine.hpp + * + * author: Roland Conybeare, Jul 2025 + */ + +#pragma once + +#include "parserstatemachine.hpp" +#include "xo/indentlog/print/pretty.hpp" + +namespace xo { + namespace print { + template<> + struct ppdetail { + static bool print_pretty(const ppindentinfo & ppii, const xo::scm::parserstatemachine & x); + }; + } +} /*namespace xo*/ diff --git a/src/reader/CMakeLists.txt b/src/reader/CMakeLists.txt index 785855ec..9024a19a 100644 --- a/src/reader/CMakeLists.txt +++ b/src/reader/CMakeLists.txt @@ -1,4 +1,4 @@ -# parser/CMakeLists.txt +# reader/CMakeLists.txt set(SELF_LIB xo_reader) set(SELF_SRCS @@ -19,7 +19,9 @@ set(SELF_SRCS expect_type_xs.cpp lambda_xs.cpp let1_xs.cpp - envframestack.cpp) + envframestack.cpp + pretty_parserstatemachine.cpp +) xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS}) xo_dependency(${SELF_LIB} xo_expression) diff --git a/src/reader/envframestack.cpp b/src/reader/envframestack.cpp index 6b27a26d..97bca077 100644 --- a/src/reader/envframestack.cpp +++ b/src/reader/envframestack.cpp @@ -4,6 +4,8 @@ */ #include "envframestack.hpp" +#include "pretty_envframestack.hpp" +#include "pretty_localenv.hpp" namespace xo { using xo::ast::LocalEnv; @@ -92,6 +94,45 @@ namespace xo { os << ">" << std::endl; } + + bool + envframestack::pretty_print(const ppindentinfo & ppii) const + { + ppstate * pps = ppii.pps(); + + if (ppii.upto()) { + if (stack_.size() > 1) + return false; + + if (!pps->print_upto("print_upto_tag("size", stack_.size())) + return false; + + if ((stack_.size() > 0) + && !pps->print_upto_tag("[0]", stack_[0])) + { + return false; + } + + pps->write(">"); + + return true; + } else { + pps->write("newline_pretty_tag(ppii.ci1(), "size", stack_.size()); + + for (std::size_t i = 0, z = stack_.size(); i < z; ++i) { + std::string i_str = tostr("[", z-i-1, "]"); + pps->newline_pretty_tag(ppii.ci1(), i_str, stack_[i]); + } + pps->write(">"); + + return false; + } + } } /*namespace scm*/ } /*namespace xo*/ diff --git a/src/reader/exprstate.cpp b/src/reader/exprstate.cpp index bf30134d..13714a38 100644 --- a/src/reader/exprstate.cpp +++ b/src/reader/exprstate.cpp @@ -3,6 +3,7 @@ #include "exprstate.hpp" #include "exprstatestack.hpp" #include "parserstatemachine.hpp" +#include "pretty_parserstatemachine.hpp" #include "pretty_expression.hpp" //#include "formal_arg.hpp" #include "xo/expression/Variable.hpp" diff --git a/src/reader/exprstatestack.cpp b/src/reader/exprstatestack.cpp index 2e070c48..bb38305a 100644 --- a/src/reader/exprstatestack.cpp +++ b/src/reader/exprstatestack.cpp @@ -4,6 +4,7 @@ */ #include "exprstatestack.hpp" +#include namespace xo { namespace scm { @@ -65,6 +66,48 @@ namespace xo { os << ">" << std::endl; } + + bool + exprstatestack::pretty_print(const ppindentinfo & ppii) const + { + ppstate * pps = ppii.pps(); + + if (ppii.upto()) { + if (stack_.size() > 1) + return false; + + if (!pps->print_upto("print_upto_tag("size", stack_.size())) + return false; + + /** always multiple lines if more than one element in stack **/ + if ((stack_.size() > 0) + && !pps->print_upto_tag("[0]", *stack_[0].get())) + { + return false; + } + + pps->write(">"); + + return true; + } else { + pps->write("newline_pretty_tag(ppii.ci1(), "size", stack_.size()); + + for (std::size_t i = 0, z = stack_.size(); i < z; ++i) { + std::string i_str = tostr("[", z-i-1, "]"); + + pps->newline_pretty_tag(ppii.ci1(), i_str, *stack_[i].get()); + } + + pps->write(">"); + + return false; + } + } } /*namespace scm*/ } /*namespace xo*/ diff --git a/src/reader/parserstatemachine.cpp b/src/reader/parserstatemachine.cpp index 34c7cbbe..feb99d53 100644 --- a/src/reader/parserstatemachine.cpp +++ b/src/reader/parserstatemachine.cpp @@ -5,6 +5,9 @@ #include "parserstatemachine.hpp" #include "exprstatestack.hpp" +#include "pretty_parserstatemachine.hpp" +#include "pretty_envframestack.hpp" +#include "pretty_localenv.hpp" #include "xo/expression/pretty_expression.hpp" namespace xo { diff --git a/src/reader/pretty_parserstatemachine.cpp b/src/reader/pretty_parserstatemachine.cpp new file mode 100644 index 00000000..b4649042 --- /dev/null +++ b/src/reader/pretty_parserstatemachine.cpp @@ -0,0 +1,44 @@ +/* file pretty_parserstatemachine.cpp + * + * author: Roland Conybeare, Jul 2025 + */ + +#include "pretty_parserstatemachine.hpp" +#include "pretty_exprstatestack.hpp" +#include "pretty_envframestack.hpp" +#include "exprstatestack.hpp" + +namespace xo { + namespace print { + bool + ppdetail::print_pretty(const ppindentinfo & ppii, const xo::scm::parserstatemachine & x) + { + ppstate * pps = ppii.pps(); + + if (ppii.upto()) { + if (!pps->print_upto("print_upto_tag("stack", x.p_stack_)) + return false; + + if (!pps->print_upto_tag("env_stack", x.p_env_stack_)) + return false; + + if (!pps->print_upto_tag("emit_expr", (void*)x.p_emit_expr_)) + return false; + + return pps->print_upto(">"); + } else { + pps->write("newline_pretty_tag(ppii.ci1(), "stack", x.p_stack_); + pps->newline_pretty_tag(ppii.ci1(), "env_stack", x.p_env_stack_); + pps->newline_pretty_tag(ppii.ci1(), "emit_expr", (void*)x.p_emit_expr_); + pps->write(">"); + + return false; + } + } + + } /*namespace print*/ +} /*namespace xo*/ diff --git a/src/reader/progress_xs.cpp b/src/reader/progress_xs.cpp index fea536a7..4fe13126 100644 --- a/src/reader/progress_xs.cpp +++ b/src/reader/progress_xs.cpp @@ -4,6 +4,7 @@ #include "exprstatestack.hpp" #include "expect_expr_xs.hpp" #include "parserstatemachine.hpp" +#include "pretty_exprstatestack.hpp" #include "xo/expression/AssignExpr.hpp" #include "xo/expression/Apply.hpp" #include "xo/expression/pretty_expression.hpp"