pretty printing -- copmlete for xo::ast::GeneralizedExpression

This commit is contained in:
Roland Conybeare 2025-07-19 11:47:03 -05:00
commit c0587aa4fb
14 changed files with 206 additions and 29 deletions

View file

@ -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<rp<LocalEnv>> stack_;

View file

@ -76,7 +76,7 @@ namespace xo {
}
class parserstatemachine; /* see parserstatemachine.hpp */
class exprstatestack;
class exprstatestack; /* see exprstatestack.hpp */
class formal_arg;

View file

@ -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<std::unique_ptr<exprstate>> stack_;
@ -65,21 +70,6 @@ namespace xo {
return os;
}
} /*namespace scm*/
#ifndef ppdetail_atomic
namespace print {
template <>
struct ppdetail<xo::scm::exprstatestack *> {
static bool print_upto(ppstate * pps, const xo::scm::exprstatestack * x) {
return ppdetail_atomic<const xo::scm::exprstatestack *>::print_upto(pps, x);
}
static void print_pretty(ppstate * pps, const xo::scm::exprstatestack * x) {
ppdetail_atomic<const xo::scm::exprstatestack *>::print_pretty(pps, x);
}
};
}
#endif
} /*namespace xo*/

View file

@ -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 */

View file

@ -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<xo::scm::envframestack*> {
static inline bool print_pretty(const ppindentinfo & ppii, const xo::scm::envframestack * p) {
return p->pretty_print(ppii);
}
};
} /*namespace print*/
} /*namespace xo*/

View file

@ -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<xo::scm::exprstatestack*> {
static inline bool print_pretty(const ppindentinfo & ppii, const xo::scm::exprstatestack * p) {
return p->pretty_print(ppii);
}
};
} /*namespace print*/
} /*namespace xo*/

View file

@ -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<xo::scm::parserstatemachine> {
static bool print_pretty(const ppindentinfo & ppii, const xo::scm::parserstatemachine & x);
};
}
} /*namespace xo*/

View file

@ -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)

View file

@ -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("<envframestack"))
return false;
if (!pps->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("<envframestack");
pps->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*/

View file

@ -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"

View file

@ -4,6 +4,7 @@
*/
#include "exprstatestack.hpp"
#include <cstdint>
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("<exprstatestack"))
return false;
if (!pps->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("<exprstatestack");
pps->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*/

View file

@ -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 {

View file

@ -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<xo::scm::parserstatemachine>::print_pretty(const ppindentinfo & ppii, const xo::scm::parserstatemachine & x)
{
ppstate * pps = ppii.pps();
if (ppii.upto()) {
if (!pps->print_upto("<psm"))
return false;
if (!pps->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("<psm");
pps->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*/

View file

@ -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"