xo-expression2: + DIfElseExpr + utest

This commit is contained in:
Roland Conybeare 2026-01-27 10:09:26 -05:00
commit 0fcb548587
15 changed files with 1027 additions and 3 deletions

View file

@ -8,6 +8,7 @@ set(SELF_SRCS
DVariable.cpp
DDefineExpr.cpp
DApplyExpr.cpp
DIfElseExpr.cpp
TypeRef.cpp
@ -25,6 +26,9 @@ set(SELF_SRCS
IExpression_DApplyExpr.cpp
IPrintable_DApplyExpr.cpp
IExpression_DIfElseExpr.cpp
IPrintable_DIfElseExpr.cpp
DLocalSymtab.cpp
DGlobalSymtab.cpp

View file

@ -1,5 +1,5 @@
/** @file DDefineExpr.cpp
*
*
* @author Roland Conybeare, Jan 2026
**/

View file

@ -0,0 +1,184 @@
/** @file DIfElseExpr.cpp
*
* @author Roland Conybeare, Jan 2026
**/
#include "DIfElseExpr.hpp"
#include "detail/IExpression_DIfElseExpr.hpp"
#include <xo/printable2/Printable.hpp>
#include <xo/facet/FacetRegistry.hpp>
#include <xo/reflectutil/typeseq.hpp>
namespace xo {
using xo::print::APrintable;
using xo::reflect::typeseq;
using xo::facet::FacetRegistry;
namespace scm {
DIfElseExpr::DIfElseExpr(TypeRef ifexpr_tref,
obj<AExpression> test_expr,
obj<AExpression> when_true,
obj<AExpression> when_false)
: typeref_{ifexpr_tref},
test_{test_expr},
when_true_{when_true},
when_false_{when_false}
{}
obj<AExpression,DIfElseExpr>
DIfElseExpr::make(obj<AAllocator> mm,
obj<AExpression> test,
obj<AExpression> when_true,
obj<AExpression> when_false)
{
return obj<AExpression,DIfElseExpr>
(_make(mm,
test, when_true, when_false));
}
DIfElseExpr *
DIfElseExpr::_make(obj<AAllocator> mm,
obj<AExpression> test,
obj<AExpression> when_true,
obj<AExpression> when_false)
{
void * mem = mm.alloc(typeseq::id<DIfElseExpr>(),
sizeof(DIfElseExpr));
// just crete typevar here, then rely on type checking
// later
auto prefix = TypeRef::prefix_type::from_chars("if");
TypeRef tref = TypeRef::dwim(prefix, nullptr);
return new (mem) DIfElseExpr(tref,
test,
when_true,
when_false);
}
void
DIfElseExpr::assign_valuetype(TypeDescr td) noexcept
{
typeref_.resolve(td);
}
bool
DIfElseExpr::pretty(const ppindentinfo & ppii) const
{
auto test
= FacetRegistry::instance().try_variant<APrintable,
AExpression>(test_);
auto when_true
= FacetRegistry::instance().try_variant<APrintable,
AExpression>(when_true_);
auto when_false
= FacetRegistry::instance().try_variant<APrintable,
AExpression>(when_false_);
return ppii.pps()->pretty_struct
(ppii,
"DIfElseExpr",
refrtag("typeref", typeref_),
refrtag("test", test),
refrtag("when_true", when_true),
refrtag("when_false", when_false));
}
// ----------------------------------------------------------------
#ifdef NOPE
auto IfExpr::check_consistent_valuetype(const rp<Expression> & when_true,
const rp<Expression> & when_false) -> TypeDescr
{
if (when_true->valuetype() != when_false->valuetype())
return nullptr;
return when_true->valuetype();
}
void IfExpr::establish_valuetype()
{
if (this->when_true_.get() && this->when_false_.get())
this->assign_valuetype(check_consistent_valuetype(this->when_true_, this->when_false_));
}
rp<IfExpr>
IfExpr::make(const rp<Expression> & test,
const rp<Expression> & when_true,
const rp<Expression> & when_false)
{
/** TODO: verify test returns _boolean_ type **/
if (when_true->valuetype() != when_false->valuetype()) {
throw std::runtime_error
(tostr("IfExpr::make:"
" types {T1,T2} found for branches of if-expr"
" where equal types expected",
xtag("T1", when_true->valuetype()->canonical_name()),
xtag("T2", when_false->valuetype()->canonical_name())));
}
/* arbitrary choice here */
auto ifexpr_type = when_true->valuetype();
return new IfExpr(ifexpr_type,
test,
when_true,
when_false);
} /*make*/
void
IfExpr::display(std::ostream & os) const {
os << "<IfExpr"
<< xtag("test", test_)
<< xtag("when_true", when_true_);
if (when_false_)
os << xtag("when_false", when_false_);
os << ">";
} /*display*/
std::uint32_t
IfExpr::pretty_print(const ppindentinfo & ppii) const {
return ppii.pps()->pretty_struct(ppii, "IfExpr",
refrtag("test", test_),
refrtag("when_true", when_true_),
refrtag("when_false", when_false_));
}
rp<IfExprAccess>
IfExprAccess::make(rp<Expression> test,
rp<Expression> when_true,
rp<Expression> when_false)
{
auto ifexpr_type = check_consistent_valuetype(when_true, when_false);
return new IfExprAccess(ifexpr_type, std::move(test), std::move(when_true), std::move(when_false));
}
rp<IfExprAccess>
IfExprAccess::make_empty()
{
return new IfExprAccess(nullptr /*ifexpr_valuetype*/,
nullptr /*test*/,
nullptr /*when_true*/,
nullptr /*when_false*/);
}
void
IfExprAccess::assign_when_true(rp<Expression> x)
{
this->when_true_ = std::move(x);
}
void
IfExprAccess::assign_when_false(rp<Expression> x)
{
this->when_false_ = std::move(x);
}
#endif
} /*namespace scm*/
} /*namespace xo*/
/* end DIfElseExpr.cpp */

View file

@ -0,0 +1,45 @@
/** @file IExpression_DIfElseExpr.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IExpression_DIfElseExpr.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IExpression_DIfElseExpr.json5]
**/
#include "detail/IExpression_DIfElseExpr.hpp"
namespace xo {
namespace scm {
auto
IExpression_DIfElseExpr::extype(const DIfElseExpr & self) noexcept -> exprtype
{
return self.extype();
}
auto
IExpression_DIfElseExpr::typeref(const DIfElseExpr & self) noexcept -> TypeRef
{
return self.typeref();
}
auto
IExpression_DIfElseExpr::valuetype(const DIfElseExpr & self) noexcept -> TypeDescr
{
return self.valuetype();
}
auto
IExpression_DIfElseExpr::assign_valuetype(DIfElseExpr & self, TypeDescr td) noexcept -> void
{
self.assign_valuetype(td);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IExpression_DIfElseExpr.cpp */

View file

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

View file

@ -20,6 +20,9 @@
#include <xo/expression2/detail/IExpression_DApplyExpr.hpp>
#include <xo/expression2/detail/IPrintable_DApplyExpr.hpp>
#include <xo/expression2/detail/IExpression_DIfElseExpr.hpp>
#include <xo/expression2/detail/IPrintable_DIfElseExpr.hpp>
#include <xo/gc/detail/AGCObject.hpp>
#include <xo/printable2/detail/APrintable.hpp>
#include <xo/facet/FacetRegistry.hpp>
@ -44,7 +47,8 @@ namespace xo {
// +- Constant
// +- Variable
// +- DefineExpr
// \- ApplyExpr
// +- ApplyExpr
// \- IfElseExpr
FacetRegistry::register_impl<AExpression, DConstant>();
FacetRegistry::register_impl<APrintable, DConstant>();
@ -58,11 +62,15 @@ namespace xo {
FacetRegistry::register_impl<AExpression, DApplyExpr>();
FacetRegistry::register_impl<APrintable, DApplyExpr>();
FacetRegistry::register_impl<AExpression, DIfElseExpr>();
FacetRegistry::register_impl<APrintable, DIfElseExpr>();
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("DApplyExpr.tseq", typeseq::id<DApplyExpr>()));
log && log(xtag("DIfElseExpr.tseq", typeseq::id<DIfElseExpr>()));
log && log(xtag("AExpression.tqseq", typeseq::id<AExpression>()));