xo-interpreter: expose builtin primitives to interpreter

This commit is contained in:
Roland Conybeare 2025-11-29 11:39:34 -05:00
commit 212a1fdc8c
6 changed files with 135 additions and 44 deletions

View file

@ -3,7 +3,11 @@
* author: Roland Conybeare, Aug 2025
*/
#pragma once
#include "xo/alloc/Object.hpp"
#include "ObjectConversion.hpp"
#include "xo/indentlog/print/tag.hpp"
namespace xo {
namespace obj {
@ -34,6 +38,24 @@ namespace xo {
private:
bool value_;
};
template <typename BoolType>
struct ObjectConversion_Boolean {
static gp<Object> to_object(gc::IAlloc * /*mm*/, BoolType x) {
return Boolean::boolean_obj(x);
}
static BoolType from_object(gc::IAlloc *, gp<Object> x) {
gp<Boolean> x_bool = Boolean::from(x);
if (x_bool.get()) {
return x_bool->value();
} else {
throw std::runtime_error(tostr("ObjectConversion_Boolean: x found where Boolean expected", xtag("x", x)));
}
}
};
template <>
struct ObjectConversion<bool> : public ObjectConversion_Boolean<bool> {};
}
}

View file

@ -6,6 +6,8 @@
#pragma once
#include "Number.hpp"
#include "ObjectConversion.hpp"
#include "xo/indentlog/print/tag.hpp"
namespace xo {
namespace obj {
@ -35,6 +37,26 @@ namespace xo {
private:
float_type value_ = 0.0;
};
template <typename FloatType>
struct ObjectConversion_Float {
static gp<Object> to_object(gc::IAlloc * mm, FloatType x) {
return new (MMPtr(mm)) Float(x);
}
static FloatType from_object(gc::IAlloc *, gp<Object> x) {
gp<Float> x_int = Float::from(x);
if (x_int.get()) {
return x_int->value();
} else {
throw std::runtime_error(tostr("ObjectConversion_Float: x found where Float expected", xtag("x", x)));
}
}
};
template <>
struct ObjectConversion<double> : public ObjectConversion_Float<double> {};
template <>
struct ObjectConversion<float> : public ObjectConversion_Float<float> {};
}
}

View file

@ -14,6 +14,16 @@ namespace xo {
static gp<Object> to_object(gc::IAlloc * mm, const T & x) = delete;
static T from_object(gc::IAlloc * mm, gp<Object> x) = delete;
};
/** see specializations:
* ObjectConversion<bool>
* in object/Boolean.hpp
*
* ObjectConversion<int64_t>
* ObjectConversion<int32_t>
* ObjectConversion<int16_t>
* in object/Integer.hpp
**/
}
}

View file

@ -6,7 +6,9 @@
#pragma once
#include "Procedure.hpp"
#include "Float.hpp"
#include "String.hpp"
#include "Boolean.hpp"
#include "ObjectConversion.hpp"
#include "xo/reflect/Reflect.hpp"