worked operators, need a terminal engine

thread
Brett 2024-06-19 21:44:00 -04:00
parent ac36a63c5b
commit bfd70c7f81
5 changed files with 102 additions and 62 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(blt-gp VERSION 0.0.21) project(blt-gp VERSION 0.0.22)
option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)

View File

@ -293,15 +293,21 @@ void test()
// unsigned char data[9582]; // unsigned char data[9582];
//}; //};
blt::gp::stack_allocator alloc; blt::gp::type_system type_system;
blt::gp::gp_program program(type_system);
blt::gp::operation_t<float(float, float)> add([](float a, float b) { return a + b; });
blt::gp::operation_t<float(float, float)> sub([](float a, float b) { return a - b; });
blt::gp::operation_t<float(float, float)> mul([](float a, float b) { return a * b; });
blt::gp::operation_t<float(float, float)> pro_div([](float a, float b) { return b == 0 ? 0.0f : a / b; });
blt::gp::operation_t<float()> lit([]() {return 0.0f;});
float nyah(float f, int i, bool b)
{
return f + static_cast<float>(i * b);
}
int main() int main()
{ {
type_system.register_type<float>();
type_system.register_type<bool>();
// constexpr blt::size_t MAX_ALIGNMENT = 8; // constexpr blt::size_t MAX_ALIGNMENT = 8;
// test(); // test();
// std::cout << alignof(silly) << " " << sizeof(silly) << std::endl; // std::cout << alignof(silly) << " " << sizeof(silly) << std::endl;
@ -364,18 +370,18 @@ int main()
// alloc.push(550.3f); // alloc.push(550.3f);
// alloc.push(20.1230345); // alloc.push(20.1230345);
// alloc.push(std::string("SillyString")); // alloc.push(std::string("SillyString"));
alloc.push(33.22f); // alloc.push(33.22f);
alloc.push(120); // alloc.push(120);
alloc.push(true); // alloc.push(true);
//
// blt::gp::operation_t<float(float, int, bool)> silly_op(nyah);
// blt::gp::operation_t<float(float, float)> silly_op_2([](float f, float g) {
// return f + g;
// });
blt::gp::operation_t<float(float, int, bool)> silly_op(nyah); // std::cout << silly_op(alloc) << std::endl;
blt::gp::operation_t<float(float, float)> silly_op_2([](float f, float g) { //
return f + g; // std::cout << "Is empty? " << alloc.empty() << std::endl;
});
std::cout << silly_op(alloc) << std::endl;
std::cout << "Is empty? " << alloc.empty() << std::endl;
// std::cout << std::endl; // std::cout << std::endl;
// //

View File

@ -19,28 +19,58 @@
#ifndef BLT_GP_PROGRAM_H #ifndef BLT_GP_PROGRAM_H
#define BLT_GP_PROGRAM_H #define BLT_GP_PROGRAM_H
#include <blt/gp/stack.h>
#include <cstddef> #include <cstddef>
#include <blt/gp/fwdecl.h>
#include <functional> #include <functional>
#include <blt/std/ranges.h>
#include <blt/std/hashmap.h>
#include <blt/std/types.h>
#include <blt/std/utility.h>
#include <type_traits> #include <type_traits>
#include <string_view> #include <string_view>
#include <string> #include <string>
#include <utility> #include <utility>
#include <iostream> #include <iostream>
#include <random>
#include <blt/std/ranges.h>
#include <blt/std/hashmap.h>
#include <blt/std/types.h>
#include <blt/std/utility.h>
#include <blt/std/memory.h>
#include <blt/gp/fwdecl.h>
#include <blt/gp/stack.h>
namespace blt::gp namespace blt::gp
{ {
template<typename T>
struct integer_type
{
T id;
integer_type() = default;
integer_type(T id): id(id) // NOLINT
{}
inline operator T() const // NOLINT
{
return id;
}
};
struct operator_id : integer_type<blt::size_t>
{
using integer_type<blt::size_t>::integer_type;
};
struct type_id : integer_type<blt::size_t>
{
using integer_type<blt::size_t>::integer_type;
};
class type class type
{ {
public: public:
type() = default;
template<typename T> template<typename T>
static type make_type(blt::size_t id) static type make_type(type_id id)
{ {
return type(sizeof(T), id, blt::type_string<T>()); return type(sizeof(T), id, blt::type_string<T>());
} }
@ -50,7 +80,7 @@ namespace blt::gp
return size_; return size_;
} }
[[nodiscard]] blt::size_t id() const [[nodiscard]] type_id id() const
{ {
return id_; return id_;
} }
@ -61,12 +91,19 @@ namespace blt::gp
} }
private: private:
type(size_t size, size_t id, std::string_view name): size_(size), id_(id), name_(name) type(size_t size, type_id id, std::string_view name): size_(size), id_(id), name_(name)
{} {}
blt::size_t size_; blt::size_t size_{};
blt::size_t id_; type_id id_{};
std::string name_; std::string name_{};
};
class allowed_types_t
{
public:
private:
}; };
class type_system class type_system
@ -77,7 +114,7 @@ namespace blt::gp
template<typename T> template<typename T>
inline type register_type() inline type register_type()
{ {
types[blt::type_string_raw<T>()](type::make_type<T>(types.size())); types.insert({blt::type_string_raw<T>(), type::make_type<T>(types.size())});
return types[blt::type_string_raw<T>()]; return types[blt::type_string_raw<T>()];
} }
@ -167,27 +204,24 @@ namespace blt::gp
template<typename Return, typename... Args> template<typename Return, typename... Args>
void add_operator(const operation_t<Return(Args...)>& op) void add_operator(const operation_t<Return(Args...)>& op)
{ {
if (op.get_argc() == 0) auto return_type_id = system.get_type<Return>().id();
terminals.push_back(operators.size()); auto& operator_list = op.get_argc() == 0 ? terminals : non_terminals;
else operator_list[return_type_id].push_back(operators.size());
non_terminals.push_back(operators.size());
auto operator_index = operators.size();
(argument_types[operator_index].push_back(system.get_type<Args>()), ...);
operators.push_back(op.make_callable()); operators.push_back(op.make_callable());
std::vector<type> types;
(types.push_back(system.get_type<Args>()), ...);
arg_types.push_back(std::move(types));
return_types.push_back(system.get_type<Return>());
} }
private: private:
type_system system; type_system system;
blt::gp::stack_allocator alloc;
// indexed from return TYPE ID, returns index of operator
blt::expanding_buffer<std::vector<blt::size_t>> terminals;
blt::expanding_buffer<std::vector<blt::size_t>> non_terminals;
// indexed from OPERATOR NUMBER
blt::expanding_buffer<std::vector<type>> argument_types;
std::vector<std::function<void(stack_allocator&)>> operators; std::vector<std::function<void(stack_allocator&)>> operators;
std::vector<blt::size_t> terminals;
std::vector<blt::size_t> non_terminals;
std::vector<type> return_types;
std::vector<std::vector<type>> arg_types;
}; };
} }

@ -1 +1 @@
Subproject commit 2266d64f042bc97b54e7e50dcaac730bcc3ab4c3 Subproject commit 9ad96191ff91ec7efa6aa142e377201fe81b4c40