xo-interpreter2 stack: work on apply for closures [WIP]

This commit is contained in:
Roland Conybeare 2026-02-12 20:09:22 -05:00
commit 5b97cddbcd
10 changed files with 79 additions and 31 deletions

View file

@ -26,15 +26,15 @@ namespace xo {
/** @defgroup scm-localenv-constructors constructors **/
///@{
/** empty instance with parent @p p for variables in @p symtab **/
/** create 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);
static DLocalEnv * _make(obj<AAllocator> mm,
DLocalEnv * parent,
DLocalSymtab * symtab,
DArray * args);
///@}
/** @defgroup scm-local-env-methods methods **/

View file

@ -24,6 +24,7 @@ namespace xo {
DVsmRcx(VirtualSchematikaMachine * vsm);
obj<AAllocator> allocator() const noexcept;
obj<AAllocator> error_allocator() const noexcept;
private:
/** schematika interpreter **/

View file

@ -31,7 +31,7 @@ namespace xo {
/** Configuration for error allocator
* TODO: may want to make ArenaConfig polymorphic
**/
ArenaConfig error_config_ = ArenaConfig().with_size(64*1024);
ArenaConfig error_config_ = ArenaConfig().with_name("error-reserve").with_size(64*1024);
};
} /*namespace scm*/
} /*namespace xo*/

View file

@ -7,6 +7,7 @@
#include "LambdaExpr.hpp"
#include "LocalEnv.hpp"
#include "VsmRcx.hpp"
#include <xo/object2/RuntimeError.hpp>
#include <xo/indentlog/scope.hpp>
#include <cstddef>
@ -35,6 +36,8 @@ namespace xo {
DClosure::apply_nocheck(obj<ARuntimeContext> rcx,
const DArray * args)
{
(void)args;
scope log(XO_DEBUG(true));
auto vsm_rcx
@ -42,26 +45,31 @@ namespace xo {
log && log(xtag("vsm_rcx.data", (void*)vsm_rcx.data()));
// let's try a not-implemented error
// don't want to clutter Procedure facet, since it's
// lower-level than xo-interpreter2
// we already checked this stuff before calling apply_nocheck()
// assert (n_args == args->size());
#ifdef NOT_YET
// TODO: verify arguments against type signature.
// unless we have evidence that program is type correct
int32_t n_args = this->n_args();
if (n_args != args->size()) {
//
}
auto local_env
= DLocalEnv::_make(vsm_rcx->allocator(),
env_,
lambda_->local_symtab(),
args);
#endif
// plan:
// 1. push current local environment to vsm stack_
// 2. set expr_ to lambda body
// 2. set pc_ to eval
// 3. set cont_ to restore local_env_
(void)args;
auto err_mm
= vsm_rcx->error_allocator();
assert(false);
auto err
= DRuntimeError::make(err_mm,
"DClosure::apply_nocheck",
"not implemented");
return err;
}
size_t

View file

@ -22,10 +22,10 @@ namespace xo {
{}
DLocalEnv *
DLocalEnv::_make_empty(obj<AAllocator> mm,
DLocalEnv * parent,
DLocalSymtab * symtab,
DArray * args)
DLocalEnv::_make(obj<AAllocator> mm,
DLocalEnv * parent,
DLocalSymtab * symtab,
DArray * args)
{
assert(symtab);

View file

@ -19,6 +19,12 @@ namespace xo {
return vsm_->allocator();
}
obj<AAllocator>
DVsmRcx::error_allocator() const noexcept
{
return vsm_->error_allocator();
}
} /*namespace scm*/
} /*namespace xo*/

View file

@ -82,6 +82,7 @@ namespace xo {
VirtualSchematikaMachine::visit_pools(const MemorySizeVisitor & visitor) const
{
mm_.visit_pools(visitor);
error_mm_.visit_pools(visitor);
reader_.visit_pools(visitor);
}

View file

@ -6,9 +6,8 @@
#include <xo/interpreter2/VirtualSchematikaMachine.hpp>
#include <xo/interpreter2/Closure.hpp>
#include <xo/object2/Float.hpp>
//#include <xo/object2/number/IGCObject_DFloat.hpp>
#include <xo/object2/Integer.hpp>
//#include <xo/object2/number/IGCObject_DInteger.hpp>
#include <xo/object2/RuntimeError.hpp>
#ifdef NOT_YET
#include <xo/reader2/SchematikaParser.hpp>
@ -32,6 +31,7 @@ namespace xo {
using xo::scm::DClosure;
using xo::scm::DFloat;
using xo::scm::DInteger;
using xo::scm::DRuntimeError;
using xo::mm::AGCObject;
using xo::mm::MemorySizeInfo;
using xo::facet::FacetRegistry;
@ -245,9 +245,13 @@ namespace xo {
log && log(xtag("res.tseq", res.value()->_typeseq()));
auto x = obj<AGCObject,DClosure>::from(*res.value());
// currently get not-implemented error
auto x = obj<AGCObject,DRuntimeError>::from(*res.value());
REQUIRE(x);
log && log("runtime-error", xtag("ex.src", x->src_function()), xtag("ex.descr", x->error_descr()));
//REQUIRE(x.data()->value() == 1.570796325);
REQUIRE(res.remaining_.size() == 1);

View file

@ -6,6 +6,7 @@
#pragma once
#include "String.hpp"
#include <xo/facet/obj.hpp>
#include <xo/alloc2/Allocator.hpp>
namespace xo {
@ -15,20 +16,29 @@ namespace xo {
**/
class DRuntimeError {
public:
using AGCObject = xo::mm::AGCObject;
using ACollector = xo::mm::ACollector;
using AAllocator = xo::mm::AAllocator;
using ppindentinfo = xo::print::ppindentinfo;
public:
/** convenience shortcut.**/
static obj<AGCObject,DRuntimeError> make(obj<AAllocator> mm,
const char * src_fn,
const char * error_descr);
/** create instance using memory from allocator @p mm
* @p src_fn identifies the (c++) function/method in which
* error detercted.
* @p error_descr contains human-readable error message;
* will be copied by this function.
**/
DRuntimeError * _make(obj<AAllocator> mm,
DString * src_fn,
DString * error_descr);
static DRuntimeError * _make(obj<AAllocator> mm,
DString * src_fn,
DString * error_descr);
DString * src_function() const noexcept { return src_function_; }
DString * error_descr() const noexcept { return error_descr_; }
/** @defgroup scm-runtimeerror-printable-facet printable facet **/
///@{

View file

@ -3,7 +3,7 @@
* @author Roland Conybeare, Feb 2026
**/
#include "DRuntimeError.hpp"
#include "RuntimeError.hpp"
namespace xo {
using xo::mm::AGCObject;
@ -11,6 +11,24 @@ namespace xo {
namespace scm {
obj<AGCObject,DRuntimeError>
DRuntimeError::make(obj<AAllocator> mm,
const char * src_fn,
const char * error_descr)
{
DRuntimeError * err = DRuntimeError::_make(mm, nullptr, nullptr);
// pedantic: allocate strings after allocating DRuntimeError instance
DString * src = DString::from_cstr(mm, src_fn);
DString * err_descr = DString::from_cstr(mm, error_descr);
err->src_function_ = src;
err->error_descr_ = err_descr;
return obj<AGCObject,DRuntimeError>(err);
}
DRuntimeError *
DRuntimeError::_make(obj<AAllocator> mm,
DString * src_fn,