xo-reader2 xo-expression2: define example working and printing

def foo : f64 = 3.141593;
This commit is contained in:
Roland Conybeare 2026-01-22 18:40:42 -05:00
commit 2bf23b41a9
12 changed files with 206 additions and 10 deletions

View file

@ -44,6 +44,8 @@ xo_add_genfacet(
OUTPUT_CPP_DIR src/expression2
)
# ----------------------------------------------------------------
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-expression2-facetimpl-expression-constant
@ -56,6 +58,18 @@ xo_add_genfacetimpl(
OUTPUT_CPP_DIR src/expression2
)
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-expression2-facetimpl-printable-constant
FACET_PKG xo_printable2
FACET Printable
REPR Constant
INPUT idl/IPrintable_DConstant.json5
OUTPUT_HPP_DIR include/xo/expression2
OUTPUT_IMPL_SUBDIR detail
OUTPUT_CPP_DIR src/expression2
)
# ----------------------------------------------------------------
# note: manual target; generated code committed to git

View file

@ -0,0 +1,13 @@
{
mode: "implementation",
includes: [ "<xo/printable2/Printable.hpp>",
"<xo/printable2/detail/IPrintable_Xfer.hpp>" ],
local_types: [ ],
namespace1: "xo",
namespace2: "scm",
facet_idl: "idl/Printable.json5",
brief: "provide APrintable interface for DConstant",
using_doxygen: true,
repr: "DConstant",
doc: [ "implement APrintable for DConstant" ],
}

View file

@ -22,6 +22,7 @@ namespace xo {
using AAllocator = xo::mm::AAllocator;
using AGCObject = xo::mm::AGCObject;
using typeseq = xo::reflect::typeseq;
using ppindentinfo = xo::print::ppindentinfo;
public:
explicit DConstant(obj<AGCObject> value) noexcept;
@ -45,6 +46,8 @@ namespace xo {
void assign_valuetype(TypeDescr td) noexcept { typeref_.resolve(td); }
bool pretty(const ppindentinfo & ppii) const;
private:
static TypeDescr _lookup_td(typeseq tseq);

View file

@ -0,0 +1,62 @@
/** @file IPrintable_DConstant.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IPrintable_DConstant.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IPrintable_DConstant.json5]
**/
#pragma once
#include "Printable.hpp"
#include <xo/printable2/Printable.hpp>
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
#include "DConstant.hpp"
namespace xo { namespace scm { class IPrintable_DConstant; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::print::APrintable,
xo::scm::DConstant>
{
using ImplType = xo::print::IPrintable_Xfer
<xo::scm::DConstant,
xo::scm::IPrintable_DConstant>;
};
}
}
namespace xo {
namespace scm {
/** @class IPrintable_DConstant
**/
class IPrintable_DConstant {
public:
/** @defgroup scm-printable-dconstant-type-traits **/
///@{
using ppindentinfo = xo::print::APrintable::ppindentinfo;
using Copaque = xo::print::APrintable::Copaque;
using Opaque = xo::print::APrintable::Opaque;
///@}
/** @defgroup scm-printable-dconstant-methods **/
///@{
// const methods
/** Pretty-printing support for this object.
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
static bool pretty(const DConstant & self, const ppindentinfo & ppii);
// non-const methods
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -11,7 +11,9 @@ set(SELF_SRCS
TypeRef.cpp
IExpression_Any.cpp
IExpression_DConstant.cpp
IPrintable_DConstant.cpp
IExpression_DVariable.cpp
IPrintable_DVariable.cpp

View file

@ -7,12 +7,16 @@
#include "TypeDescr.hpp"
#include <xo/object2/DFloat.hpp>
#include <xo/object2/DInteger.hpp>
#include <xo/facet/FacetRegistry.hpp>
#include <xo/reflect/Reflect.hpp>
#include <xo/printable2/Printable.hpp>
#include <xo/reflectutil/typeseq.hpp>
namespace xo {
using xo::scm::DFloat;
using xo::scm::DInteger;
using xo::print::APrintable;
using xo::facet::FacetRegistry;
using xo::reflect::Reflect;
using xo::reflect::TypeDescr;
using xo::reflect::typeseq;
@ -57,6 +61,20 @@ namespace xo {
return nullptr;
}
bool
DConstant::pretty(const ppindentinfo & ppii) const
{
obj<APrintable> value
= FacetRegistry::instance().variant<APrintable,AGCObject>(value_);
return ppii.pps()->pretty_struct
(ppii,
"DConstant",
refrtag("value_.tseq", value_._typeseq()),
refrtag("value.tseq", value._typeseq()),
refrtag("value", value));
}
} /*namespace scm*/
} /*namespace xo*/

View file

@ -79,11 +79,21 @@ namespace xo {
auto rhs = FacetRegistry::instance().try_variant<APrintable,
AExpression>(rhs_);
return ppii.pps()->pretty_struct
(ppii,
"DDefineExpr",
refrtag("lhs", lhs),
refrtag("rhs", cond(rhs_, rhs, "nullptr")));
// note: xo::print::cond() doesn't resolve the way we want here
if (rhs) {
return ppii.pps()->pretty_struct
(ppii,
"DDefineExpr",
refrtag("lhs", lhs),
refrtag("rhs", rhs));
} else {
return ppii.pps()->pretty_struct
(ppii,
"DDefineExpr",
refrtag("lhs", lhs));
}
}
} /*namespace scm*/

View file

@ -0,0 +1,28 @@
/** @file IPrintable_DConstant.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IPrintable_DConstant.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IPrintable_DConstant.json5]
**/
#include "detail/IPrintable_DConstant.hpp"
namespace xo {
namespace scm {
auto
IPrintable_DConstant::pretty(const DConstant & self, const ppindentinfo & ppii) -> bool
{
return self.pretty(ppii);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IPrintable_DConstant.cpp */

View file

@ -8,10 +8,15 @@
#include <xo/expression2/detail/IGCObject_DUniqueString.hpp>
#include <xo/expression2/detail/IPrintable_DUniqueString.hpp>
#include <xo/expression2/detail/IExpression_DDefineExpr.hpp>
#include <xo/expression2/detail/IPrintable_DDefineExpr.hpp>
#include <xo/expression2/detail/IExpression_DVariable.hpp>
#include <xo/expression2/detail/IPrintable_DVariable.hpp>
#include <xo/expression2/detail/IExpression_DConstant.hpp>
#include <xo/expression2/detail/IPrintable_DConstant.hpp>
#include <xo/gc/detail/AGCObject.hpp>
#include <xo/printable2/detail/APrintable.hpp>
#include <xo/facet/FacetRegistry.hpp>
@ -32,13 +37,24 @@ namespace xo {
FacetRegistry::register_impl<AGCObject, DUniqueString>();
FacetRegistry::register_impl<APrintable, DUniqueString>();
FacetRegistry::register_impl<APrintable, DDefineExpr>();
// Expression
// +- Constant
// +- Variable
// \- DefineExpr
FacetRegistry::register_impl<AExpression, DConstant>();
FacetRegistry::register_impl<APrintable, DConstant>();
FacetRegistry::register_impl<AExpression, DVariable>();
FacetRegistry::register_impl<APrintable, DVariable>();
FacetRegistry::register_impl<AExpression, DDefineExpr>();
FacetRegistry::register_impl<APrintable, DDefineExpr>();
log && log(xtag("DUniqueString.tseq", typeseq::id<DUniqueString>()));
log && log(xtag("DDefineExpr.tseq", typeseq::id<DDefineExpr>()));
log && log(xtag("DVariable.tseq", typeseq::id<DVariable>()));
log && log(xtag("DConstant.tseq", typeseq::id<DConstant>()));
log && log(xtag("AExpression.tqseq", typeseq::id<AExpression>()));

View file

@ -128,7 +128,8 @@ namespace xo {
if (!retval)
throw std::runtime_error(tostr("FacetRegistry::try_variant failed",
xtag("AFrom.tseq", typeseq::id<AFrom>()),
xtag("ATo.tseq", typeseq::id<ATo>())));
xtag("ATo.tseq", typeseq::id<ATo>()),
xtag("DRepr", from._typeseq())));
return retval;
}

View file

@ -11,6 +11,7 @@
#include "ssm/IPrintable_DDefineSsm.hpp"
#include <xo/expression2/detail/IPrintable_DDefineExpr.hpp>
#include <xo/printable2/Printable.hpp>
#include <xo/facet/FacetRegistry.hpp>
#ifdef NOT_YET
#include "parserstatemachine.hpp"
@ -22,6 +23,7 @@
namespace xo {
using xo::print::APrintable;
using xo::facet::FacetRegistry;
using xo::facet::with_facet;
using xo::facet::typeseq;
@ -617,7 +619,7 @@ namespace xo {
bool
DDefineSsm::pretty(const ppindentinfo & ppii) const
{
auto expr = with_facet<APrintable>::mkobj(def_expr_.data());
auto expr = FacetRegistry::instance().variant<APrintable,AExpression>(def_expr_);
return ppii.pps()->pretty_struct
(ppii,

View file

@ -5,7 +5,10 @@
#include "DProgressSsm.hpp"
#include "ssm/ISyntaxStateMachine_DProgressSsm.hpp"
#include <xo/printable2/Printable.hpp>
#include <xo/facet/FacetRegistry.hpp>
#include <xo/reflectutil/typeseq.hpp>
#include <xo/indentlog/print/cond.hpp>
#ifdef NOT_YET
#include "apply_xs.hpp"
@ -25,6 +28,8 @@ namespace xo {
using xo::scm::Variable;
using xo::scm::Apply;
#endif
using xo::print::APrintable;
using xo::facet::FacetRegistry;
using xo::facet::with_facet;
using xo::reflect::typeseq;
@ -211,12 +216,23 @@ namespace xo {
DProgressSsm::on_semicolon_token(const Token & tk,
ParserStateMachine * p_psm)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
/* note: implementation should parallel .on_rightparen_token() */
(void)tk;
obj<AExpression> expr = this->assemble_expr(p_psm);
{
obj<APrintable> expr_pr = FacetRegistry::instance().variant<APrintable,AExpression>(expr);
assert(expr_pr);
log && log(xtag("expr", expr_pr));
}
p_psm->pop_ssm();
p_psm->on_parsed_expression_with_semicolon(expr);
@ -951,12 +967,23 @@ namespace xo {
bool
DProgressSsm::pretty(const xo::print::ppindentinfo & ppii) const
{
scope log(XO_DEBUG(true));
log && log(xtag("lhs_.tseq", lhs_._typeseq()));
log && log(xtag("rhs_.tseq", rhs_._typeseq()));
obj<APrintable> lhs
= FacetRegistry::instance().variant<APrintable,AExpression>(lhs_);
obj<APrintable> rhs;
if (rhs_)
rhs = FacetRegistry::instance().variant<APrintable,AExpression>(rhs_);
return ppii.pps()->pretty_struct
(ppii,
"DProgressSsm",
refrtag("lhs", lhs_),
refrtag("lhs", lhs),
refrtag("op", op_type_),
refrtag("rhs", rhs_));
cond(rhs, refrtag("rhs", rhs), "nullptr"));
#ifdef NOPE
if (ppii.upto()) {