xo-expression2: + DApplyExpr [WIP]. Builds, not used or tested
This commit is contained in:
parent
9a87b28f3f
commit
6c73d08e32
5 changed files with 165 additions and 1 deletions
64
include/xo/expression2/DApplyExpr.hpp
Normal file
64
include/xo/expression2/DApplyExpr.hpp
Normal 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 */
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
/** @class DVariable*
|
||||
/** @class DVariable
|
||||
* @brief syntax for a variable reference
|
||||
**/
|
||||
class DVariable {
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ set(SELF_SRCS
|
|||
DConstant.cpp
|
||||
DVariable.cpp
|
||||
DDefineExpr.cpp
|
||||
DApplyExpr.cpp
|
||||
|
||||
TypeRef.cpp
|
||||
|
||||
|
|
|
|||
95
src/expression2/DApplyExpr.cpp
Normal file
95
src/expression2/DApplyExpr.cpp
Normal 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 */
|
||||
Loading…
Add table
Add a link
Reference in a new issue