From ee917ea7870154a5d9a4d510f76d7f03f0ea9152 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 14 Mar 2024 10:34:57 -0400 Subject: [PATCH] funcy --- CMakeLists.txt | 2 +- include/lilfbtf/type.h | 18 ++++++++++++++++-- src/type.cpp | 6 +++--- tests/src/main.cpp | 11 ++++++++++- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5062ca6..b061eee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(lilfbtf5 VERSION 0.1.23) +project(lilfbtf5 VERSION 0.1.24) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/include/lilfbtf/type.h b/include/lilfbtf/type.h index 55a3119..f518017 100644 --- a/include/lilfbtf/type.h +++ b/include/lilfbtf/type.h @@ -42,7 +42,7 @@ namespace fb if constexpr (init) { for (blt::size_t i = size_; i < new_size; i++) - new(&new_size[i]) T(); + new(&new_data[i]) T(); } for (blt::size_t i = 0; i < size_; i++) new(&new_data[i]) T(std::move(data_[i])); @@ -55,6 +55,20 @@ namespace fb associative_array(): size_(0), data_(nullptr) {} + [[nodiscard]] const T& at(K index) const + { + while (index >= size_) + expand(); + return data_[index]; + } + + T& at(K index) + { + while (index >= size_) + expand(); + return data_[index]; + } + T& operator[](K index) { return data_[index]; @@ -109,7 +123,7 @@ namespace fb // Also a bad idea to store references, however these functions should be declared statically so this isn't as big of an issue. associative_array> functions; // function id -> list of type_id for parameters where index 0 = arg 1 - associative_array> function_inputs; + associative_array, true> function_inputs; associative_array function_argc; associative_array> function_initializer; diff --git a/src/type.cpp b/src/type.cpp index f9414fa..03b226e 100644 --- a/src/type.cpp +++ b/src/type.cpp @@ -35,7 +35,7 @@ namespace fb type_id tid = get_type_id(output); name_to_function[func_name] = id; functions.insert(id, func); - non_terminals[tid].push_back(id); + non_terminals.at(tid).push_back(id); all_non_terminals.emplace_back(tid, id); function_argc.insert(id, argc); if (auto& init = initializer) @@ -49,7 +49,7 @@ namespace fb std::vector type_ids; for (const auto& v : types) type_ids.push_back(get_type_id(v)); - function_inputs[id] = std::move(type_ids); + function_inputs.at(id) = std::move(type_ids); return *this; } @@ -60,7 +60,7 @@ namespace fb type_id tid = get_type_id(output); name_to_function[func_name] = id; functions.insert(id, func); - terminals[tid].push_back(id); + terminals.at(tid).push_back(id); function_argc.insert(id, 0); if (auto& init = initializer) function_initializer.insert(id, init.value()); diff --git a/tests/src/main.cpp b/tests/src/main.cpp index 1de183c..447b894 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -35,7 +35,13 @@ const fb::func_t_call_t div_f = [](fb::func_t& us, blt::spanvalue().any_cast() + dim); }; -const fb::func_t_call_t value_f = [](fb::func_t&, blt::span) {}; +const fb::func_t_call_t empty_f = [](fb::func_t&, blt::span) {}; +const fb::func_t_init_t value_init_f = [](fb::func_t& us){ + us.setValue(fb::random_value()); +}; +const fb::func_t_init_t bool_init_f = [](fb::func_t& us){ + us.setValue(fb::choice()); +}; 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()); @@ -131,6 +137,9 @@ int main(int argc, const char** argv) typeEngine.register_function("or_b", "bool", or_b_f, 2); typeEngine.register_function("or_n", "u8", or_n_f, 2); + typeEngine.register_terminal_function("value", "u8", empty_f, value_init_f); + typeEngine.register_terminal_function("bool_value", "bool", empty_f, bool_init_f); + typeEngine.associate_input("add", {"u8", "u8"}); typeEngine.associate_input("sub", {"u8", "u8"}); typeEngine.associate_input("mul", {"u8", "u8"});