xo-interpreter2 stack: scaffold DClosure, DLocalEnv [WIP]
This commit is contained in:
parent
a63e4b1dae
commit
cbca2b7c6b
7 changed files with 254 additions and 0 deletions
49
include/xo/interpreter2/DClosure.hpp
Normal file
49
include/xo/interpreter2/DClosure.hpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/** @file DClosure.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#include "LocalEnv.hpp"
|
||||
#include <xo/expression2/LambdaExpr.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
/** @brief runtime representation for a procedure
|
||||
*
|
||||
* Maintains lambda + captured lexical context
|
||||
**/
|
||||
class DClosure {
|
||||
public:
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
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);
|
||||
|
||||
/** 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(); }
|
||||
|
||||
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 */
|
||||
67
include/xo/interpreter2/DLocalEnv.hpp
Normal file
67
include/xo/interpreter2/DLocalEnv.hpp
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/** @file DLocalEnv.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <xo/expression2/LocalSymtab.hpp>
|
||||
#include <xo/object2/DArray.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
/** @brief bindings for arguments to a lambda
|
||||
**/
|
||||
class DLocalEnv {
|
||||
public:
|
||||
using DArray = xo::scm::DArray;
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
using ppindentinfo = xo::print::ppindentinfo;
|
||||
using size_type = std::uint32_t;
|
||||
|
||||
public:
|
||||
/** @defgroup scm-localenv-constructors constructors **/
|
||||
///@{
|
||||
|
||||
/** empty instance with parent @p p for variables in @p symtab **/
|
||||
DLocalEnv(DLocalEnv * parent,
|
||||
DLocalSymtab * symtab,
|
||||
DArray * args);
|
||||
|
||||
static DLocalEnv * _make_empty(obj<AAllocator> mm,
|
||||
DLocalEnv * parent,
|
||||
DLocalSymtab * symtab,
|
||||
DArray * args);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-local-env-methods methods **/
|
||||
///@{
|
||||
|
||||
DLocalEnv * parent() const noexcept { return parent_; }
|
||||
size_type size() const noexcept { return symtab_->size(); }
|
||||
|
||||
/** lookup current value associated with binding @p ix **/
|
||||
obj<AGCObject> lookup_value(Binding ix) const noexcept;
|
||||
|
||||
/** assign value associated with binding @p ix to @p x **/
|
||||
void assign_value(Binding ix, obj<AGCObject> x);
|
||||
|
||||
///@}
|
||||
|
||||
private:
|
||||
/** parent environment (from closure) **/
|
||||
DLocalEnv * parent_ = nullptr;
|
||||
/** bind values for variables in this symbol table **/
|
||||
DLocalSymtab * symtab_ = nullptr;
|
||||
/** bindings.
|
||||
* (*args)[i] associates a value with symtab->slots_[i]
|
||||
**/
|
||||
DArray * args_ = nullptr;;
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end DLocalEnv.hpp */
|
||||
|
|
@ -21,6 +21,7 @@ namespace xo {
|
|||
VsmInstr old_cont,
|
||||
DArray * args);
|
||||
|
||||
/** create instance using memory from @p mm **/
|
||||
static DVsmApplyFrame * make(obj<AAllocator> mm,
|
||||
obj<AGCObject> old_parent,
|
||||
VsmInstr old_cont,
|
||||
|
|
|
|||
12
include/xo/interpreter2/LocalEnv.hpp
Normal file
12
include/xo/interpreter2/LocalEnv.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/** @file LocalEnv.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "DLocalEnv.hpp"
|
||||
//#include "detail/IGCObject_DLocalEnv.hpp"
|
||||
//#include "detail/IPrintable_DLocalEnv.hpp"
|
||||
|
||||
/* end LocalEnv.hpp */
|
||||
|
|
@ -17,6 +17,10 @@ set(SELF_SRCS
|
|||
IPrintable_DVsmApplyFrame.cpp
|
||||
|
||||
VsmInstr.cpp
|
||||
|
||||
DClosure.cpp
|
||||
DLocalEnv.cpp
|
||||
|
||||
#IExpression_Any.cpp
|
||||
)
|
||||
|
||||
|
|
|
|||
29
src/interpreter2/DClosure.cpp
Normal file
29
src/interpreter2/DClosure.cpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/** @file DClosure.cpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#include "DClosure.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
DClosure::DClosure(const DLambdaExpr * lm,
|
||||
const DLocalEnv * env)
|
||||
: lambda_{lm}, env_{env}
|
||||
{}
|
||||
|
||||
DClosure *
|
||||
DClosure::make(obj<AAllocator> mm,
|
||||
const DLambdaExpr * lm,
|
||||
const DLocalEnv * env)
|
||||
{
|
||||
void * mem = mm.alloc_for<DClosure>();
|
||||
|
||||
return new (mem) DClosure(lm, env);
|
||||
}
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end DClosure.cpp */
|
||||
92
src/interpreter2/DLocalEnv.cpp
Normal file
92
src/interpreter2/DLocalEnv.cpp
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
/** @file DLocalEnv.cpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#include "DLocalEnv.hpp"
|
||||
#include <xo/reflectutil/typeseq.hpp>
|
||||
|
||||
namespace xo {
|
||||
using xo::mm::AGCObject;
|
||||
using xo::reflect::typeseq;
|
||||
|
||||
namespace scm {
|
||||
|
||||
DLocalEnv::DLocalEnv(DLocalEnv * parent,
|
||||
DLocalSymtab * symtab,
|
||||
DArray * args)
|
||||
: parent_{parent},
|
||||
symtab_{symtab},
|
||||
args_{args}
|
||||
{}
|
||||
|
||||
DLocalEnv *
|
||||
DLocalEnv::_make_empty(obj<AAllocator> mm,
|
||||
DLocalEnv * parent,
|
||||
DLocalSymtab * symtab,
|
||||
DArray * args)
|
||||
{
|
||||
assert(symtab);
|
||||
|
||||
void * mem = mm.alloc_for<DLocalEnv>();
|
||||
|
||||
return new (mem) DLocalEnv(parent, symtab, args);
|
||||
}
|
||||
|
||||
obj<AGCObject>
|
||||
DLocalEnv::lookup_value(Binding ix) const noexcept
|
||||
{
|
||||
assert(!ix.is_global());
|
||||
|
||||
const DLocalEnv * env = this;
|
||||
|
||||
for (auto i = ix.i_link(); i > 0; --i) {
|
||||
env = env->parent();
|
||||
}
|
||||
|
||||
if (env) {
|
||||
auto j = ix.j_slot();
|
||||
|
||||
if (j < static_cast<decltype(j)>(env->size())) {
|
||||
return (*(env->args_))[j];
|
||||
} else {
|
||||
assert(false);
|
||||
}
|
||||
} else {
|
||||
assert(false);
|
||||
}
|
||||
|
||||
/* something terribly wrong if control here */
|
||||
return obj<AGCObject>();
|
||||
}
|
||||
|
||||
void
|
||||
DLocalEnv::assign_value(Binding ix, obj<AGCObject> x)
|
||||
{
|
||||
assert(!ix.is_global());
|
||||
|
||||
const DLocalEnv * env = this;
|
||||
|
||||
for (auto i = ix.i_link(); i > 0; --i) {
|
||||
env = env->parent();
|
||||
}
|
||||
|
||||
if (env) {
|
||||
auto j = ix.j_slot();
|
||||
|
||||
if (j < static_cast<decltype(j)>(env->size())) {
|
||||
(*(env->args_))[j] = x;
|
||||
} else {
|
||||
assert(false);
|
||||
}
|
||||
} else {
|
||||
assert(false);
|
||||
}
|
||||
|
||||
/* something terribly wrong if control here */
|
||||
}
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end DLocalEnv.cpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue