152 lines
4.2 KiB
C++
152 lines
4.2 KiB
C++
/** @file VirtualSchematikaMachine.hpp
|
|
*
|
|
* @author Roland Conybare, Jan 2026
|
|
**/
|
|
|
|
#pragma once
|
|
|
|
#include "VsmConfig.hpp"
|
|
#include "VsmInstr.hpp"
|
|
#include <xo/reader2/SchematikaReader.hpp>
|
|
#include <xo/expression2/Expression.hpp>
|
|
#include <xo/gc/GCObject.hpp>
|
|
#include <xo/facet/box.hpp>
|
|
|
|
namespace xo {
|
|
namespace scm {
|
|
/** similar to @ref xo::scm::ReaderResult **/
|
|
struct VsmResult {
|
|
using AGCObject = xo::mm::AGCObject;
|
|
using span_type = xo::mm::span<const char>;
|
|
|
|
bool is_tk_error() const { return tk_error_.is_error(); }
|
|
|
|
/** result of evaluating first expression encountered in input **/
|
|
obj<AGCObject> value_;
|
|
|
|
/** unconsumed portion of input span **/
|
|
span_type remaining_input_;
|
|
|
|
/** {src_function, error_description, input_state, error_pos} **/
|
|
TokenizerError tk_error_;
|
|
};
|
|
|
|
/** @class VirtualSchematikaMachine
|
|
* @brief virtual machine for schematika
|
|
**/
|
|
class VirtualSchematikaMachine {
|
|
public:
|
|
// will be DArenaVector<obj<StackFrame>> probably
|
|
using Stack = void *;
|
|
using AAllocator = xo::mm::AAllocator;
|
|
using AGCObject = xo::mm::AGCObject;
|
|
using span_type = xo::mm::span<const char>;
|
|
|
|
public:
|
|
VirtualSchematikaMachine(const VsmConfig & config);
|
|
|
|
/** consume input @p input_cstr **/
|
|
VsmResult read_eval_print(span_type input_span, bool eof);
|
|
|
|
/** evaluate expression @p expr **/
|
|
std::pair<obj<AGCObject>, TokenizerError> eval(obj<AExpression> expr);
|
|
|
|
/** borrow calling thread to run indefinitely,
|
|
* until halt instruction
|
|
**/
|
|
void run();
|
|
|
|
/** execute vsm instruction in @ref pc_.
|
|
* @retval instruction count. 1 unless pc_ is halt.
|
|
**/
|
|
bool execute_one();
|
|
|
|
private:
|
|
/** Require:
|
|
* - expression in @ref expr_
|
|
**/
|
|
void _do_eval_op();
|
|
|
|
/** evaluate a constant expression
|
|
* Require:
|
|
* - expression in @ref expr_
|
|
**/
|
|
void _do_eval_constant_op();
|
|
|
|
/** evaluate a define-expression
|
|
* Require:
|
|
* - expression in @ref expr_
|
|
**/
|
|
void _do_eval_define_op();
|
|
|
|
/** evaluate a lambda expression
|
|
* Require:
|
|
* - expression in @ref expr_
|
|
**/
|
|
void _do_eval_lambda_op();
|
|
|
|
/** evaluate a variable expression
|
|
* Require:
|
|
* - expression in @ref expr_
|
|
**/
|
|
void _do_eval_variable_op();
|
|
|
|
/** evaluate an apply expression
|
|
* Require:
|
|
* - expression in @ref expr_
|
|
**/
|
|
void _do_eval_apply_op();
|
|
|
|
/** evaluate an if-else expression
|
|
* Require:
|
|
* - expression in @ref expr_
|
|
**/
|
|
void _do_eval_if_else_op();
|
|
|
|
/** evaluate a sequence expression
|
|
* Require:
|
|
* - expression in @ref expr_
|
|
**/
|
|
void _do_eval_sequence_op();
|
|
|
|
private:
|
|
/*
|
|
* Some registers are preserved by evaluation:
|
|
* stack_
|
|
* cont_
|
|
*
|
|
* Other registers are not preserved
|
|
* pc_
|
|
* expr_
|
|
* value_
|
|
*/
|
|
|
|
/** configuration **/
|
|
VsmConfig config_;
|
|
|
|
box<AAllocator> mm_;
|
|
|
|
/** reader: text -> expression **/
|
|
SchematikaReader reader_;
|
|
|
|
/** program counter **/
|
|
VsmInstr pc_ = VsmInstr::halt();
|
|
|
|
#ifdef NOT_YET
|
|
/** stack pointer **/
|
|
Stack stack_;
|
|
#endif
|
|
|
|
/** expression register **/
|
|
obj<AExpression> expr_;
|
|
|
|
/** result register **/
|
|
obj<AGCObject> value_;
|
|
|
|
/** continuation register **/
|
|
VsmInstr cont_ = VsmInstr::halt();
|
|
};
|
|
} /*namespace scm*/
|
|
} /*namespace xo*/
|
|
|
|
/* end VirtualSchematikaMachine.hpp */
|