xo-interpreter: + VSM work in progress

This commit is contained in:
Roland Conybeare 2026-02-03 21:44:40 -05:00
commit dc88663271
10 changed files with 307 additions and 7 deletions

View file

@ -0,0 +1,20 @@
/** @file DApplyFrame.hpp
*
* @author Roland Conyberae, Feb 2026
**/
#pragma once
namespace xo {
namespace scm {
/** In virtual schematika machine (VSM):
* stack frame for interpreted apply expression
* (@ref DApplyExpr)
**/
class DApplyFrame {
obj<AGCObject>
};
}
}
/* end DApplyFrame.hpp */

View file

@ -7,6 +7,7 @@
#include "VsmConfig.hpp"
#include "VsmInstr.hpp"
#include "VsmFrame.hpp"
#include <xo/reader2/SchematikaReader.hpp>
#include <xo/expression2/Expression.hpp>
#include <xo/gc/GCObject.hpp>
@ -143,6 +144,12 @@ namespace xo {
**/
void _do_eval_sequence_op();
/** apply a function to evaluated arguments **/
void _do_apply_op();
/** evaluate arguments on behalf of a function call **/
void _do_evalargs_op();
private:
/*
* Some registers are preserved by evaluation:
@ -163,16 +170,18 @@ namespace xo {
**/
box<AAllocator> mm_;
// consider separate allocator (which _may_ turn out to be the same)
// for VM stack. Only works for code that doesn't rely on fancy
// lexical scoping
/** reader: text -> expression **/
SchematikaReader reader_;
/** program counter **/
VsmInstr pc_ = VsmInstr::c_halt;
#ifdef NOT_YET
/** stack pointer **/
Stack stack_;
#endif
VsmFrame * stack_ = nullptr;
/** expression register **/
obj<AExpression> expr_;

View file

@ -0,0 +1,86 @@
/** @file VsmFrame.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include "VsmInstr.hpp"
#include <xo/procedure2/Procedure.hpp>
#include <xo/object2/DArray.hpp>
namespace xo {
namespace scm {
class VsmFrame {
public:
VsmFrame(VsmFrame * parent, VsmInstr cont) : parent_{parent}, cont_{cont} {}
VsmFrame * parent() const noexcept { return parent_; }
VsmInstr cont() const noexcept { return cont_; }
protected:
/** saved VSM stack; restore when this frame consumed **/
VsmFrame * parent_ = nullptr;
/** saved continuation; restore when this frame consumed **/
VsmInstr cont_;
};
class VsmApplyFrame : public VsmFrame {
public:
using AProcedure = xo::scm::AProcedure;
using AAllocator = xo::mm::AAllocator;
public:
VsmApplyFrame(VsmFrame * old_parent,
VsmInstr old_cont,
DArray * args);
static VsmApplyFrame * make(obj<AAllocator> mm,
VsmFrame * old_parent,
VsmInstr old_cont,
DArray * args);
obj<AProcedure> fn() const noexcept { return fn_; }
DArray * args() const noexcept { return args_; }
private:
/** evaluated target procedure.
*
* note: when initially created, this will be unpopulated;
* don't know correct value until we evaluate
* expression in head position
**/
obj<AProcedure> fn_;
/** evaluated arguments (to target procedure) **/
DArray * args_;
};
/** frame for executing an apply expression **/
class VsmEvalArgsFrame : public VsmFrame {
public:
using AAllocator = xo::mm::AAllocator;
public:
/** see picture in VirtualSchematikaMachine._do_eval_apply_op()
*
* old_parent = [apply frame]
* old_cont = [xfer to called function]
*
**/
VsmEvalArgsFrame(VsmApplyFrame * old_parent,
VsmInstr old_cont);
static VsmEvalArgsFrame * make(obj<AAllocator> mm,
VsmApplyFrame * apply_frame,
VsmInstr old_cont);
private:
/** next argument to be evaluated. -1 means function head **/
int32_t i_arg_ = -1;
};
} /*namespace scm*/
} /*namespace xo*/
/* end VsmFrame.hpp */

View file

@ -16,6 +16,9 @@ namespace xo {
static VsmInstr c_halt;
static VsmInstr c_eval;
static VsmInstr c_apply;
static VsmInstr c_evalargs;
vsm_opcode opcode() const noexcept { return opcode_; }
private:

View file

@ -18,6 +18,15 @@ namespace xo {
/** Evaluate expression in expr register **/
eval,
/** Apply function in stack frame
* See diagram in VirtualSchematikaMachine::_do_eval_apply_op
**/
apply,
/** Eval arguments to function.
* See diagram in VirtualSchematikaMachine::_do_eval_apply_op
**/
evalargs,
/** sentinel, counts number of opcodes **/
N,
};