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

This commit is contained in:
Roland Conybeare 2025-07-19 11:47:03 -05:00
commit b8ae0f95fa
28 changed files with 327 additions and 116 deletions

View file

@ -101,7 +101,7 @@ namespace xo {
virtual void attach_envs(bp<Environment> p) override;
virtual void display(std::ostream & os) const override;
virtual std::uint32_t pretty_print(ppstate * pps, bool upto) const override;
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
private:
Apply(TypeDescr apply_valuetype,

View file

@ -40,6 +40,7 @@ namespace xo {
virtual void attach_envs(bp<Environment> p) override;
virtual void display(std::ostream & os) const override;
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
private:
AssignExpr(const rp<Variable> & lhs,

View file

@ -6,6 +6,7 @@
#pragma once
#include "ConstantInterface.hpp"
#include "pretty_expression.hpp"
#include <type_traits>
namespace xo {
@ -70,11 +71,17 @@ namespace xo {
if (value_td_)
os << xtag("type", value_td_->short_name());
else
os << xtag("type", "nullptr");;
os << xtag("type", "nullptr");
os << xtag("value", value_);
os << ">";
}
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override {
return ppii.pps()->pretty_struct(ppii, "Constant",
rtag("type", print::quot(this->valuetype()->short_name())),
refrtag("value", value_));
}
private:
explicit Constant(TypeDescr x_type, const T & x)
: ConstantInterface(exprtype::constant, x_type),

View file

@ -66,6 +66,7 @@ namespace xo {
}
virtual void display(std::ostream & os) const override;
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
protected:
ConvertExpr(TypeDescr dest_type,

View file

@ -76,7 +76,7 @@ namespace xo {
}
virtual void display(std::ostream & os) const override;
virtual std::uint32_t pretty_print(ppstate * pps, bool upto) const override;
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
protected:
/**

View file

@ -7,8 +7,10 @@
#include "xo/refcnt/Refcounted.hpp"
#include "binding_path.hpp"
#include "xo/indentlog/print/pretty.hpp"
namespace xo {
namespace ast {
class Expression;
@ -35,6 +37,7 @@ namespace xo {
virtual bp<Expression> lookup_var(const std::string & vname) const = 0;
virtual void print(std::ostream & os) const = 0;
virtual std::uint32_t pretty_print(const xo::print::ppindentinfo & ppii) const = 0;
};
inline std::ostream &

View file

@ -20,8 +20,9 @@ namespace xo {
**/
class GeneralizedExpression : public ref::Refcount {
public:
using TypeDescr = xo::reflect::TypeDescr;
using ppstate = xo::print::ppstate;
using TypeDescr = xo::reflect::TypeDescr;
using ppstate = xo::print::ppstate;
using ppindentinfo = xo::print::ppindentinfo;
public:
GeneralizedExpression(exprtype extype, TypeDescr valuetype)
@ -35,7 +36,7 @@ namespace xo {
/** human-readable string representation **/
virtual std::string display_string() const;
/** pretty printing support. See [xo-indentlog/xo/indentlog/pretty.hpp] **/
virtual std::uint32_t pretty_print(ppstate * pps, bool upto) const;
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const = 0;
protected:
/** useful when scaffolding expressions in a parser **/
@ -59,4 +60,4 @@ namespace xo {
} /*namespace xo*/
/** end GeneralizedExpression.hpp **/
/* end GeneralizedExpression.hpp */

View file

@ -42,6 +42,7 @@ namespace xo {
}
virtual void print(std::ostream & os) const override;
virtual std::uint32_t pretty_print(const xo::print::ppindentinfo & ppii) const override;
private:
GlobalEnv();

View file

@ -6,7 +6,7 @@
#pragma once
#include "Expression.hpp"
#include <vector>
//#include <vector>
#include <string>
//#include <cstdint>
@ -102,6 +102,7 @@ namespace xo {
#endif
virtual void display(std::ostream & os) const override;
virtual std::uint32_t pretty_print(const ppindentinfo & ppi) const override;
private:
/**

View file

@ -98,7 +98,7 @@ namespace xo {
virtual void attach_envs(bp<Environment> p) override;
virtual void display(std::ostream & os) const override;
virtual std::uint32_t pretty_print(ppstate * pps, bool upto) const override;
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
protected:
/** create type description for lambda with arguments @p argv

View file

@ -79,6 +79,7 @@ namespace xo {
}
virtual void print(std::ostream & os) const override;
virtual std::uint32_t pretty_print(const print::ppindentinfo & ppii) const override;
private:
LocalEnv(const std::vector<rp<Variable>> & argv, const rp<Environment> & parent_env);
@ -103,12 +104,6 @@ namespace xo {
};
} /*namespace ast*/
#ifndef ppdetail_atomic
namespace print {
PPDETAIL_ATOMIC(xo::ast::LocalEnv);
}
#endif
} /*namespace xo*/

View file

@ -6,9 +6,10 @@
#pragma once
#include "PrimitiveInterface.hpp"
#include "pretty_expression.hpp"
#include "llvmintrinsic.hpp"
#include "xo/reflect/Reflect.hpp"
//#include <cstdint>
#include "xo/indentlog/print/quoted.hpp"
extern "C" {
/* these symbols needed to link primitives */
@ -90,6 +91,53 @@ namespace xo {
<< ">";
}
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override {
/* 1. rtag instead of refrtag:
* print::quot() is a temporary rvalue; lifetime ends before control enters pretty_struct()
*
* 2. value cast to void*:
* we don't have pretty printer for native function pointers anyway
* + simplifies ppdetail_atomic
*/
return ppii.pps()->pretty_struct(ppii, "Primitive",
refrtag("name", name_),
rtag("type", print::quot(this->valuetype()->short_name())),
refrtag("value", (void*)(this->value())));
#ifdef OBSOLETE
ppstate * pps = ppii.pps();
if (ppii.upto()) {
if (!pps->print_upto("<Primitive"))
return false;
if (!pps->print_upto_tag("name", name_))
return false;
if (!pps->print_upto_tag("type", print::quot(this->value_td()->short_name())))
return false;
if (!pps->print_upto_tag("value", (void*)(this->value())))
return false;
pps->write(">");
return true;
} else {
pps->write("<Primitive");
pps->newline_pretty_tag(ppii.ci1(), "name", name_);
pps->newline_pretty_tag(ppii.ci1(), "type", print::quot(this->value_td()->short_name()));
/* don't have pretty printer for native function pointers anyway
* + simplifies ppdetail_atomic
*/
pps->newline_pretty_tag(ppii.ci1(), "value", (void*)this->value());
pps->write(">");
return false;
}
#endif
}
private:
Primitive(TypeDescr fn_type,
const std::string & name,

View file

@ -6,7 +6,6 @@
#pragma once
#include "Expression.hpp"
//#include <cstdint>
namespace xo {
namespace ast {
@ -38,7 +37,7 @@ namespace xo {
// ----- from GeneralizedExpression ----
virtual void display(std::ostream & os) const override;
virtual std::uint32_t pretty_print(ppstate * pps, bool upto) const override;
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
private:
/** sequence of expressions; evaluate in left-to-right order.
@ -49,5 +48,4 @@ namespace xo {
} /*namespace xo*/
/** end Sequence.hpp **/

View file

@ -65,6 +65,7 @@ namespace xo {
virtual void attach_envs(bp<Environment> /*p*/) override;
virtual void display(std::ostream & os) const override;
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
private:
Variable(const std::string & name,

View file

@ -6,7 +6,6 @@
#pragma once
#include "xo/indentlog/print/pretty.hpp"
#include "xo/indentlog/print/pretty_tag.hpp"
#include "xo/refcnt/pretty_refcnt.hpp"
#include "Expression.hpp"
@ -14,23 +13,17 @@ namespace xo {
namespace print {
template<>
struct ppdetail<xo::ast::GeneralizedExpression> {
static bool print_upto(ppstate * pps, const xo::ast::GeneralizedExpression & x) {
return x.pretty_print(pps, true);
}
static void print_pretty(ppstate * pps, const xo::ast::GeneralizedExpression & x) {
x.pretty_print(pps, false);
static bool print_pretty(const ppindentinfo & ppii,
const xo::ast::GeneralizedExpression & x) {
return x.pretty_print(ppii);
}
};
template <>
struct ppdetail<xo::ast::Expression> {
static bool print_upto(ppstate * pps, const xo::ast::Expression & x) {
return x.pretty_print(pps, true);
}
static void print_pretty(ppstate * pps, const xo::ast::Expression & x) {
x.pretty_print(pps, false);
static bool print_pretty(const ppindentinfo & ppii,
const xo::ast::Expression & x) {
return x.pretty_print(ppii);
}
};
}

View file

@ -0,0 +1,18 @@
/* @file pretty_localenv.hpp */
#pragma once
#include "xo/indentlog/print/pretty.hpp"
#include "xo/refcnt/pretty_refcnt.hpp"
#include "LocalEnv.hpp"
namespace xo {
namespace print {
template <>
struct ppdetail<xo::ast::LocalEnv> {
static bool print_pretty(const ppindentinfo & ppii, const xo::ast::LocalEnv & x) {
return x.pretty_print(ppii);
}
};
}
}

View file

@ -0,0 +1,20 @@
/* file pretty_variable.hpp
*
* author: Roland Conybeare, Jul 2025
*/
#pragma once
#include "pretty_expression.hpp"
#include "Variable.hpp"
namespace xo {
namespace print {
template <>
struct ppdetail<xo::ast::Variable> {
static bool print_pretty(const ppindentinfo & ppii, const xo::ast::Expression & x) {
return x.pretty_print(ppii);
}
};
}
}