xo-procedure2: procedure abstraction for Schematika

This commit is contained in:
Roland Conybeare 2026-01-25 10:31:06 -05:00
commit e35c85def0
19 changed files with 885 additions and 0 deletions

View file

@ -0,0 +1,41 @@
# xo-procedure2/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(xo_procedure2 VERSION 1.0)
enable_language(CXX)
include(GNUInstallDirs)
include(cmake/xo-bootstrap-macros.cmake)
xo_cxx_toplevel_options3()
# ----------------------------------------------------------------
# c++ settings
# one-time project-specific c++ flags. usually empty
set(PROJECT_CXX_FLAGS "")
add_definitions(${PROJECT_CXX_FLAGS})
# ----------------------------------------------------------------
# output targets
# note: manual target; generated code committed to git
xo_add_genfacet(
TARGET xo-procedure2-facet-procedure
FACET Procedure
INPUT idl/Procedure.json5
OUTPUT_HPP_DIR include/xo/procedure2
OUTPUT_IMPL_SUBDIR detail
OUTPUT_CPP_DIR src/procedure2
)
add_subdirectory(src/procedure2)
#add_subdirectory(utest)
# ----------------------------------------------------------------
# cmake export
#xo_export_cmake_config(${PROJECT_NAME} ${PROJECT_VERSION} ${PROJECT_NAME}Targets)
# end CMakeLists.txt

View file

@ -0,0 +1,41 @@
# ----------------------------------------------------------------
# for example:
# $ PREFIX=/usr/local # for example
# $ cmake -DCMAKE_MODULE_PATH=prefix -DCMAKE_INSTALL_PREFIX=$PREFIX -B .build
#
# will get
# CMAKE_MODULE_PATH
# from xo-cmake-config --cmake-module-path
#
# and expect .cmake macros in
# CMAKE_MODULE_PATH/xo_macros/xo_cxx.cmake
# ----------------------------------------------------------------
find_program(XO_CMAKE_CONFIG_EXECUTABLE NAMES xo-cmake-config REQUIRED)
if ("${XO_CMAKE_CONFIG_EXECUTABLE}" STREQUAL "XO_CMAKE_CONFIG_EXECUTABLE-NOT_FOUND")
message(FATAL "could not find xo-cmake-config executable")
endif()
message(STATUS "XO_CMAKE_CONFIG_EXECUTABLE=${XO_CMAKE_CONFIG_EXECUTABLE}")
if (XO_SUBMODULE_BUILD)
if (("${CMAKE_MODULE_PATH}" STREQUAL "") OR ("${CMAKE_MODULE_PATH}" STREQUAL prefix))
# local version of xo-cmake macros
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/xo-cmake/cmake")
message(STATUS "CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}")
endif()
else()
if (("${CMAKE_MODULE_PATH}" STREQUAL "") OR ("${CMAKE_MODULE_PATH}" STREQUAL prefix))
# default to typical install location for xo-project-macros
execute_process(COMMAND ${XO_CMAKE_CONFIG_EXECUTABLE} --cmake-module-path OUTPUT_VARIABLE CMAKE_MODULE_PATH)
message(STATUS "CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}")
endif()
endif()
# needs to have been installed somewhere on CMAKE_MODULE_PATH,
# (e.g. from xo-cmake with the same value for CMAKE_INSTALL_PREFIX)
#
include(xo_macros/xo_cxx)
xo_cxx_bootstrap_message()

View file

@ -0,0 +1,13 @@
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
# note: changes to find_dependency() calls here
# must coordinate with xo_dependency() calls
# in CMakeLists.txt
#
find_dependency(xo_gc)
find_dependency(subsys)
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components("@PROJECT_NAME@")

View file

@ -0,0 +1,58 @@
{
mode: "facet",
// includes in ASyntaxStateMachine.hpp
includes: [
],
// extra includes in SyntaxStateMachine.hpp, if any
user_hpp_includes: [
],
namespace1: "xo",
namespace2: "scm",
// text after includes, before ASyntaxStateMachine
pretext: [ "// {pretex} here" ],
facet: "Procedure",
detail_subdir: "detail",
brief: "abstraction for a schematika procedure i.e. something callable",
using_doxygen: true,
doc: [
"Abstraction for a schematika procedure"
],
types: [
{ name: "AGCObject",
definition: "xo::mm::AGCObject",
doc: [ "a gc-aware object" ],
},
// { name: string, doc: [ string ], definition: string },
],
const_methods: [
{
name: "is_nary",
doc: [ "true iff procedure takes n arguments" ],
return_type: "bool",
args: [],
const: true,
noexcept: true,
attributes: []
},
{
name: "n_args",
doc: ["number of arguments. -1 for n-ary" ],
return_type: "std::int32_t",
args: [],
const: true,
noexcept: true,
attributes: []
}
],
nonconst_methods: [
{
name: "apply_nocheck",
doc: ["invoke procedure; assume arguments satisfy type system" ],
return_type: "obj<AGCObject>",
args: [
{type: "obj<AAllocator>", name: "mm"},
{type: "const DArray *", name: "args"},
]
}
],
}

View file

@ -0,0 +1,94 @@
/** @file DPrimitive.hpp
*
* @author Roland Conybeare, Jan 2025
**/
#pragma once
#include <xo/object2/DArray.hpp>
#include <xo/gc/GCObjectConversion.hpp>
#include <xo/gc/GCObject.hpp>
#include <xo/alloc2/Allocator.hpp>
#include <tuple>
#include <type_traits>
#include <utility>
#include <string_view>
namespace xo {
namespace scm {
/** @brief Extract return type and argument types from a function type.
*
* Primary template (undefined) - specializations handle specific cases
**/
template <typename Fn>
struct FnTraits;
/** specialization for function pointers **/
template <typename R, typename... Args>
struct FnTraits<R(*)(Args...)> {
/** function return type **/
using return_type = R;
/** tuple type for function arguments **/
using args_tuple = std::tuple<Args...>;
/** number of arguments **/
static constexpr std::size_t n_args = sizeof...(Args);
/** arg_type<i> is the type of the i'th argument to Fn **/
template <std::size_t I>
using arg_type = std::tuple_element_t<I, args_tuple>;
};
/** specialization for function references **/
template <typename R, typename... Args>
struct FnTraits<R(&)(Args...)> : FnTraits<R(*)(Args...)> {};
/** specialization for plain function types **/
template <typename R, typename... Args>
struct FnTraits<R(Args...)> : FnTraits<R(*)(Args...)> {};
template <typename Fn>
class Primitive {
public:
using AAllocator = xo::mm::AAllocator;
using AGCObject = xo::mm::AGCObject;
using DArray = xo::scm::DArray;
using Traits = FnTraits<Fn>;
public:
Primitive(std::string_view name, Fn fn) : name_{name}, fn_{fn} {}
bool is_nary() const noexcept { return false; }
static constexpr std::int32_t n_args() noexcept { return Traits::n_args; }
obj<AGCObject> apply_nocheck(obj<AAllocator> mm, const DArray * args) {
return _apply_nocheck(mm, args,
std::make_index_sequence<Traits::n_args>{});
}
private:
template <std::size_t... Is>
obj<AGCObject> _apply_nocheck(obj<AAllocator> mm,
const DArray * args,
std::index_sequence<Is...>)
{
using R = typename Traits::return_type;
R result
= fn_(GCObjectConversion<typename Traits::template arg_type<Is>>::from_gco(mm, args->at(Is))...
);
return GCObjectConversion<R>::to_gco(mm, result);
}
private:
/** name of this primitive **/
std::string_view name_;
/** function implementation **/
Fn fn_;
}; /*Primitive*/
} /*namespace scm*/
} /*namespace xo*/
/* end DPrimitive.hpp */

View file

@ -0,0 +1,22 @@
/** @file Procedure.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/Procedure.json5]
* 2. jinja2 template for facet .hpp file:
* [facet.hpp.j2]
* 3. idl for facet methods
* [idl/Procedure.json5]
**/
#pragma once
#include "detail/AProcedure.hpp"
#include "detail/IProcedure_Any.hpp"
#include "detail/IProcedure_Xfer.hpp"
#include "detail/RProcedure.hpp"
/* end Procedure.hpp */

View file

@ -0,0 +1,77 @@
/** @file AProcedure.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/Procedure.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [abstract_facet.hpp.j2]
* 3. idl for facet methods
* [idl/Procedure.json5]
**/
#pragma once
// includes (via {facet_includes})
#include <xo/facet/obj.hpp>
#include <xo/facet/facet_implementation.hpp>
#include <xo/facet/typeseq.hpp>
// {pretex} here
namespace xo {
namespace scm {
using Copaque = const void *;
using Opaque = void *;
/**
Abstraction for a schematika procedure
**/
class AProcedure {
public:
/** @defgroup scm-procedure-type-traits **/
///@{
// types
/** integer identifying a type **/
using typeseq = xo::facet::typeseq;
using Copaque = const void *;
using Opaque = void *;
/** a gc-aware object **/
using AGCObject = xo::mm::AGCObject;
///@}
/** @defgroup scm-procedure-methods **/
///@{
// const methods
/** RTTI: unique id# for actual runtime data representation **/
virtual typeseq _typeseq() const noexcept = 0;
/** true iff procedure takes n arguments **/
virtual bool is_nary(Copaque data) const noexcept = 0;
/** number of arguments. -1 for n-ary **/
virtual std::int32_t n_args(Copaque data) const noexcept = 0;
// nonconst methods
/** invoke procedure; assume arguments satisfy type system **/
virtual obj<AGCObject> apply_nocheck(Opaque data, obj<AAllocator> mm, const DArray * args) = 0;
///@}
}; /*AProcedure*/
/** Implementation IProcedure_DRepr of AProcedure for state DRepr
* should provide a specialization:
*
* template <>
* struct xo::facet::FacetImplementation<AProcedure, DRepr> {
* using Impltype = IProcedure_DRepr;
* };
*
* then IProcedure_ImplType<DRepr> --> IProcedure_DRepr
**/
template <typename DRepr>
using IProcedure_ImplType = xo::facet::FacetImplType<AProcedure, DRepr>;
} /*namespace scm*/
} /*namespace xo*/
/* AProcedure.hpp */

View file

@ -0,0 +1,88 @@
/** @file IProcedure_Any.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/Procedure.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/Procedure.json5]
**/
#pragma once
#include "AProcedure.hpp"
#include <xo/facet/obj.hpp>
namespace xo { namespace scm { class IProcedure_Any; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::scm::AProcedure,
DVariantPlaceholder>
{
using ImplType = xo::scm::IProcedure_Any;
};
}
}
namespace xo {
namespace scm {
/** @class IProcedure_Any
* @brief AProcedure implementation for empty variant instance
**/
class IProcedure_Any : public AProcedure {
public:
/** @defgroup scm-procedure-any-type-traits **/
///@{
/** integer identifying a type **/
using typeseq = xo::facet::typeseq;
using AGCObject = AProcedure::AGCObject;
///@}
/** @defgroup scm-procedure-any-methods **/
///@{
const AProcedure * iface() const { return std::launder(this); }
// from AProcedure
// const methods
typeseq _typeseq() const noexcept override { return s_typeseq; }
[[noreturn]] bool is_nary(Copaque) const noexcept override { _fatal(); }
[[noreturn]] std::int32_t n_args(Copaque) const noexcept override { _fatal(); }
// nonconst methods
[[noreturn]] obj<AGCObject> apply_nocheck(Opaque, obj<AAllocator>, const DArray *) override;
///@}
private:
/** @defgraoup scm-procedure-any-private-methods **/
///@{
[[noreturn]] static void _fatal();
///@}
public:
/** @defgroup scm-procedure-any-member-vars **/
///@{
static typeseq s_typeseq;
static bool _valid;
///@}
};
} /*namespace scm */
} /*namespace xo */
/* IProcedure_Any.hpp */

View file

@ -0,0 +1,86 @@
/** @file IProcedure_Xfer.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/Procedure.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/Procedure.json5]
**/
#pragma once
namespace xo {
namespace scm {
/** @class IProcedure_Xfer
**/
template <typename DRepr, typename IProcedure_DRepr>
class IProcedure_Xfer : public AProcedure {
public:
/** @defgroup scm-procedure-xfer-type-traits **/
///@{
/** actual implementation (not generated; often delegates to DRepr) **/
using Impl = IProcedure_DRepr;
/** integer identifying a type **/
using typeseq = AProcedure::typeseq;
using AGCObject = AProcedure::AGCObject;
///@}
/** @defgroup scm-procedure-xfer-methods **/
///@{
static const DRepr & _dcast(Copaque d) { return *(const DRepr *)d; }
static DRepr & _dcast(Opaque d) { return *(DRepr *)d; }
// from AProcedure
// const methods
typeseq _typeseq() const noexcept override { return s_typeseq; }
bool is_nary(Copaque data) const noexcept override {
return I::is_nary(_dcast(data));
}
std::int32_t n_args(Copaque data) const noexcept override {
return I::n_args(_dcast(data));
}
// non-const methods
obj<AGCObject> apply_nocheck(Opaque data, obj<AAllocator> mm, const DArray * args) override {
return I::apply_nocheck(_dcast(data), mm, args);
}
///@}
private:
using I = Impl;
public:
/** @defgroup scm-procedure-xfer-member-vars **/
///@{
/** typeseq for template parameter DRepr **/
static typeseq s_typeseq;
/** true iff satisfies facet implementation **/
static bool _valid;
///@}
};
template <typename DRepr, typename IProcedure_DRepr>
xo::facet::typeseq
IProcedure_Xfer<DRepr, IProcedure_DRepr>::s_typeseq
= xo::facet::typeseq::id<DRepr>();
template <typename DRepr, typename IProcedure_DRepr>
bool
IProcedure_Xfer<DRepr, IProcedure_DRepr>::_valid
= xo::facet::valid_facet_implementation<AProcedure,
IProcedure_Xfer>();
} /*namespace scm */
} /*namespace xo*/
/* end IProcedure_Xfer.hpp */

View file

@ -0,0 +1,86 @@
/** @file RProcedure.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/Procedure.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/Procedure.json5]
**/
#pragma once
#include "AProcedure.hpp"
namespace xo {
namespace scm {
/** @class RProcedure
**/
template <typename Object>
class RProcedure : public Object {
private:
using O = Object;
public:
/** @defgroup scm-procedure-router-type-traits **/
///@{
using ObjectType = Object;
using DataPtr = Object::DataPtr;
using typeseq = xo::reflect::typeseq;
using AGCObject = AProcedure::AGCObject;
///@}
/** @defgroup scm-procedure-router-ctors **/
///@{
RProcedure() {}
RProcedure(Object::DataPtr data) : Object{std::move(data)} {}
RProcedure(const AProcedure * iface, void * data)
requires std::is_same_v<typename Object::DataType, xo::facet::DVariantPlaceholder>
: Object(iface, data) {}
///@}
/** @defgroup scm-procedure-router-methods **/
///@{
// const methods
typeseq _typeseq() const noexcept { return O::iface()->_typeseq(); }
bool is_nary() const noexcept {
return O::iface()->is_nary(O::data());
}
std::int32_t n_args() const noexcept {
return O::iface()->n_args(O::data());
}
// non-const methods (still const in router!)
obj<AGCObject> apply_nocheck(obj<AAllocator> mm, const DArray * args) {
return O::iface()->apply_nocheck(O::data(), mm, args);
}
///@}
/** @defgroup scm-procedure-member-vars **/
///@{
static bool _valid;
///@}
};
template <typename Object>
bool
RProcedure<Object>::_valid = xo::facet::valid_object_router<Object>();
} /*namespace scm*/
} /*namespace xo*/
namespace xo { namespace facet {
template <typename Object>
struct RoutingFor<xo::scm::AProcedure, Object> {
using RoutingType = xo::scm::RProcedure<Object>;
};
} }
/* end RProcedure.hpp */

View file

@ -0,0 +1,29 @@
/** @file init_primitives.hpp **/
#pragma once
#include "DPrimitive.hpp"
namespace xo {
namespace scm {
using Primitive_f64_1_f64 = Primitive<double (*)(double)>;
using Primitive_f64_2_f64_f64 = Primitive<double (*)(double, double)>;
struct Primitives {
static Primitive_f64_1_f64 s_neg_f64_pm;
static Primitive_f64_2_f64_f64 s_add_f64_f64_pm;
static Primitive_f64_2_f64_f64 s_sub_f64_f64_pm;
static Primitive_f64_2_f64_f64 s_mul_f64_f64_pm;
static Primitive_f64_2_f64_f64 s_div_f64_f64_pm;
static Primitive_f64_2_f64_f64 s_pow_f64_f64_pm;
static Primitive_f64_1_f64 s_log_f64_pm;
static Primitive_f64_1_f64 s_sin_f64_pm;
static Primitive_f64_1_f64 s_cos_f64_pm;
static Primitive_f64_1_f64 s_tan_f64_pm;
};
}
}
/* end init_primitives.hpp */

View file

@ -0,0 +1,21 @@
/** @file init_procedure2.hpp
*
* @author Roland Conybeare, Jan 2026
**/
#pragma once
#include <xo/subsys/Subsystem.hpp>
namespace xo {
/* tag to represent the xo-procedure2/ subsystem within ordered iniitalization */
enum S_procedure2_tag {};
template <>
struct InitSubsys<S_procedure2_tag> {
static void init();
static InitEvidence require();
};
} /*namespace xo*/
/* end init_procedure2.hpp */

View file

@ -0,0 +1,13 @@
/** @file primitives.hpp **/
#pragma once
namespace xo {
namespace scm {
struct Primitives {
static void init_primitives();
};
}
}
/* end primitives.hpp */

View file

@ -0,0 +1,26 @@
# xo-procedure2/src/CMakeLists.txt
set(SELF_LIB xo_procedure2)
set(SELF_SRCS
init_procedure2.cpp
init_primitives.cpp
DPrimitive.cpp
# Add source files here, e.g.:
# procedure2.cpp
)
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})
xo_install_include_tree3(include/xo/procedure2)
# ----------------------------------------------------------------
# input dependencies
#
# NOTE: dependency set here must be kept consistent with
# xo-procedure2/cmake/xo_procedure2Config.cmake.in
xo_dependency(${SELF_LIB} xo_object2)
xo_dependency(${SELF_LIB} xo_gc)
xo_dependency(${SELF_LIB} subsys)
#xo_dependency(${SELF_LIB} xo_indentlog)
# end src/CMakeLists.txt

View file

@ -0,0 +1,16 @@
/** @file DPrimitive.cpp
*
* @author Roland Conybeare, Jan 2026
**/
#include "DPrimitive.hpp"
#include <xo/gc/GCObject.hpp>
namespace xo {
namespace scm {
} /*namespace scm*/
} /*namespace xo*/
/* end DPrimitive.cpp */

View file

@ -0,0 +1,47 @@
/** @file IProcedure_Any.cpp
*
**/
#include "detail/IProcedure_Any.hpp"
#include <iostream>
namespace xo {
namespace scm {
using xo::facet::DVariantPlaceholder;
using xo::facet::typeseq;
using xo::facet::valid_facet_implementation;
void
IProcedure_Any::_fatal()
{
/* control here on uninitialized IAllocator_Any.
* Initialized instance will have specific implementation type
*/
std::cerr << "fatal"
<< ": attempt to call uninitialized"
<< " IProcedure_Any method"
<< std::endl;
std::terminate();
}
typeseq
IProcedure_Any::s_typeseq = typeseq::id<DVariantPlaceholder>();
bool
IProcedure_Any::_valid
= valid_facet_implementation<AProcedure, IProcedure_Any>();
// nonconst methods
auto
IProcedure_Any::apply_nocheck(Opaque, obj<AAllocator>, const DArray *) -> obj<AGCObject>
{
_fatal();
}
} /*namespace scm*/
} /*namespace xo*/
/* end IProcedure_Any.cpp */

View file

@ -0,0 +1,95 @@
/** @file init_primitives.cpp
*
* @author Roland Conybeare, Jan 2026
**/
#include "init_primitives.hpp"
#include "DPrimitive.hpp"
namespace xo {
namespace scm {
double
neg_f64(double x) {
return -x;
}
double
add_f64_f64(double x, double y) {
return x + y;
}
double
sub_f64_f64(double x, double y) {
return x - y;
}
double
mul_f64_f64(double x, double y) {
return x * y;
}
double
div_f64_f64(double x, double y) {
return x / y;
}
double
pow_f64_f64(double x, double y) {
return ::pow(x, y);
}
double
log_f64(double x) {
return ::log(x);
}
double
sin_f64(double x) {
return ::sin(x);
}
double
cos_f64(double x) {
return ::cos(x);
}
double
tan_f64(double x) {
return ::tan(x);
}
Primitive_f64_1_f64
Primitives::s_neg_f64_pm("_neg_d",
&neg_f64);
Primitive_f64_2_f64_f64
Primitives::s_add_f64_f64_pm("_add_d_d", &add_f64_f64);
Primitive_f64_2_f64_f64
Primitives::s_sub_f64_f64_pm("_sub_d_d", &sub_f64_f64);
Primitive_f64_2_f64_f64
Primitives::s_mul_f64_f64_pm("_mul_d_d", &mul_f64_f64);
Primitive_f64_2_f64_f64
Primitives::s_div_f64_f64_pm("_div_d_d", &div_f64_f64);
Primitive_f64_2_f64_f64
Primitives::s_pow_f64_f64_pm("_pow_d_d", &pow_f64_f64);
Primitive_f64_1_f64
Primitives::s_log_f64_pm("_log_d", &log_f64);
Primitive_f64_1_f64
Primitives::s_sin_f64_pm("_sin_d", &sin_f64);
Primitive_f64_1_f64
Primitives::s_cos_f64_pm("_cos_d", &cos_f64);
Primitive_f64_1_f64
Primitives::s_tan_f64_pm("_tan_d", &tan_f64);
} /*namespace scm*/
} /*namespace xo*/
/* end init_primitives.cpp */

View file

@ -0,0 +1,32 @@
/** @file init_procedure2.cpp
*
* @author Roland Conybeare, Jan 2026
**/
#include "init_procedure2.hpp"
#include "init_primitives.hpp"
//#include "procedure2_register_facets.hpp"
//#include "procedure2_register_types.hpp"
#include <xo/gc/CollectorTypeRegistry.hpp>
namespace xo {
using xo::scm::Primitives;
void
InitSubsys<S_procedure2_tag>::init()
{
}
InitEvidence
InitSubsys<S_procedure2_tag>::require()
{
InitEvidence retval;
/* xo-procedure2/'s own initialization code */
retval ^= Subsystem::provide<S_procedure2_tag>("procedure2", &init);
return retval;
}
} /*namespace xo*/
/* end init_procedure2.cpp */