functions

main
Brett 2024-03-14 08:47:14 -04:00
parent 48c77af126
commit d001087a48
6 changed files with 84 additions and 10 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(lilfbtf5 VERSION 0.1.21) project(lilfbtf5 VERSION 0.1.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

@ -93,6 +93,11 @@ namespace fb
type.call(blt::span<node_t*>{children, type.argc()}); type.call(blt::span<node_t*>{children, type.argc()});
} }
inline blt::unsafe::any_t value()
{
return type.getValue();
}
~node_t() ~node_t()
{ {
for (blt::size_t i = 0; i < type.argc(); i++) for (blt::size_t i = 0; i < type.argc(); i++)
@ -110,10 +115,12 @@ namespace fb
private: private:
blt::bump_allocator<blt::BLT_2MB_SIZE, false>& alloc; blt::bump_allocator<blt::BLT_2MB_SIZE, false>& alloc;
type_engine_t& types; type_engine_t& types;
detail::node_t* root;
public: public:
tree_t(blt::bump_allocator<blt::BLT_2MB_SIZE, false>& alloc, type_engine_t& types); tree_t(blt::bump_allocator<blt::BLT_2MB_SIZE, false>& alloc, type_engine_t& types);
static tree_t make_tree(blt::bump_allocator<blt::BLT_2MB_SIZE, false>& alloc, type_engine_t& types, blt::size_t min_height,
blt::size_t max_height);
}; };
} }

View File

@ -114,15 +114,16 @@ namespace fb
associative_array<function_id, std::reference_wrapper<func_t_init_t>> function_initializer; associative_array<function_id, std::reference_wrapper<func_t_init_t>> function_initializer;
associative_array<type_id, std::vector<function_id>, true> terminals; associative_array<type_id, std::vector<function_id>, true> terminals;
associative_array<type_id, std::vector<function_id>, true> non_terminals; associative_array<type_id, std::vector<function_id>, true> non_terminals;
std::vector<std::pair<type_id, function_id>> all_non_terminals;
public: public:
type_engine_t() = default; type_engine_t() = default;
type_id register_type(type_name type_name); type_id register_type(type_name type_name);
function_id register_function(function_name func_name, type_id output, func_t_call_t& func, function_id register_function(function_name func_name, type_name output, func_t_call_t& func,
std::optional<std::reference_wrapper<func_t_init_t>> initializer); std::optional<std::reference_wrapper<func_t_init_t>> initializer);
function_id register_terminal_function(function_name func_name, type_id output, func_t_call_t& func, function_id register_terminal_function(function_name func_name, type_name output, func_t_call_t& func,
std::optional<std::reference_wrapper<func_t_init_t>> initializer); std::optional<std::reference_wrapper<func_t_init_t>> initializer);
inline type_id get_type_id(type_name name) inline type_id get_type_id(type_name name)

View File

@ -26,5 +26,15 @@ namespace fb
tree_t::tree_t(blt::bump_allocator<blt::BLT_2MB_SIZE, false>& alloc, type_engine_t& types): alloc(alloc), types(types) tree_t::tree_t(blt::bump_allocator<blt::BLT_2MB_SIZE, false>& alloc, type_engine_t& types): alloc(alloc), types(types)
{} {}
tree_t tree_t::make_tree(blt::bump_allocator<blt::BLT_2MB_SIZE, false>& alloc, type_engine_t& types, blt::size_t min_height,
blt::size_t max_height)
{
tree_t tree(alloc, types);
return tree;
}
} }

View File

@ -28,13 +28,15 @@ namespace fb
return id; return id;
} }
function_id type_engine_t::register_function(function_name func_name, type_id output, func_t_call_t& func, function_id type_engine_t::register_function(function_name func_name, type_name output, func_t_call_t& func,
std::optional<std::reference_wrapper<func_t_init_t>> initializer) std::optional<std::reference_wrapper<func_t_init_t>> initializer)
{ {
function_id id = function_to_name.size(); function_id id = function_to_name.size();
type_id tid = get_type_id(output);
name_to_function[func_name] = id; name_to_function[func_name] = id;
functions.insert(id, func); functions.insert(id, func);
non_terminals[output].push_back(id); non_terminals[tid].push_back(id);
all_non_terminals.emplace_back(tid, id);
if (auto& init = initializer) if (auto& init = initializer)
function_initializer.insert(id, init.value()); function_initializer.insert(id, init.value());
return id; return id;
@ -50,13 +52,14 @@ namespace fb
return *this; return *this;
} }
function_id type_engine_t::register_terminal_function(function_name func_name, type_id output, func_t_call_t& func, function_id type_engine_t::register_terminal_function(function_name func_name, type_name output, func_t_call_t& func,
std::optional<std::reference_wrapper<func_t_init_t>> initializer) std::optional<std::reference_wrapper<func_t_init_t>> initializer)
{ {
function_id id = function_to_name.size(); function_id id = function_to_name.size();
type_id tid = get_type_id(output);
name_to_function[func_name] = id; name_to_function[func_name] = id;
functions.insert(id, func); functions.insert(id, func);
terminals[output].push_back(id); terminals[tid].push_back(id);
if (auto& init = initializer) if (auto& init = initializer)
function_initializer.insert(id, init.value()); function_initializer.insert(id, init.value());
return id; return id;

View File

@ -8,13 +8,60 @@
#include <lilfbtf/test4.h> #include <lilfbtf/test4.h>
#include "blt/profiling/profiler_v2.h" #include "blt/profiling/profiler_v2.h"
#include "lilfbtf/test5.h" #include "lilfbtf/test5.h"
#include <lilfbtf/tree.h>
#include <lilfbtf/type.h>
struct data { struct data
{
float f; float f;
int i; int i;
char c; char c;
}; };
const fb::func_t_call_t add_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
us.setValue(args[0]->value().any_cast<double>() + args[1]->value().any_cast<double>());
};
const fb::func_t_call_t sub_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
us.setValue(args[0]->value().any_cast<double>() - args[1]->value().any_cast<double>());
};
const fb::func_t_call_t mul_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
us.setValue(args[0]->value().any_cast<double>() * args[1]->value().any_cast<double>());
};
const fb::func_t_call_t div_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
auto dim = args[1]->value().any_cast<double>();
if (dim == 0)
us.setValue(0);
else
us.setValue(args[0]->value().any_cast<double>() + dim);
};
const fb::func_t_call_t value_f = [](fb::func_t&, blt::span<fb::detail::node_t*>) {};
const fb::func_t_call_t if_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
if (args[0]->value().any_cast<bool>())
us.setValue(args[1]->value().any_cast<double>());
else
us.setValue(args[2]->value().any_cast<double>());
};
const fb::func_t_call_t equals_b_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
us.setValue(args[0]->value().any_cast<bool>() == args[1]->value().any_cast<bool>());
};
const fb::func_t_call_t equals_n_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
us.setValue(args[0]->value().any_cast<double>() == args[1]->value().any_cast<double>());
};
const fb::func_t_call_t less_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
us.setValue(args[0]->value().any_cast<double>() < args[1]->value().any_cast<double>());
};
const fb::func_t_call_t greater_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
us.setValue(args[0]->value().any_cast<double>() > args[1]->value().any_cast<double>());
};
const fb::func_t_call_t not_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) { us.setValue(!args[0]->value().any_cast<bool>()); };
const fb::func_t_call_t and_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
us.setValue(args[0]->value().any_cast<bool>() && args[1]->value().any_cast<bool>());
};
const fb::func_t_call_t or_f = [](fb::func_t& us, blt::span<fb::detail::node_t*> args) {
us.setValue(args[0]->value().any_cast<bool>() || args[1]->value().any_cast<bool>());
};
int main(int argc, const char** argv) int main(int argc, const char** argv)
{ {
size_t size = 32; size_t size = 32;
@ -55,7 +102,13 @@ int main(int argc, const char** argv)
//fb::test2(); //fb::test2();
//fb::test3(); //fb::test3();
//fb::test4(); //fb::test4();
fb::test5(); //fb::test5();
fb::type_engine_t typeEngine;
typeEngine.register_type("u8");
typeEngine.register_type("bool");
BLT_PRINT_PROFILE("Tree Construction"); BLT_PRINT_PROFILE("Tree Construction");
BLT_PRINT_PROFILE("Tree Evaluation"); BLT_PRINT_PROFILE("Tree Evaluation");