From d001087a483b78002498500ef6ddec6b8cefb63f Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 14 Mar 2024 08:47:14 -0400 Subject: [PATCH] functions --- CMakeLists.txt | 2 +- include/lilfbtf/tree.h | 9 ++++++- include/lilfbtf/type.h | 5 ++-- src/tree.cpp | 10 ++++++++ src/type.cpp | 11 +++++--- tests/src/main.cpp | 57 ++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 84 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 93cab74..c586cb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ 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_UBSAN "Enable the ub sanitizer" OFF) diff --git a/include/lilfbtf/tree.h b/include/lilfbtf/tree.h index 340e04b..29fecee 100644 --- a/include/lilfbtf/tree.h +++ b/include/lilfbtf/tree.h @@ -93,6 +93,11 @@ namespace fb type.call(blt::span{children, type.argc()}); } + inline blt::unsafe::any_t value() + { + return type.getValue(); + } + ~node_t() { for (blt::size_t i = 0; i < type.argc(); i++) @@ -110,10 +115,12 @@ namespace fb private: blt::bump_allocator& alloc; type_engine_t& types; + detail::node_t* root; public: tree_t(blt::bump_allocator& alloc, type_engine_t& types); - + static tree_t make_tree(blt::bump_allocator& alloc, type_engine_t& types, blt::size_t min_height, + blt::size_t max_height); }; } diff --git a/include/lilfbtf/type.h b/include/lilfbtf/type.h index deb2034..e13d699 100644 --- a/include/lilfbtf/type.h +++ b/include/lilfbtf/type.h @@ -114,15 +114,16 @@ namespace fb associative_array> function_initializer; associative_array, true> terminals; associative_array, true> non_terminals; + std::vector> all_non_terminals; public: type_engine_t() = default; 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> 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> initializer); inline type_id get_type_id(type_name name) diff --git a/src/tree.cpp b/src/tree.cpp index 909f4e3..532fe8b 100644 --- a/src/tree.cpp +++ b/src/tree.cpp @@ -26,5 +26,15 @@ namespace fb tree_t::tree_t(blt::bump_allocator& alloc, type_engine_t& types): alloc(alloc), types(types) {} + tree_t tree_t::make_tree(blt::bump_allocator& alloc, type_engine_t& types, blt::size_t min_height, + blt::size_t max_height) + { + tree_t tree(alloc, types); + + + + return tree; + } + } \ No newline at end of file diff --git a/src/type.cpp b/src/type.cpp index c655915..4aeacc7 100644 --- a/src/type.cpp +++ b/src/type.cpp @@ -28,13 +28,15 @@ namespace fb 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> initializer) { function_id id = function_to_name.size(); + type_id tid = get_type_id(output); name_to_function[func_name] = id; 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) function_initializer.insert(id, init.value()); return id; @@ -50,13 +52,14 @@ namespace fb 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> initializer) { function_id id = function_to_name.size(); + type_id tid = get_type_id(output); name_to_function[func_name] = id; functions.insert(id, func); - terminals[output].push_back(id); + terminals[tid].push_back(id); if (auto& init = initializer) function_initializer.insert(id, init.value()); return id; diff --git a/tests/src/main.cpp b/tests/src/main.cpp index 76e9250..734ced7 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -8,13 +8,60 @@ #include #include "blt/profiling/profiler_v2.h" #include "lilfbtf/test5.h" +#include +#include -struct data { +struct data +{ float f; int i; char c; }; +const fb::func_t_call_t add_f = [](fb::func_t& us, blt::span args) { + us.setValue(args[0]->value().any_cast() + args[1]->value().any_cast()); +}; +const fb::func_t_call_t sub_f = [](fb::func_t& us, blt::span args) { + us.setValue(args[0]->value().any_cast() - args[1]->value().any_cast()); +}; +const fb::func_t_call_t mul_f = [](fb::func_t& us, blt::span args) { + us.setValue(args[0]->value().any_cast() * args[1]->value().any_cast()); +}; +const fb::func_t_call_t div_f = [](fb::func_t& us, blt::span args) { + auto dim = args[1]->value().any_cast(); + if (dim == 0) + us.setValue(0); + else + us.setValue(args[0]->value().any_cast() + dim); +}; + +const fb::func_t_call_t value_f = [](fb::func_t&, blt::span) {}; +const fb::func_t_call_t if_f = [](fb::func_t& us, blt::span args) { + if (args[0]->value().any_cast()) + us.setValue(args[1]->value().any_cast()); + else + us.setValue(args[2]->value().any_cast()); +}; +const fb::func_t_call_t equals_b_f = [](fb::func_t& us, blt::span args) { + us.setValue(args[0]->value().any_cast() == args[1]->value().any_cast()); +}; +const fb::func_t_call_t equals_n_f = [](fb::func_t& us, blt::span args) { + us.setValue(args[0]->value().any_cast() == args[1]->value().any_cast()); +}; +const fb::func_t_call_t less_f = [](fb::func_t& us, blt::span args) { + us.setValue(args[0]->value().any_cast() < args[1]->value().any_cast()); +}; +const fb::func_t_call_t greater_f = [](fb::func_t& us, blt::span args) { + us.setValue(args[0]->value().any_cast() > args[1]->value().any_cast()); +}; +const fb::func_t_call_t not_f = [](fb::func_t& us, blt::span args) { us.setValue(!args[0]->value().any_cast()); }; +const fb::func_t_call_t and_f = [](fb::func_t& us, blt::span args) { + us.setValue(args[0]->value().any_cast() && args[1]->value().any_cast()); +}; +const fb::func_t_call_t or_f = [](fb::func_t& us, blt::span args) { + us.setValue(args[0]->value().any_cast() || args[1]->value().any_cast()); +}; + int main(int argc, const char** argv) { size_t size = 32; @@ -55,7 +102,13 @@ int main(int argc, const char** argv) //fb::test2(); //fb::test3(); //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 Evaluation");