xo-interpreter2 stack: refactor + bugfix operator expr
This commit is contained in:
parent
cc42c98928
commit
3dc6268dfe
20 changed files with 614 additions and 230 deletions
94
include/xo/reader2/DGlobalEnv.hpp
Normal file
94
include/xo/reader2/DGlobalEnv.hpp
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/** @file DGlobalEnv.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <xo/expression2/DGlobalSymtab.hpp>
|
||||
#include <xo/object2/DArray.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
/** @brief runtime bindings for global variabels
|
||||
*
|
||||
* Implementation here uses a DArenaHashMap to hold <key,value> pairs.
|
||||
* The hash map has its own memory outside GC space.
|
||||
* Keys are DUniqueStrings, also outside GC space.
|
||||
* Values are regular gc-aware objects, generally will be in GC space.
|
||||
*
|
||||
* We need collector to traverse all the values in a global env
|
||||
* on each cycle. Arrange that by having DGlobalEnv itself
|
||||
* in GC space.
|
||||
*
|
||||
**/
|
||||
class DGlobalEnv {
|
||||
public:
|
||||
using TypeDescr = xo::reflect::TypeDescr;
|
||||
using ACollector = xo::mm::ACollector;
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
using MemorySizeVisitor = xo::mm::MemorySizeVisitor;
|
||||
using ppindentinfo = xo::print::ppindentinfo;
|
||||
using size_type = std::uint32_t;
|
||||
|
||||
public:
|
||||
/** @defgroup scm-globalenv-ctors constructors **/
|
||||
///@{
|
||||
|
||||
DGlobalEnv(DGlobalSymtab * symtab, DArray * values);
|
||||
|
||||
static DGlobalEnv * _make(obj<AAllocator> mm,
|
||||
DGlobalSymtab * symtab);
|
||||
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-globalenv-methods methods **/
|
||||
///@{
|
||||
|
||||
/** symbol-table size. Is the number of distinct global variables **/
|
||||
size_type n_vars() const noexcept { return symtab_->n_vars(); }
|
||||
|
||||
/** lookup current value associated with binding @p ix **/
|
||||
obj<AGCObject> lookup_value(Binding ix) const noexcept;
|
||||
|
||||
/** assign value associated with binding @p to @p x.
|
||||
* If need to expand size of this env, use memory from @p mm
|
||||
**/
|
||||
void assign_value(obj<AAllocator> mm, Binding ix, obj<AGCObject> x);
|
||||
|
||||
/** create/establish global for symbol @p sym with resolved type @p td
|
||||
* and associate with @p value.
|
||||
**/
|
||||
DVariable * _upsert_value(obj<AAllocator> mm,
|
||||
const DUniqueString * sym,
|
||||
TypeDescr td,
|
||||
obj<AGCObject> value);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-globalenv-gcobject-facet **/
|
||||
///@{
|
||||
|
||||
std::size_t shallow_size() const noexcept;
|
||||
DGlobalEnv * shallow_copy(obj<AAllocator> mm) const noexcept;
|
||||
std::size_t forward_children(obj<ACollector> gc) noexcept;
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-globalenv-printable-facet **/
|
||||
///@{
|
||||
|
||||
bool pretty(const ppindentinfo & ppii) const;
|
||||
|
||||
///@}
|
||||
|
||||
private:
|
||||
|
||||
/** symbol table assigns a unique index for each symbol **/
|
||||
DGlobalSymtab * symtab_;
|
||||
|
||||
/** value for a symbol S will be in values_[symtab->lookup_binding(S)] **/
|
||||
DArray * values_ = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
12
include/xo/reader2/GlobalEnv.hpp
Normal file
12
include/xo/reader2/GlobalEnv.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/** @file GlobalEnv.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "DGlobalEnv.hpp"
|
||||
#include "env/IGCObject_DGlobalEnv.hpp"
|
||||
#include "env/IPrintable_DGlobalEnv.hpp"
|
||||
|
||||
/* end GlobalEnv.hpp */
|
||||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <xo/procedure2/PrimitiveRegistry.hpp>
|
||||
#include <xo/arena/ArenaHashMapConfig.hpp>
|
||||
#include <xo/arena/ArenaConfig.hpp>
|
||||
|
||||
|
|
@ -55,6 +56,9 @@ namespace xo {
|
|||
/** max capacity for unique string table **/
|
||||
size_t max_stringtable_capacity_ = 4096;
|
||||
|
||||
/** flags controlling which primitives to install **/
|
||||
InstallFlags pm_install_flags_ = InstallFlags::f_all;
|
||||
|
||||
/** control SchematikaParser debug logging **/
|
||||
bool debug_flag_ = false;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -6,11 +6,13 @@
|
|||
#pragma once
|
||||
|
||||
#include "ParserResult.hpp"
|
||||
#include "GlobalEnv.hpp"
|
||||
#include <xo/expression2/DGlobalSymtab.hpp>
|
||||
#include <xo/expression2/DLocalSymtab.hpp>
|
||||
#include <xo/expression2/DVariable.hpp>
|
||||
#include <xo/expression2/VarRef.hpp>
|
||||
#include <xo/tokenizer2/Token.hpp>
|
||||
#include <xo/procedure2/PrimitiveRegistry.hpp>
|
||||
#include <xo/type/Type.hpp>
|
||||
#include <xo/object2/DArray.hpp>
|
||||
#include <xo/stringtable2/StringTable.hpp>
|
||||
|
|
@ -45,6 +47,9 @@ namespace xo {
|
|||
using size_type = std::size_t;
|
||||
|
||||
public:
|
||||
/** @defgroup scm-parserstatemachine-ctors constructors **/
|
||||
///@{
|
||||
|
||||
/**
|
||||
* @p config arena configuration for parser state
|
||||
* @p symtab_var_config configuration for global symtab variables
|
||||
|
|
@ -53,6 +58,8 @@ namespace xo {
|
|||
* (maps to separate dedicated memory)
|
||||
* @p max_stringtable_capacity
|
||||
* hard max size for unique stringtable
|
||||
* @p pm_install_flags
|
||||
* flags controlling primitives to install
|
||||
* @p expr_alloc allocator for schematika expressions.
|
||||
* Probably shared with execution.
|
||||
* @p aux_alloc auxiliary allocator for non-copyable memory
|
||||
|
|
@ -64,12 +71,17 @@ namespace xo {
|
|||
const ArenaHashMapConfig & symtab_var_config,
|
||||
const ArenaHashMapConfig & symtab_type_config,
|
||||
size_type max_stringtable_capacity,
|
||||
InstallFlags pm_install_flags,
|
||||
obj<AAllocator> expr_alloc,
|
||||
obj<AAllocator> aux_alloc);
|
||||
|
||||
/** non-trivial dtor for @ref global_symtab_ **/
|
||||
~ParserStateMachine() = default;
|
||||
/** not copyable (need to put global_env into gc **/
|
||||
ParserStateMachine(const ParserStateMachine & other) = delete;
|
||||
|
||||
/** non-trivial dtor for @ref global_symtab_ **/
|
||||
~ParserStateMachine();
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-parserstatemachine-accessors accessor methods **/
|
||||
///@{
|
||||
|
||||
|
|
@ -80,6 +92,21 @@ namespace xo {
|
|||
DLocalSymtab * local_symtab() const noexcept { return local_symtab_; }
|
||||
const ParserResult & result() const noexcept { return result_; }
|
||||
|
||||
/** polymoprhihc multiply primitive. Use to implement infix op* **/
|
||||
obj<AGCObject> multiply_pm() const;
|
||||
/** polymorphic divide primitive. Use to implement infix op/ **/
|
||||
obj<AGCObject> divide_pm() const;
|
||||
/** polymorphic add primitive. Use to implement infix op+ **/
|
||||
obj<AGCObject> add_pm() const;
|
||||
/** polymorphic subtract primitive. Use to implement infix op- **/
|
||||
obj<AGCObject> subtract_pm() const;
|
||||
/** polymorphic equality comparison. Use to implement infix op== **/
|
||||
obj<AGCObject> cmpeq_pm() const;
|
||||
/** polymorphic inequality comparison. Use to implement infix op!= **/
|
||||
obj<AGCObject> cmpne_pm() const;
|
||||
/** polymorphich less-than comparison. Use to implement infix op< **/
|
||||
obj<AGCObject> cmplt_pm() const;
|
||||
|
||||
/** true iff state machine is currently idle (at top-level) **/
|
||||
bool is_at_toplevel() const noexcept;
|
||||
|
||||
|
|
@ -378,6 +405,20 @@ namespace xo {
|
|||
**/
|
||||
DLocalSymtab * local_symtab_ = nullptr;
|
||||
|
||||
/** global variable bindings (builtin primitives) **/
|
||||
obj<AGCObject,DGlobalEnv> global_env_;
|
||||
|
||||
/** bindings for special builtin primitives
|
||||
* (asociated with hardwired operator syntax)
|
||||
**/
|
||||
Binding multiply_binding_;
|
||||
Binding divide_binding_;
|
||||
Binding add_binding_;
|
||||
Binding subtract_binding_;
|
||||
Binding cmpeq_binding_;
|
||||
Binding cmpne_binding_;
|
||||
Binding cmplt_binding_;
|
||||
|
||||
/** current output from parser **/
|
||||
ParserResult result_;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <xo/procedure2/PrimitiveRegistry.hpp>
|
||||
#include <xo/arena/CircularBufferConfig.hpp>
|
||||
#include <xo/arena/ArenaHashMapConfig.hpp>
|
||||
#include <xo/arena/ArenaConfig.hpp>
|
||||
|
|
@ -71,6 +72,9 @@ namespace xo {
|
|||
/** max size (in bytes) of stringtable **/
|
||||
size_t max_stringtable_cap_ = 64*1024;
|
||||
|
||||
/** flags controlling which primitives to install **/
|
||||
InstallFlags pm_install_flags_ = InstallFlags::f_all;
|
||||
|
||||
/** debug flag for schematika_reader **/
|
||||
bool reader_debug_flag_ = false;
|
||||
};
|
||||
|
|
|
|||
67
include/xo/reader2/env/IGCObject_DGlobalEnv.hpp
vendored
Normal file
67
include/xo/reader2/env/IGCObject_DGlobalEnv.hpp
vendored
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/** @file IGCObject_DGlobalEnv.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/IGCObject_DGlobalEnv.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/IGCObject_DGlobalEnv.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "GCObject.hpp"
|
||||
#include <xo/alloc2/GCObject.hpp>
|
||||
#include <xo/alloc2/Allocator.hpp>
|
||||
#include "DGlobalEnv.hpp"
|
||||
|
||||
namespace xo { namespace scm { class IGCObject_DGlobalEnv; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::mm::AGCObject,
|
||||
xo::scm::DGlobalEnv>
|
||||
{
|
||||
using ImplType = xo::mm::IGCObject_Xfer
|
||||
<xo::scm::DGlobalEnv,
|
||||
xo::scm::IGCObject_DGlobalEnv>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class IGCObject_DGlobalEnv
|
||||
**/
|
||||
class IGCObject_DGlobalEnv {
|
||||
public:
|
||||
/** @defgroup scm-gcobject-dglobalenv-type-traits **/
|
||||
///@{
|
||||
using size_type = xo::mm::AGCObject::size_type;
|
||||
using AAllocator = xo::mm::AGCObject::AAllocator;
|
||||
using ACollector = xo::mm::AGCObject::ACollector;
|
||||
using Copaque = xo::mm::AGCObject::Copaque;
|
||||
using Opaque = xo::mm::AGCObject::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-gcobject-dglobalenv-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** memory consumption for this instance **/
|
||||
static size_type shallow_size(const DGlobalEnv & self) noexcept;
|
||||
/** copy instance using allocator **/
|
||||
static Opaque shallow_copy(const DGlobalEnv & self, obj<AAllocator> mm) noexcept;
|
||||
|
||||
// non-const methods
|
||||
/** during GC: forward immdiate children **/
|
||||
static size_type forward_children(DGlobalEnv & self, obj<ACollector> gc) noexcept;
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
62
include/xo/reader2/env/IPrintable_DGlobalEnv.hpp
vendored
Normal file
62
include/xo/reader2/env/IPrintable_DGlobalEnv.hpp
vendored
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/** @file IPrintable_DGlobalEnv.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/IPrintable_DGlobalEnv.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/IPrintable_DGlobalEnv.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Printable.hpp"
|
||||
#include <xo/printable2/Printable.hpp>
|
||||
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
|
||||
#include "DGlobalEnv.hpp"
|
||||
|
||||
namespace xo { namespace scm { class IPrintable_DGlobalEnv; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::print::APrintable,
|
||||
xo::scm::DGlobalEnv>
|
||||
{
|
||||
using ImplType = xo::print::IPrintable_Xfer
|
||||
<xo::scm::DGlobalEnv,
|
||||
xo::scm::IPrintable_DGlobalEnv>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class IPrintable_DGlobalEnv
|
||||
**/
|
||||
class IPrintable_DGlobalEnv {
|
||||
public:
|
||||
/** @defgroup scm-printable-dglobalenv-type-traits **/
|
||||
///@{
|
||||
using ppindentinfo = xo::print::APrintable::ppindentinfo;
|
||||
using Copaque = xo::print::APrintable::Copaque;
|
||||
using Opaque = xo::print::APrintable::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-printable-dglobalenv-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** Pretty-printing support for this object.
|
||||
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
|
||||
static bool pretty(const DGlobalEnv & self, const ppindentinfo & ppii);
|
||||
|
||||
// non-const methods
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
Loading…
Add table
Add a link
Reference in a new issue