xo-umbrella2/xo-interpreter2/include/xo/interpreter2/DClosure.hpp
Roland Conybeare 7f4b4e7cbd git subrepo clone (merge) git@github.com:Rconybea/xo-interpreter2.git xo-interpreter2
subrepo:
  subdir:   "xo-interpreter2"
  merged:   "e4c6ff57"
upstream:
  origin:   "git@github.com:Rconybea/xo-interpreter2.git"
  branch:   "main"
  commit:   "e4c6ff57"
git-subrepo:
  version:  "0.4.9"
  origin:   "???"
  commit:   "???"
2026-06-06 22:13:05 -04:00

84 lines
2.6 KiB
C++

/** @file DClosure.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include "LocalEnv.hpp"
#include <xo/expression2/LambdaExpr.hpp>
#include <xo/procedure2/RuntimeContext.hpp>
namespace xo {
namespace scm {
/** @brief runtime representation for a procedure
*
* Maintains lambda + captured lexical context
**/
class DClosure {
public:
using ARuntimeContext = xo::scm::ARuntimeContext;
using AGCObject = xo::mm::AGCObject;
using AGCObjectVisitor = xo::mm::AGCObjectVisitor;
using VisitReason = xo::mm::VisitReason;
using AAllocator = xo::mm::AAllocator;
using ppindentinfo = xo::print::ppindentinfo;
using size_type = std::int32_t;
public:
DClosure(const DLambdaExpr * lm,
const DLocalEnv * env);
/** create instance using memory from @p mm
* for lambda @p lm with captured environment @p env.
**/
static DClosure * make(obj<AAllocator> mm,
const DLambdaExpr * lm,
const DLocalEnv * env);
/** @defgroup scm-closure-general-methods **/
///@{
const DLambdaExpr * lambda() const noexcept { return lambda_; }
const DLocalEnv * env() const noexcept { return env_; }
///@}
/** @defgroup scm-closure-procedure-facet **/
///@{
/** for now, support just fixed-arity procedures **/
bool is_nary() const noexcept { return false; }
/** number of arguments expected by this procedure (-1 if nary) **/
size_type n_args() const noexcept { return lambda_->n_args(); }
obj<AGCObject> apply_nocheck(obj<ARuntimeContext> rcx, const DArray * args);
///@}
/** @defgroup scm-closure-gcobject-facet **/
///@{
DClosure * gco_shallow_move(obj<AGCObjectVisitor> gc) noexcept;
void visit_gco_children(VisitReason reason, obj<AGCObjectVisitor> gc) noexcept;
///@}
/** @defgroup scm-closure-printable-facet **/
///@{
bool pretty(const ppindentinfo & ppii) const;
///@}
private:
/** lambda expression **/
const DLambdaExpr * lambda_ = nullptr;
/** bindings for captured variables
* (from lexical context where lambda evaluated)
**/
const DLocalEnv * env_ = nullptr;
};
} /*namespace scm*/
} /*namespace xo*/
/* end DClosure.hpp */