xo-expression2: + DApplyExpr [WIP]. Builds, not used or tested

This commit is contained in:
Roland Conybeare 2026-01-25 13:14:26 -05:00
commit c0978f5098
7 changed files with 181 additions and 1 deletions

View file

@ -0,0 +1,64 @@
/** @file DApplyExpr.hpp
*
* @author Roland Conybeare, Jan 2026
**/
#pragma once
#include "Expression.hpp"
#include "TypeRef.hpp"
#include "exprtype.hpp"
#include <xo/object2/DArray.hpp>
#include <xo/reflect/TypeDescr.hpp>
#include <xo/indentlog/print/pretty.hpp>
namespace xo {
namespace scm {
/** @class DApplyExpr
* @brief syntax for a procedure/function call
**/
class DApplyExpr {
public:
using TypeDescr = xo::reflect::TypeDescr;
using ppindentinfo = xo::print::ppindentinfo;
using size_type = std::size_t;
public:
obj<AExpression> fn() const noexcept { return fn_; }
const DArray * args() const noexcept { return args_; }
size_type n_arg() const noexcept { return args_->size(); }
obj<AExpression> arg(size_type i) const;
/** @defgroup scm-applyexpr-expression-facet **/
///@{
exprtype extype() const noexcept { return exprtype::apply; }
TypeRef typeref() const noexcept { return typeref_; }
TypeDescr valuetype() const noexcept { return typeref_.td(); }
void assign_valuetype(TypeDescr td) noexcept;
///@}
/** @defgroup scm-applyexpr-printable-facet **/
///@{
bool pretty(const ppindentinfo & ppii) const;
///@}
private:
/** expression value always has type consistent
* with this description
**/
TypeRef typeref_;
/** expression for function/procedure to invoke **/
obj<AExpression> fn_;
/** expression for each argument vector **/
const DArray * args_;
};
}
}
/* end DApplyExpr.hpp */

View file

@ -15,7 +15,7 @@
namespace xo {
namespace scm {
/** @class DVariable*
/** @class DVariable
* @brief syntax for a variable reference
**/
class DVariable {

View file

@ -29,8 +29,10 @@ namespace xo {
#ifdef NOT_YET
/** variable assignment **/
assign,
#endif
/** function call **/
apply,
#ifdef NOT_YET
/** function definition **/
lambda,
#endif
@ -61,7 +63,9 @@ namespace xo {
case exprtype::define: return "define";
#ifdef NOT_YET
case exprtype::assign: return "assign";
#endif
case exprtype::apply: return "apply";
#ifdef NOT_YET
case exprtype::lambda: return "lambda";
case exprtype::variable: return "variable";
case exprtype::ifexpr: return "if_expr";

View file

@ -7,6 +7,7 @@ set(SELF_SRCS
DConstant.cpp
DVariable.cpp
DDefineExpr.cpp
DApplyExpr.cpp
TypeRef.cpp

View file

@ -0,0 +1,95 @@
/** @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::mm::AGCObject;
namespace scm {
obj<AExpression>
DApplyExpr::arg(size_type i) const
{
if (i >= args_->size()) [[unlikely]] {
throw std::runtime_error(tostr("attempt to fetch argument i where [0..n) expected",
xtag("i", i),
xtag("n", args_->size()),
xtag("src", "DApplyExpr::arg")));
}
obj<AGCObject> arg_i = args_->at(i);
auto expr_i = FacetRegistry::instance().variant<AExpression>(arg_i);
if (!expr_i) [[unlikely]] {
throw std::runtime_error(tostr("expected expression interface on argument i",
xtag("i", i),
xtag("arg[i]", arg_i)));
}
return expr_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, n_arg = this->n_arg(); i_arg < n_arg; ++i_arg) {
obj<APrintable> arg_i
= FacetRegistry::instance().variant<APrintable>(this->arg(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, n_arg = this->n_arg(); i_arg < n_arg; ++i_arg) {
obj<APrintable> arg_i
= FacetRegistry::instance().variant<APrintable>(fn_);
pps->newline_indent(ppii.ci1());
pps->pretty(refrtag(concat("arg", 1+i_arg), arg_i));
}
return false;
}
}
} /*namespace scm*/
} /*namespace xo*/
/* end DApplyExpr.cpp */

View file

@ -57,6 +57,12 @@ namespace xo {
**/
void _do_eval_variable_op();
/** evaluate an apply expression
* Require:
* - expression in @ref expr_
**/
void _do_eval_apply_op();
private:
/*
* Some registers are preserved by evaluation:

View file

@ -51,6 +51,9 @@ namespace xo {
case exprtype::variable:
_do_eval_variable_op();
break;
case exprtype::apply:
_do_eval_apply_op();
break;
}
}
@ -77,6 +80,13 @@ namespace xo {
// not implemented
assert(false);
}
void
VirtualSchematikaMachine::_do_eval_apply_op()
{
// not implemented
assert(false);
}
} /*namespace scm*/
} /*namespace xo*/