xo-interpreter2 stack: + dict.make() + Dictionary impl

This commit is contained in:
Roland Conybeare 2026-03-03 16:45:54 +11:00
commit 7871c7dbea
16 changed files with 904 additions and 23 deletions

View file

@ -37,6 +37,10 @@ set(SELF_SRCS
IGCObject_DString.cpp
IPrintable_DString.cpp
DDictionary.cpp
IGCObject_DDictionary.cpp
IPrintable_DDictionary.cpp
DRuntimeError.cpp
IGCObject_DRuntimeError.cpp
IPrintable_DRuntimeError.cpp

View file

@ -67,6 +67,19 @@ namespace xo {
}
}
bool
DArray::assign_at(size_type ix, obj<AGCObject> x) noexcept
{
if (ix >= size_)
return false;
scope log(XO_DEBUG(true), "need write barrier");
this->elts_[ix] = x;
return true;
}
bool
DArray::push_back(obj<AGCObject> elt) noexcept
{

208
src/object2/DDictionary.cpp Normal file
View file

@ -0,0 +1,208 @@
/** @file DDictionary.cpp
*
* @author Roland Conybeare, Mar 2026
**/
#include "DDictionary.hpp"
#include "Array.hpp"
#include "String.hpp"
#include <xo/facet/FacetRegistry.hpp>
#include <utility>
namespace xo {
using xo::print::APrintable;
using xo::facet::FacetRegistry;
using xo::mm::AGCObject;
namespace scm {
DDictionary::DDictionary(DArray * keys,
DArray * values)
: keys_{keys}, values_{values}
{}
DDictionary *
DDictionary::empty(obj<AAllocator> mm, size_type cap)
{
void * mem = mm.alloc_for<DDictionary>();
if (cap <= 0)
cap = 1;
DArray * keys = DArray::empty(mm, cap);
DArray * values = DArray::empty(mm, cap);
return new (mem) DDictionary(keys, values);
}
std::pair<const DString *, obj<AGCObject>>
DDictionary::at_index(size_type ix) const
{
if (ix < keys_->size()) {
auto key_str = obj<AGCObject,DString>::from((*keys_)[ix]);
assert(key_str);
return pair_type(this->key_at_index(ix), (*values_)[ix]);
}
return pair_type();
}
const DString *
DDictionary::key_at_index(size_type ix) const
{
auto key_str = obj<AGCObject,DString>::from((*keys_)[ix]);
assert(key_str);
return key_str.data();
}
obj<AGCObject>
DDictionary::value_at_index(size_type ix) const
{
if (ix < keys_->size()) {
assert(ix < values_->size());
return (*values_)[ix];
}
return obj<AGCObject>();
}
bool
DDictionary::try_update(const pair_type & kv_pair)
{
for (size_type i = 0, n = keys_->size(); i < n; ++i) {
auto key_i = obj<AGCObject,DString>::from((*keys_)[i]);
assert(key_i);
if (*(key_i.data()) == *(kv_pair.first)) {
values_->assign_at(i, kv_pair.second);
return true;
}
}
return false;
}
bool
DDictionary::try_upsert(const pair_type & kv_pair)
{
if (this->try_update(kv_pair))
return true;
if (keys_->size() == keys_->capacity())
return false;
keys_->push_back(obj<AGCObject,DString>(const_cast<DString *>(kv_pair.first)));
values_->push_back(kv_pair.second);
return true;
}
bool
DDictionary::upsert(obj<AAllocator> mm, const pair_type & kv_pair)
{
if (this->try_update(kv_pair))
return true;
if (keys_->size() == keys_->capacity()) {
assert(keys_->capacity() > 0);
size_type cap_2x = 2 * keys_->capacity();
DArray * keys_2x = DArray::copy(mm, keys_, cap_2x);
DArray * values_2x = DArray::copy(mm, values_, cap_2x);
if (keys_2x && values_2x) {
this->keys_ = keys_2x;
this->values_ = values_2x;
} else {
return false;
}
}
keys_->push_back(obj<AGCObject,DString>(const_cast<DString *>(kv_pair.first)));
values_->push_back(kv_pair.second);
return true;
}
void
DDictionary::shrink_to_fit() noexcept
{
keys_->shrink_to_fit();
values_->shrink_to_fit();
}
// ----- printable facet ----
bool
DDictionary::pretty(const ppindentinfo & ppii) const
{
using xo::print::ppstate;
ppstate * pps = ppii.pps();
if (ppii.upto()) {
pps->write("{");
for (size_type i = 0, n = this->size(); i < n; ++i) {
if (i > 0)
pps->write(" ");
obj<APrintable> key
= FacetRegistry::instance().variant<APrintable,AGCObject>((*keys_)[i]);
obj<APrintable> value
= FacetRegistry::instance().variant<APrintable,AGCObject>((*values_)[i]);
assert(key.data());
assert(value.data());
if (!pps->print_upto(key))
return false;
pps->write(": ");
if (!pps->print_upto(value))
return false;
}
pps->write("}");
return true;
} else {
pps->write("{...}");
return false;
}
}
// ----- gcobject facet -----
std::size_t
DDictionary::shallow_size() const noexcept
{
return sizeof(DDictionary);
}
DDictionary *
DDictionary::shallow_copy(obj<AAllocator> mm) const noexcept
{
return mm.std_copy_for(this);
}
std::size_t
DDictionary::forward_children(obj<ACollector> gc) noexcept
{
gc.forward_inplace(&keys_);
gc.forward_inplace(&values_);
return this->shallow_size();
}
} /*namespace scm*/
} /*namespace xo*/
/* end DDictionary.cpp */

View file

@ -0,0 +1,39 @@
/** @file IGCObject_DDictionary.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IGCObject_DDictionary.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IGCObject_DDictionary.json5]
**/
#include "dictionary/IGCObject_DDictionary.hpp"
namespace xo {
namespace scm {
auto
IGCObject_DDictionary::shallow_size(const DDictionary & self) noexcept -> size_type
{
return self.shallow_size();
}
auto
IGCObject_DDictionary::shallow_copy(const DDictionary & self, obj<AAllocator> mm) noexcept -> Opaque
{
return self.shallow_copy(mm);
}
auto
IGCObject_DDictionary::forward_children(DDictionary & self, obj<ACollector> gc) noexcept -> size_type
{
return self.forward_children(gc);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IGCObject_DDictionary.cpp */

View file

@ -0,0 +1,28 @@
/** @file IPrintable_DDictionary.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IPrintable_DDictionary.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IPrintable_DDictionary.json5]
**/
#include "dictionary/IPrintable_DDictionary.hpp"
namespace xo {
namespace scm {
auto
IPrintable_DDictionary::pretty(const DDictionary & self, const ppindentinfo & ppii) -> bool
{
return self.pretty(ppii);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IPrintable_DDictionary.cpp */

View file

@ -6,30 +6,14 @@
#include "object2_register_facets.hpp"
#include "RuntimeError.hpp"
#include <xo/object2/Dictionary.hpp>
#include <xo/object2/Array.hpp>
//#include <xo/object2/List.hpp>
#include <xo/object2/List.hpp>
#include <xo/object2/Boolean.hpp>
#include <xo/object2/Integer.hpp>
#include <xo/object2/Float.hpp>
#include <xo/object2/String.hpp>
//#include <xo/object2/array/IGCObject_DArray.hpp>
#include <xo/object2/list/IGCObject_DList.hpp>
//#include <xo/object2/boolean/IGCObject_DBoolean.hpp>
//#include <xo/object2/number/IGCObject_DFloat.hpp>
//#include <xo/object2/number/IGCObject_DInteger.hpp>
//#include <xo/object2/string/IGCObject_DString.hpp>
//#include <xo/object2/array/IPrintable_DArray.hpp>
#include <xo/object2/list/IPrintable_DList.hpp>
//#include <xo/object2/boolean/IPrintable_DBoolean.hpp>
//#include <xo/object2/number/IPrintable_DFloat.hpp>
//#include <xo/object2/number/IPrintable_DInteger.hpp>
//#include <xo/object2/string/IPrintable_DString.hpp>
#include <xo/object2/list/ISequence_DList.hpp>
//#include <xo/object2/array/ISequence_DArray.hpp>
#include <xo/printable2/detail/APrintable.hpp>
#include <xo/alloc2/alloc/AAllocator.hpp>
#include <xo/facet/FacetRegistry.hpp>
@ -74,6 +58,9 @@ namespace xo {
FacetRegistry::register_impl<APrintable, DArray>();
FacetRegistry::register_impl<ASequence, DArray>();
FacetRegistry::register_impl<AGCObject, DDictionary>();
FacetRegistry::register_impl<APrintable, DDictionary>();
FacetRegistry::register_impl<AGCObject, DRuntimeError>();
FacetRegistry::register_impl<APrintable, DRuntimeError>();
@ -85,6 +72,7 @@ namespace xo {
log && log(xtag("DInteger.tseq", typeseq::id<DInteger>()));
log && log(xtag("DString.tseq", typeseq::id<DString>()));
log && log(xtag("DArray.tseq", typeseq::id<DArray>()));
log && log(xtag("DDictionary.tseq", typeseq::id<DDictionary>()));
log && log(xtag("DRuntimeError.tseq", typeseq::id<DRuntimeError>()));
log && log(xtag("AAllocator.tseq", typeseq::id<AAllocator>()));

View file

@ -11,10 +11,7 @@
#include "string/IGCObject_DString.hpp"
#include "list/IGCObject_DList.hpp"
#include "array/IGCObject_DArray.hpp"
//#include "list/IPrintable_DList.hpp"
//#include "IPrintable_DFloat.hpp"
//#include "number/IPrintable_DInteger.hpp"
#include "dictionary/IGCObject_DDictionary.hpp"
#include <xo/facet/FacetRegistry.hpp>
#include <xo/indentlog/scope.hpp>
@ -23,7 +20,6 @@ namespace xo {
using xo::mm::ACollector;
using xo::mm::AGCObject;
using xo::facet::impl_for;
using xo::facet::typeseq;
using xo::scope;
namespace scm {
@ -46,6 +42,8 @@ namespace xo {
ok &= gc.install_type(impl_for<AGCObject, DArray>());
ok &= gc.install_type(impl_for<AGCObject, DDictionary>());
return ok;
}
}