xo-interpreter2/src/expression2/DApplyExpr.cpp

128 lines
4.1 KiB
C++

/** @file DApplyExpr.cpp
*
* @author Roland Conybeare, Jan 2026
**/
#include "DApplyExpr.hpp"
#include "Expression.hpp"
#include <xo/printable2/Printable.hpp>
#include <xo/facet/FacetRegistry.hpp>
namespace xo {
using xo::print::APrintable;
using xo::facet::FacetRegistry;
using xo::reflect::typeseq;
using xo::mm::AGCObject;
namespace scm {
/* incomplete! */
DApplyExpr::DApplyExpr(TypeRef typeref,
obj<AExpression> fn_expr,
size_type n_args) : typeref_{typeref},
fn_{fn_expr},
n_args_{n_args}
{}
DApplyExpr *
DApplyExpr::scaffold(obj<AAllocator> mm,
TypeRef typeref,
obj<AExpression> fn_expr,
size_type n_args)
{
void * mem = mm.alloc(typeseq::id<DApplyExpr>(),
sizeof(DApplyExpr) + (n_args * sizeof(obj<AExpression>)));
DApplyExpr * result = new (mem) DApplyExpr(typeref,
fn_expr,
n_args);
return result;
}
void
DApplyExpr::assign_arg(size_type i_arg,
obj<AExpression> expr)
{
if (i_arg < n_args_) {
this->args_[i_arg] = expr;
} else {
assert(false);
throw std::runtime_error(tostr("assign out-of-range argument i_arg where [0..n_args) expected",
xtag("i_arg", i_arg),
xtag("expr", expr),
xtag("n_args", n_args_)));
}
}
obj<AExpression>
DApplyExpr::arg(size_type i) const
{
if (i >= n_args_) [[unlikely]] {
throw std::runtime_error(tostr("attempt to fetch argument i where [0..n) expected",
xtag("i", i),
xtag("n", n_args_),
xtag("src", "DApplyExpr::arg")));
}
return args_[i];
}
void
DApplyExpr::assign_valuetype(TypeDescr td) noexcept {
typeref_.resolve(td);
}
bool
DApplyExpr::pretty(const ppindentinfo & ppii) const {
using xo::print::ppstate;
ppstate * pps = ppii.pps();
if (ppii.upto()) {
/* perhaps print on one line */
if (!pps->print_upto("<ApplyExpr"))
return false;
{
obj<APrintable> fn
= FacetRegistry::instance().variant<APrintable>(fn_);
if (!pps->print_upto(refrtag("fn", fn)))
return false;
}
for (size_t i_arg = 0; i_arg < n_args_; ++i_arg) {
obj<APrintable> arg_i
= FacetRegistry::instance().variant<APrintable>(args_[i_arg]);
if (!pps->print_upto(refrtag(concat("arg", 1+i_arg), arg_i)))
return false;
}
return true;
} else {
pps->write("<ApplyExpr");
obj<APrintable> fn
= FacetRegistry::instance().variant<APrintable>(fn_);
pps->newline_indent(ppii.ci1());
pps->pretty(refrtag("fn", fn));
for (size_t i_arg = 0; i_arg < n_args_; ++i_arg) {
obj<APrintable> arg_i
= FacetRegistry::instance().variant<APrintable>(args_[i_arg]);
pps->newline_indent(ppii.ci1());
pps->pretty(refrtag(concat("arg", 1+i_arg), arg_i));
}
return false;
}
}
} /*namespace scm*/
} /*namespace xo*/
/* end DApplyExpr.cpp */