#pragma once /* * Copyright (C) 2024 Brett Terpstra * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef BLT_GP_PROGRAM_H #define BLT_GP_PROGRAM_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace blt::gp { template 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 { using integer_type::integer_type; }; struct type_id : integer_type { using integer_type::integer_type; }; class type { public: type() = default; template static type make_type(type_id id) { return type(sizeof(T), id, blt::type_string()); } [[nodiscard]] blt::size_t size() const { return size_; } [[nodiscard]] type_id id() const { return id_; } [[nodiscard]] std::string_view name() const { return name_; } private: type(size_t size, type_id id, std::string_view name): size_(size), id_(id), name_(name) {} blt::size_t size_{}; type_id id_{}; std::string name_{}; }; class allowed_types_t { public: private: }; class type_system { public: type_system() = default; template inline type register_type() { types.insert({blt::type_string_raw(), type::make_type(types.size())}); return types[blt::type_string_raw()]; } template inline type get_type() { return types[blt::type_string_raw()]; } private: blt::hashmap_t types; }; template class operation_t; template class operation_t { public: using function_t = std::function; constexpr operation_t(const operation_t& copy) = default; constexpr operation_t(operation_t&& move) = default; template constexpr explicit operation_t(const Functor& functor): func(functor) {} template [[nodiscard]] inline constexpr static blt::size_t getByteOffset() { blt::size_t offset = 0; blt::size_t current_index = 0; ((offset += (current_index++ > index ? stack_allocator::aligned_size() : 0)), ...); return offset; } template inline constexpr Return exec_sequence_to_indices(stack_allocator& allocator, std::integer_sequence) const { // expands Args and indices, providing each argument with its index calculating the current argument byte offset return func(allocator.from(getByteOffset())...); } [[nodiscard]] constexpr inline Return operator()(stack_allocator& allocator) const { constexpr auto seq = std::make_integer_sequence(); Return ret = exec_sequence_to_indices(allocator, seq); allocator.call_destructors(); allocator.pop_bytes((stack_allocator::aligned_size() + ...)); return ret; } [[nodiscard]] std::function make_callable() const { return [this](stack_allocator& values) { values.push(this->operator()(values)); }; } [[nodiscard]] blt::size_t get_argc() const { return sizeof...(Args); } private: function_t func; }; template operation_t(Return (*)(Args...)) -> operation_t; template operation_t(Return (Class::*)(Args...) const) -> operation_t; template operation_t make_operator(Return (Class::*)(Args...) const lambda) { // https://ventspace.wordpress.com/2022/04/11/quick-snippet-c-type-trait-templates-for-lambda-details/ } template operation_t make_operator(Lambda&& lambda) { return operation_t(std::forward(lambda)); } // // template // operation(std::function) -> operation; class gp_program { public: explicit gp_program(type_system system): system(std::move(system)) {} template void add_operator(const operation_t& op) { auto return_type_id = system.get_type().id(); auto& operator_list = op.get_argc() == 0 ? terminals : non_terminals; operator_list[return_type_id].push_back(operators.size()); auto operator_index = operators.size(); (argument_types[operator_index].push_back(system.get_type()), ...); operators.push_back(op.make_callable()); } private: type_system system; blt::gp::stack_allocator alloc; // indexed from return TYPE ID, returns index of operator blt::expanding_buffer> terminals; blt::expanding_buffer> non_terminals; // indexed from OPERATOR NUMBER blt::expanding_buffer> argument_types; std::vector> operators; }; } #endif //BLT_GP_PROGRAM_H