xo-interpreter: + xo::scm::Float

This commit is contained in:
Roland Conybeare 2025-11-23 12:38:28 -05:00
commit 57ef654a55
6 changed files with 120 additions and 2 deletions

View file

@ -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())));

View file

@ -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<Float> make(IAlloc * mm, float_type x);
/** downcast from @p x iff x is actually a Float. Otherwise nullptr **/
static gp<Float> from(gp<Object> 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 */

View file

@ -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)

View file

@ -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>
Float::make(IAlloc * mm, float_type x)
{
return new (MMPtr(mm)) Float(x);
}
gp<Float>
Float::from(gp<Object> x) {
return dynamic_cast<Float*>(x.ptr());
}
TaggedPtr
Float::self_tp() const {
return Reflect::make_tp(const_cast<Float*>(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 */

View file

@ -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<Integer>

View file

@ -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 <typename T>
gp<Object>
float_to_object(IAlloc * mm, const TaggedPtr & src)
{
T * native = src.recover_native<T>();
assert(native);
return Float::make(mm, *native);
}
}
ObjectConverter::ObjectConverter()
{
this->establish_conversion<std::int32_t>(&int_to_object<std::int32_t>);
this->establish_conversion<std::int64_t>(&int_to_object<std::int32_t>);
this->establish_conversion<std::int64_t>(&int_to_object<std::int64_t>);
this->establish_conversion<double>(&float_to_object<double>);
}
gp<Object>