From 6c73d08e32f5302d5a692c8bc514663a8d9a0815 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sun, 25 Jan 2026 13:14:26 -0500 Subject: [PATCH] xo-expression2: + DApplyExpr [WIP]. Builds, not used or tested --- include/xo/expression2/DApplyExpr.hpp | 64 ++++++++++++++++++ include/xo/expression2/DVariable.hpp | 2 +- include/xo/expression2/exprtype.hpp | 4 ++ src/expression2/CMakeLists.txt | 1 + src/expression2/DApplyExpr.cpp | 95 +++++++++++++++++++++++++++ 5 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 include/xo/expression2/DApplyExpr.hpp create mode 100644 src/expression2/DApplyExpr.cpp diff --git a/include/xo/expression2/DApplyExpr.hpp b/include/xo/expression2/DApplyExpr.hpp new file mode 100644 index 00000000..882cf5cd --- /dev/null +++ b/include/xo/expression2/DApplyExpr.hpp @@ -0,0 +1,64 @@ +/** @file DApplyExpr.hpp + * + * @author Roland Conybeare, Jan 2026 + **/ + +#pragma once + +#include "Expression.hpp" +#include "TypeRef.hpp" +#include "exprtype.hpp" +#include +#include +#include + +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 fn() const noexcept { return fn_; } + const DArray * args() const noexcept { return args_; } + + size_type n_arg() const noexcept { return args_->size(); } + obj 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 fn_; + /** expression for each argument vector **/ + const DArray * args_; + }; + } +} + +/* end DApplyExpr.hpp */ diff --git a/include/xo/expression2/DVariable.hpp b/include/xo/expression2/DVariable.hpp index ae362bf1..af2eddd5 100644 --- a/include/xo/expression2/DVariable.hpp +++ b/include/xo/expression2/DVariable.hpp @@ -15,7 +15,7 @@ namespace xo { namespace scm { - /** @class DVariable* + /** @class DVariable * @brief syntax for a variable reference **/ class DVariable { diff --git a/include/xo/expression2/exprtype.hpp b/include/xo/expression2/exprtype.hpp index d64a1191..df4e3be6 100644 --- a/include/xo/expression2/exprtype.hpp +++ b/include/xo/expression2/exprtype.hpp @@ -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"; diff --git a/src/expression2/CMakeLists.txt b/src/expression2/CMakeLists.txt index 17c34275..9dea6457 100644 --- a/src/expression2/CMakeLists.txt +++ b/src/expression2/CMakeLists.txt @@ -7,6 +7,7 @@ set(SELF_SRCS DConstant.cpp DVariable.cpp DDefineExpr.cpp + DApplyExpr.cpp TypeRef.cpp diff --git a/src/expression2/DApplyExpr.cpp b/src/expression2/DApplyExpr.cpp new file mode 100644 index 00000000..0c0546f3 --- /dev/null +++ b/src/expression2/DApplyExpr.cpp @@ -0,0 +1,95 @@ +/** @file DApplyExpr.cpp + * + * @author Roland Conybeare, Jan 2026 + **/ + +#include "DApplyExpr.hpp" +#include "Expression.hpp" +#include +#include + +namespace xo { + using xo::print::APrintable; + using xo::facet::FacetRegistry; + using xo::mm::AGCObject; + + namespace scm { + obj + 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 arg_i = args_->at(i); + + auto expr_i = FacetRegistry::instance().variant(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(" fn + = FacetRegistry::instance().variant(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 arg_i + = FacetRegistry::instance().variant(this->arg(i_arg)); + + if (!pps->print_upto(refrtag(concat("arg", 1+i_arg), arg_i))) + return false; + } + + return true; + } else { + pps->write(" fn + = FacetRegistry::instance().variant(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 arg_i + = FacetRegistry::instance().variant(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 */