From 57ef654a55aafcab1216f3cb025b989745cf4271 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sun, 23 Nov 2025 12:38:28 -0500 Subject: [PATCH] xo-interpreter: + xo::scm::Float --- .../interpreter/VirtualSchematikaMachine.cpp | 2 + xo-object/include/xo/object/Float.hpp | 41 +++++++++++++ xo-object/src/object/CMakeLists.txt | 4 +- xo-object/src/object/Float.cpp | 57 +++++++++++++++++++ xo-object/src/object/Integer.cpp | 2 + xo-object/src/object/ObjectConverter.cpp | 16 +++++- 6 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 xo-object/include/xo/object/Float.hpp create mode 100644 xo-object/src/object/Float.cpp diff --git a/xo-interpreter/src/interpreter/VirtualSchematikaMachine.cpp b/xo-interpreter/src/interpreter/VirtualSchematikaMachine.cpp index 818deb13..915f138c 100644 --- a/xo-interpreter/src/interpreter/VirtualSchematikaMachine.cpp +++ b/xo-interpreter/src/interpreter/VirtualSchematikaMachine.cpp @@ -145,6 +145,8 @@ namespace xo { VSM_CONTINUE(); } else { + /* see ObjectConverter::ctor to add more builtin types */ + VSM_ERROR(tostr("constant_op: unable to convert native value to object", xtag("id", expr->value_tp().td()->id()), xtag("short_name", expr->value_tp().td()->short_name()))); diff --git a/xo-object/include/xo/object/Float.hpp b/xo-object/include/xo/object/Float.hpp new file mode 100644 index 00000000..7ca1a8c0 --- /dev/null +++ b/xo-object/include/xo/object/Float.hpp @@ -0,0 +1,41 @@ +/** @file Float.hpp + * + * author: Roland Conybeare, Nov 2025 + **/ + +#pragma once + +#include "Number.hpp" + +namespace xo { + namespace obj { + class Float : public Number { + public: + using IAlloc = xo::gc::IAlloc; + using float_type = double; + + public: + Float() = default; + explicit Float(float_type x); + + /** create instance holding floating-point value @p x **/ + static gp make(IAlloc * mm, float_type x); + /** downcast from @p x iff x is actually a Float. Otherwise nullptr **/ + static gp from(gp x); + + float_type value() const { return value_; } + + // inherited from Object.. + virtual TaggedPtr self_tp() const final override; + virtual void display(std::ostream & os) const final override; + virtual std::size_t _shallow_size() const final override; + virtual Object * _shallow_copy() const final override; + virtual std::size_t _forward_children() final override; + + private: + float_type value_ = 0.0; + }; + } +} + +/* end Float.hpp */ diff --git a/xo-object/src/object/CMakeLists.txt b/xo-object/src/object/CMakeLists.txt index 0924aa03..855996e0 100644 --- a/xo-object/src/object/CMakeLists.txt +++ b/xo-object/src/object/CMakeLists.txt @@ -6,7 +6,9 @@ set(SELF_SRCS Boolean.cpp String.cpp List.cpp - Integer.cpp) + Integer.cpp + Float.cpp +) xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS}) xo_headeronly_dependency(${SELF_LIB} xo_reflectutil) diff --git a/xo-object/src/object/Float.cpp b/xo-object/src/object/Float.cpp new file mode 100644 index 00000000..b723dbfd --- /dev/null +++ b/xo-object/src/object/Float.cpp @@ -0,0 +1,57 @@ +/** @file Float.cpp + * + * @author: Roland Conybeare, Nov 2025 + **/ + +#include "Float.hpp" +#include "xo/reflect/Reflect.hpp" + +namespace xo { + using xo::reflect::Reflect; + using xo::reflect::TaggedPtr; + + namespace obj { + static_assert(sizeof(Float::float_type) == 8, "expected 64-bit representation for xo::obj::Float"); + + Float::Float(float_type x) : value_{x} {} + + gp + Float::make(IAlloc * mm, float_type x) + { + return new (MMPtr(mm)) Float(x); + } + + gp + Float::from(gp x) { + return dynamic_cast(x.ptr()); + } + + TaggedPtr + Float::self_tp() const { + return Reflect::make_tp(const_cast(this)); + } + + void + Float::display(std::ostream & os) const { + os << value_; + } + + std::size_t + Float::_shallow_size() const { + return sizeof(Float); + } + + Object * + Float::_shallow_copy() const { + Cpof cpof(Object::mm, this); + return new (cpof) Float(*this); + } + + std::size_t + Float::_forward_children() { + return Float::_shallow_size(); + } + } +} + +/* end Float.cpp */ diff --git a/xo-object/src/object/Integer.cpp b/xo-object/src/object/Integer.cpp index 59775de6..4d5f663f 100644 --- a/xo-object/src/object/Integer.cpp +++ b/xo-object/src/object/Integer.cpp @@ -13,6 +13,8 @@ namespace xo { using xo::gc::IAlloc; namespace obj { + static_assert(sizeof(Integer::int_type) == 8, "expected 64-bit representation for xo::obj::Integer"); + Integer::Integer(int_type x) : value_{x} {} gp diff --git a/xo-object/src/object/ObjectConverter.cpp b/xo-object/src/object/ObjectConverter.cpp index 85ea6e98..8b211d17 100644 --- a/xo-object/src/object/ObjectConverter.cpp +++ b/xo-object/src/object/ObjectConverter.cpp @@ -5,6 +5,7 @@ #include "ObjectConverter.hpp" #include "Integer.hpp" +#include "Float.hpp" namespace xo { using xo::reflect::TaggedPtr; @@ -22,12 +23,25 @@ namespace xo { return Integer::make(mm, *native); } + + template + gp + float_to_object(IAlloc * mm, const TaggedPtr & src) + { + T * native = src.recover_native(); + + assert(native); + + return Float::make(mm, *native); + } } ObjectConverter::ObjectConverter() { this->establish_conversion(&int_to_object); - this->establish_conversion(&int_to_object); + this->establish_conversion(&int_to_object); + + this->establish_conversion(&float_to_object); } gp