From 48c77af1262932263c8354047fb4cbef3c72a140 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 14 Mar 2024 08:36:06 -0400 Subject: [PATCH] tree type --- CMakeLists.txt | 2 +- include/lilfbtf/tree.h | 7 +++++-- include/lilfbtf/type.h | 11 +++++++---- src/tree.cpp | 6 +++++- src/type.cpp | 11 ++++++++--- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e7c1c5d..93cab74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(lilfbtf5 VERSION 0.1.20) +project(lilfbtf5 VERSION 0.1.21) 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 ae4bf30..340e04b 100644 --- a/include/lilfbtf/tree.h +++ b/include/lilfbtf/tree.h @@ -23,6 +23,7 @@ #include #include "blt/std/ranges.h" #include "blt/std/allocator.h" +#include "type.h" #include namespace fb @@ -104,13 +105,15 @@ namespace fb }; } - class tree_t { private: blt::bump_allocator& alloc; + type_engine_t& types; public: - + tree_t(blt::bump_allocator& alloc, type_engine_t& types); + + }; } diff --git a/include/lilfbtf/type.h b/include/lilfbtf/type.h index 1033036..deb2034 100644 --- a/include/lilfbtf/type.h +++ b/include/lilfbtf/type.h @@ -24,6 +24,7 @@ #include #include #include +#include namespace fb { @@ -110,7 +111,7 @@ namespace fb // function id -> list of type_id for parameters where index 0 = arg 1 associative_array> function_inputs; - associative_array> terminal_initializer; + associative_array> function_initializer; associative_array, true> terminals; associative_array, true> non_terminals; public: @@ -118,9 +119,11 @@ namespace fb 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_id 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, func_t_init_t& initializer); + function_id register_terminal_function(function_name func_name, type_id output, func_t_call_t& func, + std::optional> initializer); inline type_id get_type_id(type_name name) { return name_to_type[name]; } @@ -137,7 +140,7 @@ namespace fb { return get_function(get_function_id(name)); } inline func_t_init_t& get_function_initializer(function_id id) - { return terminal_initializer[id]; } + { return function_initializer[id]; } inline func_t_init_t& get_function_initializer(function_name name) { return get_function_initializer(get_function_id(name)); } diff --git a/src/tree.cpp b/src/tree.cpp index c63d358..909f4e3 100644 --- a/src/tree.cpp +++ b/src/tree.cpp @@ -22,5 +22,9 @@ namespace fb func_t::func_t(blt::size_t argc, const func_t_call_t& func, type_id output_type, function_id function_type): argc_(argc), type(output_type), function(function_type), func(func) {} - + + tree_t::tree_t(blt::bump_allocator& alloc, type_engine_t& types): alloc(alloc), types(types) + {} + + } \ No newline at end of file diff --git a/src/type.cpp b/src/type.cpp index ed0f9c4..c655915 100644 --- a/src/type.cpp +++ b/src/type.cpp @@ -28,12 +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_id output, func_t_call_t& func, + std::optional> initializer) { function_id id = function_to_name.size(); name_to_function[func_name] = id; functions.insert(id, func); non_terminals[output].push_back(id); + if (auto& init = initializer) + function_initializer.insert(id, init.value()); return id; } @@ -47,13 +50,15 @@ namespace fb return *this; } - function_id type_engine_t::register_terminal_function(function_name func_name, type_id output, func_t_call_t& func, func_t_init_t& initializer) + function_id type_engine_t::register_terminal_function(function_name func_name, type_id output, func_t_call_t& func, + std::optional> initializer) { function_id id = function_to_name.size(); name_to_function[func_name] = id; functions.insert(id, func); terminals[output].push_back(id); - terminal_initializer.insert(id, initializer); + if (auto& init = initializer) + function_initializer.insert(id, init.value()); return id; } } \ No newline at end of file