diff --git a/src/language/ASTNodeCFunctionExpressionBuilder.cpp b/src/language/ASTNodeBuiltinFunctionExpressionBuilder.cpp similarity index 62% rename from src/language/ASTNodeCFunctionExpressionBuilder.cpp rename to src/language/ASTNodeBuiltinFunctionExpressionBuilder.cpp index dbd1d0834bbf79328051d7c64a97b8cbb4f37055..13c1297bd3150b11d100ec46a2b46d1678249862 100644 --- a/src/language/ASTNodeCFunctionExpressionBuilder.cpp +++ b/src/language/ASTNodeBuiltinFunctionExpressionBuilder.cpp @@ -1,4 +1,4 @@ -#include <ASTNodeCFunctionExpressionBuilder.hpp> +#include <ASTNodeBuiltinFunctionExpressionBuilder.hpp> #include <PEGGrammar.hpp> #include <SymbolTable.hpp> @@ -6,12 +6,12 @@ #include <ASTNodeDataTypeFlattener.hpp> #include <ASTNodeNaturalConversionChecker.hpp> -#include <node_processor/CFunctionProcessor.hpp> +#include <node_processor/BuiltinFunctionProcessor.hpp> PUGS_INLINE std::unique_ptr<IFunctionArgumentConverter> -ASTNodeCFunctionExpressionBuilder::_getArgumentConverter(const ASTNodeDataType& parameter_type, - const ASTNodeSubDataType& argument_node_sub_data_type, - const size_t argument_number) +ASTNodeBuiltinFunctionExpressionBuilder::_getArgumentConverter(const ASTNodeDataType& parameter_type, + const ASTNodeSubDataType& argument_node_sub_data_type, + const size_t argument_number) { auto get_function_argument_converter_for = [&](const auto& parameter_v) -> std::unique_ptr<IFunctionArgumentConverter> { @@ -68,22 +68,23 @@ ASTNodeCFunctionExpressionBuilder::_getArgumentConverter(const ASTNodeDataType& PUGS_INLINE void -ASTNodeCFunctionExpressionBuilder::_storeArgumentProcessor( +ASTNodeBuiltinFunctionExpressionBuilder::_storeArgumentProcessor( const std::vector<ASTNodeDataType>& parameter_type_list, const ASTNodeDataTypeFlattener::FlattenedDataTypeList& flattened_datatype_list, const size_t argument_number, - CFunctionProcessor& c_function_processor) + BuiltinFunctionProcessor& _processor) { - c_function_processor.addArgumentConverter(this->_getArgumentConverter(parameter_type_list[argument_number], - flattened_datatype_list[argument_number], - argument_number)); + _processor.addArgumentConverter(this->_getArgumentConverter(parameter_type_list[argument_number], + flattened_datatype_list[argument_number], + argument_number)); } PUGS_INLINE void -ASTNodeCFunctionExpressionBuilder::_buildArgumentProcessors(const std::vector<ASTNodeDataType>& parameter_type_list, - ASTNode& node, - CFunctionProcessor& c_function_processor) +ASTNodeBuiltinFunctionExpressionBuilder::_buildArgumentProcessors( + const std::vector<ASTNodeDataType>& parameter_type_list, + ASTNode& node, + BuiltinFunctionProcessor& _processor) { ASTNode& argument_nodes = *node.children[1]; @@ -102,33 +103,34 @@ ASTNodeCFunctionExpressionBuilder::_buildArgumentProcessors(const std::vector<AS if (arguments_number > 1) { for (size_t i = 0; i < arguments_number; ++i) { - this->_storeArgumentProcessor(parameter_type_list, flattened_datatype_list, i, c_function_processor); + this->_storeArgumentProcessor(parameter_type_list, flattened_datatype_list, i, _processor); } } else { - this->_storeArgumentProcessor(parameter_type_list, flattened_datatype_list, 0, c_function_processor); + this->_storeArgumentProcessor(parameter_type_list, flattened_datatype_list, 0, _processor); } } -ASTNodeCFunctionExpressionBuilder::ASTNodeCFunctionExpressionBuilder(ASTNode& node) +ASTNodeBuiltinFunctionExpressionBuilder::ASTNodeBuiltinFunctionExpressionBuilder(ASTNode& node) { auto [i_function_symbol, found] = node.m_symbol_table->find(node.children[0]->string(), node.begin()); Assert(found); - Assert(i_function_symbol->attributes().dataType() == ASTNodeDataType::c_function_t); + Assert(i_function_symbol->attributes().dataType() == ASTNodeDataType::builtin_function_t); - uint64_t c_function_id = std::get<uint64_t>(i_function_symbol->attributes().value()); + uint64_t builtin_function_id = std::get<uint64_t>(i_function_symbol->attributes().value()); - auto& c_function_embedder_table = node.m_symbol_table->cFunctionEmbedderTable(); - std::shared_ptr c_function_embedder = c_function_embedder_table[c_function_id]; + auto& builtin_function_embedder_table = node.m_symbol_table->builtinFunctionEmbedderTable(); + std::shared_ptr builtin_function_embedder = builtin_function_embedder_table[builtin_function_id]; - std::vector<ASTNodeDataType> c_function_parameter_type_list = c_function_embedder->getParameterDataTypes(); + std::vector<ASTNodeDataType> builtin_function_parameter_type_list = + builtin_function_embedder->getParameterDataTypes(); - ASTNode& argument_nodes = *node.children[1]; - std::unique_ptr c_function_processor = std::make_unique<CFunctionProcessor>(argument_nodes); + ASTNode& argument_nodes = *node.children[1]; + std::unique_ptr builtin_function_processor = std::make_unique<BuiltinFunctionProcessor>(argument_nodes); - this->_buildArgumentProcessors(c_function_parameter_type_list, node, *c_function_processor); + this->_buildArgumentProcessors(builtin_function_parameter_type_list, node, *builtin_function_processor); - c_function_processor->setFunctionExpressionProcessor( - std::make_unique<CFunctionExpressionProcessor>(c_function_embedder)); + builtin_function_processor->setFunctionExpressionProcessor( + std::make_unique<BuiltinFunctionExpressionProcessor>(builtin_function_embedder)); - node.m_node_processor = std::move(c_function_processor); + node.m_node_processor = std::move(builtin_function_processor); } diff --git a/src/language/ASTNodeCFunctionExpressionBuilder.hpp b/src/language/ASTNodeBuiltinFunctionExpressionBuilder.hpp similarity index 77% rename from src/language/ASTNodeCFunctionExpressionBuilder.hpp rename to src/language/ASTNodeBuiltinFunctionExpressionBuilder.hpp index 267e4223868eed72c63dd7c19b7f0c1f75ffb6fb..00a10d06851cf8456807b1bdfa8d43cf00e7424a 100644 --- a/src/language/ASTNodeCFunctionExpressionBuilder.hpp +++ b/src/language/ASTNodeBuiltinFunctionExpressionBuilder.hpp @@ -6,10 +6,10 @@ #include <ASTNodeDataTypeFlattener.hpp> -class CFunctionProcessor; +class BuiltinFunctionProcessor; class IFunctionArgumentConverter; -class ASTNodeCFunctionExpressionBuilder +class ASTNodeBuiltinFunctionExpressionBuilder { private: PUGS_INLINE std::unique_ptr<IFunctionArgumentConverter> _getArgumentConverter( @@ -21,15 +21,15 @@ class ASTNodeCFunctionExpressionBuilder void _storeArgumentProcessor(const std::vector<ASTNodeDataType>& parameter_type_list, const ASTNodeDataTypeFlattener::FlattenedDataTypeList& flattened_datatype_list, const size_t argument_number, - CFunctionProcessor& c_function_processor); + BuiltinFunctionProcessor& _processor); PUGS_INLINE void _buildArgumentProcessors(const std::vector<ASTNodeDataType>& parameter_type_list, ASTNode& node, - CFunctionProcessor& c_function_processor); + BuiltinFunctionProcessor& _processor); public: - ASTNodeCFunctionExpressionBuilder(ASTNode& node); + ASTNodeBuiltinFunctionExpressionBuilder(ASTNode& node); }; #endif // AST_NODE_C_FUNCTION_EXPRESSION_BUILDER_HPP diff --git a/src/language/ASTNodeDataType.cpp b/src/language/ASTNodeDataType.cpp index c6191255b8d00b4ec25446b27ee5fc9865538ac1..c15091a2f1546b14290603d052f69b879c0cf139 100644 --- a/src/language/ASTNodeDataType.cpp +++ b/src/language/ASTNodeDataType.cpp @@ -55,9 +55,6 @@ dataTypeName(const ASTNodeDataType& data_type) case ASTNodeDataType::function_t: name = "function"; break; - case ASTNodeDataType::c_function_t: - name = "c_function"; - break; case ASTNodeDataType::builtin_function_t: name = "builtin_function"; break; diff --git a/src/language/ASTNodeDataType.hpp b/src/language/ASTNodeDataType.hpp index ee424e77557b39715e7c22088b0c080225429d9a..d3bce9a70c0df90ca06d71a49950f4fe03d0ddaf 100644 --- a/src/language/ASTNodeDataType.hpp +++ b/src/language/ASTNodeDataType.hpp @@ -20,11 +20,10 @@ class ASTNodeDataType list_t = 5, string_t = 6, typename_t = 10, - type_name_id_t = 20, + type_name_id_t = 11, type_id_t = 21, function_t = 22, - c_function_t = 23, - builtin_function_t = 24, + builtin_function_t = 23, void_t = std::numeric_limits<int32_t>::max() }; diff --git a/src/language/ASTNodeDataTypeBuilder.cpp b/src/language/ASTNodeDataTypeBuilder.cpp index 80c24227f09efabe13ad857b3dcc8737a5e253a1..04a410306f8e3b0a00fa75b11b54449d31b86b54 100644 --- a/src/language/ASTNodeDataTypeBuilder.cpp +++ b/src/language/ASTNodeDataTypeBuilder.cpp @@ -7,7 +7,6 @@ #include <SymbolTable.hpp> #include <BuiltinFunctionEmbedder.hpp> -#include <CFunctionEmbedder.hpp> ASTNodeDataType ASTNodeDataTypeBuilder::_buildDeclarationNodeDataTypes(ASTNode& type_node, ASTNode& name_node) const @@ -409,18 +408,6 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n) const Assert(data_type != ASTNodeDataType::undefined_t); // LCOV_EXCL_LINE n.m_data_type = data_type; - } else if (n.children[0]->m_data_type == ASTNodeDataType::c_function_t) { - const std::string c_function_name = n.children[0]->string(); - auto& symbol_table = *n.m_symbol_table; - - auto [i_symbol, success] = symbol_table.find(c_function_name, n.begin()); - Assert(success); - - uint64_t c_function_id = std::get<uint64_t>(i_symbol->attributes().value()); - auto c_function_embedder = symbol_table.cFunctionEmbedderTable()[c_function_id]; - Assert(c_function_embedder); - - n.m_data_type = c_function_embedder->getReturnDataType(); } else if (n.children[0]->m_data_type == ASTNodeDataType::builtin_function_t) { const std::string builtin_function_name = n.children[0]->string(); auto& symbol_table = *n.m_symbol_table; diff --git a/src/language/ASTNodeFunctionEvaluationExpressionBuilder.cpp b/src/language/ASTNodeFunctionEvaluationExpressionBuilder.cpp index 6c3edadfb7d3c31fbb1a4cfe8bebd34a00ea9ce5..125f2e8afb2a5298959a07017d1b7ca34a3af6f2 100644 --- a/src/language/ASTNodeFunctionEvaluationExpressionBuilder.cpp +++ b/src/language/ASTNodeFunctionEvaluationExpressionBuilder.cpp @@ -1,6 +1,6 @@ #include <ASTNodeFunctionEvaluationExpressionBuilder.hpp> -#include <ASTNodeCFunctionExpressionBuilder.hpp> +#include <ASTNodeBuiltinFunctionExpressionBuilder.hpp> #include <ASTNodeFunctionExpressionBuilder.hpp> #include <SymbolTable.hpp> @@ -15,8 +15,8 @@ ASTNodeFunctionEvaluationExpressionBuilder::ASTNodeFunctionEvaluationExpressionB ASTNodeFunctionExpressionBuilder{node}; break; } - case ASTNodeDataType::c_function_t: { - ASTNodeCFunctionExpressionBuilder{node}; + case ASTNodeDataType::builtin_function_t: { + ASTNodeBuiltinFunctionExpressionBuilder{node}; break; } // LCOV_EXCL_START diff --git a/src/language/BuiltinFunctionEmbedder.hpp b/src/language/BuiltinFunctionEmbedder.hpp index d1896ede4b9e80e0fed552e9c93732b5bbac865a..b9e4e96ca32b43f953eabb3297f5d6544b18d126 100644 --- a/src/language/BuiltinFunctionEmbedder.hpp +++ b/src/language/BuiltinFunctionEmbedder.hpp @@ -21,39 +21,95 @@ class IBuiltinFunctionEmbedder virtual ~IBuiltinFunctionEmbedder() = default; }; +template <typename FX, typename... Args> class BuiltinFunctionEmbedder : public IBuiltinFunctionEmbedder { private: - ASTNodeDataType m_return_data_type{ASTNodeDataType::type_id_t, "mesh"}; + std::function<FX(Args...)> m_f; + using ArgsTuple = std::tuple<Args...>; + + template <size_t I> + PUGS_INLINE void + _copy_value(ArgsTuple& t, const std::vector<DataVariant>& v) const + { + std::visit( + [&](auto v_i) { + if constexpr (std::is_arithmetic_v<decltype(v_i)>) { + std::get<I>(t) = v_i; + } else { + throw std::runtime_error("unexpected argument type!"); + } + }, + v[I]); + } + + template <size_t... I> + PUGS_INLINE void + _copy_from_vector(ArgsTuple& t, const std::vector<DataVariant>& v, std::index_sequence<I...>) const + { + Assert(sizeof...(Args) == v.size()); + (_copy_value<I>(t, v), ...); + } + + template <size_t I> + PUGS_INLINE ASTNodeDataType + _getOneParameterDataType(ArgsTuple& t) const + { + return ast_node_data_type_from_pod<std::decay_t<decltype(std::get<I>(t))>>; + } + + template <size_t... I> + PUGS_INLINE std::vector<ASTNodeDataType> + _getParameterDataTypes(ArgsTuple t, std::index_sequence<I...>) const + { + std::vector<ASTNodeDataType> parameter_type_list; + (parameter_type_list.push_back(this->_getOneParameterDataType<I>(t)), ...); + return parameter_type_list; + } public: - size_t - numberOfParameters() const + PUGS_INLINE ASTNodeDataType + getReturnDataType() const final { - return 1; + return ast_node_data_type_from_pod<FX>; } - ASTNodeDataType - getReturnDataType() const + PUGS_INLINE std::vector<ASTNodeDataType> + getParameterDataTypes() const final { - return m_return_data_type; + constexpr size_t N = std::tuple_size_v<ArgsTuple>; + ArgsTuple t; + using IndexSequence = std::make_index_sequence<N>; + + return this->_getParameterDataTypes(t, IndexSequence{}); } - std::vector<ASTNodeDataType> - getParameterDataTypes() const + PUGS_INLINE size_t + numberOfParameters() const final { - return {ASTNodeDataType::string_t}; + return sizeof...(Args); } + PUGS_INLINE DataVariant - apply(const std::vector<DataVariant>&) const + apply(const std::vector<DataVariant>& x) const final { - std::cerr << "NIY\n"; - return {}; + constexpr size_t N = std::tuple_size_v<ArgsTuple>; + ArgsTuple t; + using IndexSequence = std::make_index_sequence<N>; + + this->_copy_from_vector(t, x, IndexSequence{}); + return {std::apply(m_f, t)}; } - BuiltinFunctionEmbedder() = default; - ~BuiltinFunctionEmbedder() = default; + template <typename FX2, typename... Args2> + BuiltinFunctionEmbedder(std::function<FX2(Args2...)> f) : m_f(f) + { + static_assert(std::is_same_v<FX, FX2>, "incorrect return type"); + static_assert(sizeof...(Args) == sizeof...(Args2), "invalid number of arguments"); + using Args2Tuple = std::tuple<Args2...>; + static_assert(std::is_same_v<ArgsTuple, Args2Tuple>, "invalid arguments type"); + } }; #endif // BUILTIN_FUNCTION_EMBEDDER_HPP diff --git a/src/language/BuiltinModule.cpp b/src/language/BuiltinModule.cpp index bea61e9d78f39742172627a4810af7ce92777cae..96d2e4e06221f7ed8743599c3b4618a9b5b66ffd 100644 --- a/src/language/BuiltinModule.cpp +++ b/src/language/BuiltinModule.cpp @@ -1,22 +1,10 @@ #include <BuiltinModule.hpp> -#include <CFunctionEmbedder.hpp> +#include <BuiltinFunctionEmbedder.hpp> #include <TypeDescriptor.hpp> #include <iostream> -void -BuiltinModule::_addCFunction(const std::string& name, std::shared_ptr<ICFunctionEmbedder> c_function_embedder) -{ - auto [i_function, success] = m_name_cfunction_map.insert(std::make_pair(name, c_function_embedder)); - // LCOV_EXCL_START - if (not success) { - std::cerr << "function " << name << " cannot be added!\n"; - std::exit(1); - } - // LCOV_EXCL_STOP -} - void BuiltinModule::_addBuiltinFunction(const std::string& name, std::shared_ptr<IBuiltinFunctionEmbedder> builtin_function_embedder) @@ -25,7 +13,7 @@ BuiltinModule::_addBuiltinFunction(const std::string& name, m_name_builtin_function_map.insert(std::make_pair(name, builtin_function_embedder)); // LCOV_EXCL_START if (not success) { - std::cerr << "builtin function " << name << " cannot be added!\n"; + std::cerr << "builtin-function '" << name << "' cannot be added!\n"; std::exit(1); } // LCOV_EXCL_STOP diff --git a/src/language/BuiltinModule.hpp b/src/language/BuiltinModule.hpp index 7e0d599e93f82fbc64339e42261708f6b16ebcfb..ed932e9b133a564a4f1e905c0a403844eccab098 100644 --- a/src/language/BuiltinModule.hpp +++ b/src/language/BuiltinModule.hpp @@ -3,31 +3,21 @@ #include <IModule.hpp> -class ICFunctionEmbedder; class IBuiltinFunctionEmbedder; class TypeDescriptor; class BuiltinModule : public IModule { protected: - NameCFunctionMap m_name_cfunction_map; NameBuiltinFunctionMap m_name_builtin_function_map; NameTypeMap m_name_type_map; - void _addCFunction(const std::string& name, std::shared_ptr<ICFunctionEmbedder> c_function_embedder); - void _addBuiltinFunction(const std::string& name, std::shared_ptr<IBuiltinFunctionEmbedder> builtin_function_embedder); void _addTypeDescriptor(std::shared_ptr<TypeDescriptor> type_descriptor); public: - const NameCFunctionMap& - getNameCFunctionMap() const final - { - return m_name_cfunction_map; - } - const NameBuiltinFunctionMap& getNameBuiltinFunctionMap() const final { diff --git a/src/language/CFunctionEmbedder.hpp b/src/language/CFunctionEmbedder.hpp deleted file mode 100644 index ced2817c4e27c9dcf7b77c4b14d31ea40c8ebe6b..0000000000000000000000000000000000000000 --- a/src/language/CFunctionEmbedder.hpp +++ /dev/null @@ -1,126 +0,0 @@ -#ifndef CFUNCTION_EMBEDDER_HPP -#define CFUNCTION_EMBEDDER_HPP - -#include <PugsAssert.hpp> -#include <PugsMacros.hpp> - -#include <ASTNodeDataType.hpp> -#include <DataVariant.hpp> - -#include <cmath> -#include <functional> -#include <iostream> -#include <tuple> -#include <vector> - -#include <type_traits> - -class ICFunctionEmbedder -{ - public: - virtual size_t numberOfParameters() const = 0; - - virtual ASTNodeDataType getReturnDataType() const = 0; - - virtual std::vector<ASTNodeDataType> getParameterDataTypes() const = 0; - - virtual DataVariant apply(const std::vector<DataVariant>& x) const = 0; - - virtual ~ICFunctionEmbedder() = default; -}; - -template <typename FX, typename... Args> -class CFunctionEmbedder : public ICFunctionEmbedder -{ - private: - std::function<FX(Args...)> m_f; - using ArgsTuple = std::tuple<Args...>; - - template <size_t I> - PUGS_INLINE void - _copy_value(ArgsTuple& t, const std::vector<DataVariant>& v) const - { - std::visit( - [&](auto v_i) { - if constexpr (std::is_arithmetic_v<decltype(v_i)>) { - std::get<I>(t) = v_i; - } else { - throw std::runtime_error("unexpected argument type!"); - } - }, - v[I]); - } - - template <size_t... I> - PUGS_INLINE void - _copy_from_vector(ArgsTuple& t, const std::vector<DataVariant>& v, std::index_sequence<I...>) const - { - Assert(sizeof...(Args) == v.size()); - (_copy_value<I>(t, v), ...); - } - - template <size_t I> - PUGS_INLINE ASTNodeDataType - _getOneParameterDataType(ArgsTuple& t) const - { - return ast_node_data_type_from_pod<std::decay_t<decltype(std::get<I>(t))>>; - } - - template <size_t... I> - PUGS_INLINE std::vector<ASTNodeDataType> - _getParameterDataTypes(ArgsTuple t, std::index_sequence<I...>) const - { - std::vector<ASTNodeDataType> parameter_type_list; - (parameter_type_list.push_back(this->_getOneParameterDataType<I>(t)), ...); - return parameter_type_list; - } - - public: - PUGS_INLINE ASTNodeDataType - getReturnDataType() const final - { - return ast_node_data_type_from_pod<FX>; - } - - PUGS_INLINE std::vector<ASTNodeDataType> - getParameterDataTypes() const final - { - constexpr size_t N = std::tuple_size_v<ArgsTuple>; - ArgsTuple t; - using IndexSequence = std::make_index_sequence<N>; - - return this->_getParameterDataTypes(t, IndexSequence{}); - } - - PUGS_INLINE size_t - numberOfParameters() const final - { - return sizeof...(Args); - } - - PUGS_INLINE - DataVariant - apply(const std::vector<DataVariant>& x) const final - { - constexpr size_t N = std::tuple_size_v<ArgsTuple>; - ArgsTuple t; - using IndexSequence = std::make_index_sequence<N>; - - this->_copy_from_vector(t, x, IndexSequence{}); - return {std::apply(m_f, t)}; - } - - // @note This is written in a template fashion to ensure that function type - // is correct. If one uses simply CFunctionEmbedder(std::function<FX(Args...)>&&), - // types seem unchecked - template <typename FX2, typename... Args2> - CFunctionEmbedder(std::function<FX2(Args2...)> f) : m_f(f) - { - static_assert(std::is_same_v<FX, FX2>, "incorrect return type"); - static_assert(sizeof...(Args) == sizeof...(Args2), "invalid number of arguments"); - using Args2Tuple = std::tuple<Args2...>; - static_assert(std::is_same_v<ArgsTuple, Args2Tuple>, "invalid arguments type"); - } -}; - -#endif // CFUNCTION_EMBEDDER_HPP diff --git a/src/language/CMakeLists.txt b/src/language/CMakeLists.txt index 96e3a986bee0b0e4a6c10d8657c8a323b2c262f5..683ef786049fc0e0c43c424f05e02687c365d056 100644 --- a/src/language/CMakeLists.txt +++ b/src/language/CMakeLists.txt @@ -13,7 +13,7 @@ add_library( ASTNodeAffectationExpressionBuilder.cpp ASTNodeArraySubscriptExpressionBuilder.cpp ASTNodeBinaryOperatorExpressionBuilder.cpp - ASTNodeCFunctionExpressionBuilder.cpp + ASTNodeBuiltinFunctionExpressionBuilder.cpp ASTNodeDataType.cpp ASTNodeDataTypeFlattener.cpp ASTNodeDataTypeBuilder.cpp diff --git a/src/language/IModule.hpp b/src/language/IModule.hpp index deb4857146cd898f4553a963945d1e6b874a5ffd..b839ed496d8d032f660a6d21c484c7ca342c5f98 100644 --- a/src/language/IModule.hpp +++ b/src/language/IModule.hpp @@ -6,14 +6,12 @@ #include <string_view> #include <unordered_map> -class ICFunctionEmbedder; class IBuiltinFunctionEmbedder; class TypeDescriptor; class IModule { public: - using NameCFunctionMap = std::unordered_map<std::string, std::shared_ptr<ICFunctionEmbedder>>; using NameBuiltinFunctionMap = std::unordered_map<std::string, std::shared_ptr<IBuiltinFunctionEmbedder>>; using NameTypeMap = std::unordered_map<std::string, std::shared_ptr<TypeDescriptor>>; @@ -21,8 +19,6 @@ class IModule IModule(IModule&&) = default; IModule& operator=(IModule&&) = default; - virtual const NameCFunctionMap& getNameCFunctionMap() const = 0; - virtual const NameBuiltinFunctionMap& getNameBuiltinFunctionMap() const = 0; virtual const NameTypeMap& getNameTypeMap() const = 0; diff --git a/src/language/MathModule.cpp b/src/language/MathModule.cpp index 4f70162adc4769c462b11d4f39a189951e654e2c..9a7ff74644f8a4502f6d1cf538c0911d4e51d92c 100644 --- a/src/language/MathModule.cpp +++ b/src/language/MathModule.cpp @@ -1,74 +1,81 @@ #include <MathModule.hpp> -#include <CFunctionEmbedder.hpp> +#include <BuiltinFunctionEmbedder.hpp> MathModule::MathModule() { - this->_addCFunction("sqrt", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::sqrt(x); }})); + this->_addBuiltinFunction("sqrt", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::sqrt(x); }})); - this->_addCFunction("abs", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::abs(x); }})); + this->_addBuiltinFunction("abs", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::abs(x); }})); - this->_addCFunction("sin", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::sin(x); }})); + this->_addBuiltinFunction("sin", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::sin(x); }})); - this->_addCFunction("cos", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::cos(x); }})); + this->_addBuiltinFunction("cos", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::cos(x); }})); - this->_addCFunction("tan", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::tan(x); }})); + this->_addBuiltinFunction("tan", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::tan(x); }})); - this->_addCFunction("asin", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::asin(x); }})); + this->_addBuiltinFunction("asin", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::asin(x); }})); - this->_addCFunction("acos", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::acos(x); }})); + this->_addBuiltinFunction("acos", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::acos(x); }})); - this->_addCFunction("atan", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::atan(x); }})); + this->_addBuiltinFunction("atan", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::atan(x); }})); - this->_addCFunction("atan2", - std::make_shared<CFunctionEmbedder<double, double, double>>(std::function<double(double, double)>{ - [](double x, double y) -> double { return std::atan2(x, y); }})); + this->_addBuiltinFunction("atan2", std::make_shared<BuiltinFunctionEmbedder<double, double, double>>( + std::function<double(double, double)>{ + [](double x, double y) -> double { return std::atan2(x, y); }})); - this->_addCFunction("sinh", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::sinh(x); }})); + this->_addBuiltinFunction("sinh", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::sinh(x); }})); - this->_addCFunction("cosh", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::cosh(x); }})); + this->_addBuiltinFunction("cosh", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::cosh(x); }})); - this->_addCFunction("tanh", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::tanh(x); }})); + this->_addBuiltinFunction("tanh", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::tanh(x); }})); - this->_addCFunction("asinh", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::asinh(x); }})); + this->_addBuiltinFunction("asinh", + std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::asinh(x); }})); - this->_addCFunction("acosh", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::acosh(x); }})); + this->_addBuiltinFunction("acosh", + std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::acosh(x); }})); - this->_addCFunction("atanh", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::atanh(x); }})); + this->_addBuiltinFunction("atanh", + std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::atanh(x); }})); - this->_addCFunction("exp", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::exp(x); }})); + this->_addBuiltinFunction("exp", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::exp(x); }})); - this->_addCFunction("log", std::make_shared<CFunctionEmbedder<double, double>>( - std::function<double(double)>{[](double x) -> double { return std::log(x); }})); + this->_addBuiltinFunction("log", std::make_shared<BuiltinFunctionEmbedder<double, double>>( + std::function<double(double)>{[](double x) -> double { return std::log(x); }})); - this->_addCFunction("pow", - std::make_shared<CFunctionEmbedder<double, double, double>>(std::function<double(double, double)>{ - [](double x, double y) -> double { return std::pow(x, y); }})); + this->_addBuiltinFunction("pow", std::make_shared<BuiltinFunctionEmbedder<double, double, double>>( + std::function<double(double, double)>{ + [](double x, double y) -> double { return std::pow(x, y); }})); - this->_addCFunction("ceil", std::make_shared<CFunctionEmbedder<int64_t, double>>( - std::function<int64_t(double)>{[](double x) -> int64_t { return std::ceil(x); }})); + this->_addBuiltinFunction("ceil", + std::make_shared<BuiltinFunctionEmbedder<int64_t, double>>( + std::function<int64_t(double)>{[](double x) -> int64_t { return std::ceil(x); }})); - this->_addCFunction("floor", std::make_shared<CFunctionEmbedder<int64_t, double>>( - std::function<int64_t(double)>{[](double x) -> int64_t { return std::floor(x); }})); + this->_addBuiltinFunction("floor", + std::make_shared<BuiltinFunctionEmbedder<int64_t, double>>( + std::function<int64_t(double)>{[](double x) -> int64_t { return std::floor(x); }})); - this->_addCFunction("trunc", std::make_shared<CFunctionEmbedder<int64_t, double>>( - std::function<int64_t(double)>{[](double x) -> int64_t { return std::trunc(x); }})); + this->_addBuiltinFunction("trunc", + std::make_shared<BuiltinFunctionEmbedder<int64_t, double>>( + std::function<int64_t(double)>{[](double x) -> int64_t { return std::trunc(x); }})); - this->_addCFunction("round", std::make_shared<CFunctionEmbedder<int64_t, double>>( - std::function<int64_t(double)>{[](double x) -> int64_t { return std::lround(x); }})); + this->_addBuiltinFunction("round", + std::make_shared<BuiltinFunctionEmbedder<int64_t, double>>( + std::function<int64_t(double)>{[](double x) -> int64_t { return std::lround(x); }})); } diff --git a/src/language/MeshModule.cpp b/src/language/MeshModule.cpp index 1b33eedca1473a39c6488042731ba9a37e58d2d0..a145db0e72355d880422088cb00d16721fbd07ca 100644 --- a/src/language/MeshModule.cpp +++ b/src/language/MeshModule.cpp @@ -7,5 +7,7 @@ MeshModule::MeshModule() { this->_addTypeDescriptor(std::make_shared<TypeDescriptor>(std::string{"mesh"})); - this->_addBuiltinFunction("readGmsh", std::make_shared<BuiltinFunctionEmbedder>()); + this->_addBuiltinFunction("readGmsh", std::make_shared<BuiltinFunctionEmbedder<double, std::string>>( + std::function<double(std::string)>{ + [](std::string filename) -> double { return filename.size(); }})); } diff --git a/src/language/ModuleRepository.cpp b/src/language/ModuleRepository.cpp index bacda2efa3c7d17e080aaaf7e9367b231383051c..18bcd37a97a8853b3dbb27c66638b6535b46bf25 100644 --- a/src/language/ModuleRepository.cpp +++ b/src/language/ModuleRepository.cpp @@ -4,7 +4,7 @@ #include <ASTNode.hpp> -#include <CFunctionEmbedder.hpp> +#include <BuiltinFunctionEmbedder.hpp> #include <SymbolTable.hpp> #include <MathModule.hpp> @@ -60,9 +60,6 @@ ModuleRepository::populateSymbolTable(const ASTNode& module_name_node, SymbolTab if (i_module != m_module_set.end()) { const IModule& populating_module = *i_module->second; - this->_populateEmbedderTableT(module_name_node, populating_module.getNameCFunctionMap(), - ASTNodeDataType::c_function_t, symbol_table, symbol_table.cFunctionEmbedderTable()); - this->_populateEmbedderTableT(module_name_node, populating_module.getNameBuiltinFunctionMap(), ASTNodeDataType::builtin_function_t, symbol_table, symbol_table.builtinFunctionEmbedderTable()); diff --git a/src/language/SymbolTable.hpp b/src/language/SymbolTable.hpp index 3105957cc44277d4709812c38ea0c43ab09ee0e6..970261ddc7208ff4b0d3699b1576b17f41828718 100644 --- a/src/language/SymbolTable.hpp +++ b/src/language/SymbolTable.hpp @@ -15,7 +15,6 @@ #include <EmbedderTable.hpp> class TypeDescriptor; -class ICFunctionEmbedder; class IBuiltinFunctionEmbedder; class SymbolTable @@ -204,7 +203,6 @@ class SymbolTable std::shared_ptr<FunctionTable> m_function_table; - std::shared_ptr<EmbedderTable<ICFunctionEmbedder>> m_c_function_embedder_table; std::shared_ptr<EmbedderTable<IBuiltinFunctionEmbedder>> m_builtin_function_embedder_table; std::shared_ptr<EmbedderTable<TypeDescriptor>> m_type_embedder_table; @@ -236,20 +234,6 @@ class SymbolTable return *m_function_table; } - const auto& - cFunctionEmbedderTable() const - { - Assert(m_c_function_embedder_table); - return *m_c_function_embedder_table; - } - - auto& - cFunctionEmbedderTable() - { - Assert(m_c_function_embedder_table); - return *m_c_function_embedder_table; - } - const auto& builtinFunctionEmbedderTable() const { @@ -283,7 +267,7 @@ class SymbolTable { os << "-- Symbol table state -- parent : " << symbol_table.m_parent_table.get() << "\n"; for (auto i_symbol : symbol_table.m_symbol_list) { - if (i_symbol.attributes().dataType() != ASTNodeDataType::c_function_t) { + if (i_symbol.attributes().dataType() != ASTNodeDataType::builtin_function_t) { os << ' ' << i_symbol.name() << ": " << std::boolalpha << i_symbol.attributes() << '\n'; } } @@ -339,7 +323,6 @@ class SymbolTable : m_parent_table{parent_table}, m_context{context}, m_function_table{parent_table->m_function_table}, - m_c_function_embedder_table{parent_table->m_c_function_embedder_table}, m_builtin_function_embedder_table{parent_table->m_builtin_function_embedder_table}, m_type_embedder_table{parent_table->m_type_embedder_table} { @@ -355,7 +338,6 @@ class SymbolTable : m_parent_table{nullptr}, m_context{nullptr}, m_function_table{std::make_shared<FunctionTable>()}, - m_c_function_embedder_table{std::make_shared<EmbedderTable<ICFunctionEmbedder>>()}, m_builtin_function_embedder_table{std::make_shared<EmbedderTable<IBuiltinFunctionEmbedder>>()}, m_type_embedder_table{std::make_shared<EmbedderTable<TypeDescriptor>>()} { diff --git a/src/language/node_processor/CFunctionProcessor.hpp b/src/language/node_processor/BuiltinFunctionProcessor.hpp similarity index 72% rename from src/language/node_processor/CFunctionProcessor.hpp rename to src/language/node_processor/BuiltinFunctionProcessor.hpp index e41eb2ca456fab00138cb0df4b6b279c04be6124..31a561c90498f6141b57b8b453e069cb8f6f869a 100644 --- a/src/language/node_processor/CFunctionProcessor.hpp +++ b/src/language/node_processor/BuiltinFunctionProcessor.hpp @@ -1,32 +1,30 @@ -#ifndef CFUNCTION_PROCESSOR_HPP -#define CFUNCTION_PROCESSOR_HPP +#ifndef BUILTIN_FUNCTION_PROCESSOR_HPP +#define BUILTIN_FUNCTION_PROCESSOR_HPP #include <PEGGrammar.hpp> #include <node_processor/INodeProcessor.hpp> -#include <CFunctionEmbedder.hpp> +#include <BuiltinFunctionEmbedder.hpp> #include <node_processor/FunctionArgumentConverter.hpp> -class CFunctionExpressionProcessor final : public INodeProcessor +class BuiltinFunctionExpressionProcessor final : public INodeProcessor { private: - std::shared_ptr<ICFunctionEmbedder> m_embedded_c_function; + std::shared_ptr<IBuiltinFunctionEmbedder> m_embedded_; public: DataVariant execute(ExecutionPolicy& exec_policy) { - return m_embedded_c_function->apply(exec_policy.currentContext().values()); + return m_embedded_->apply(exec_policy.currentContext().values()); } - CFunctionExpressionProcessor(std::shared_ptr<ICFunctionEmbedder> embedded_c_function) - : m_embedded_c_function(embedded_c_function) - {} + BuiltinFunctionExpressionProcessor(std::shared_ptr<IBuiltinFunctionEmbedder> embedded_) : m_embedded_(embedded_) {} }; -class CFunctionProcessor : public INodeProcessor +class BuiltinFunctionProcessor : public INodeProcessor { private: ASTNode& m_argument_node; @@ -68,7 +66,7 @@ class CFunctionProcessor : public INodeProcessor return m_function_expression_processor->execute(context_exec_policy); } - CFunctionProcessor(ASTNode& argument_node) : m_argument_node{argument_node} {} + BuiltinFunctionProcessor(ASTNode& argument_node) : m_argument_node{argument_node} {} }; -#endif // CFUNCTION_PROCESSOR_HPP +#endif // BUILTIN_FUNCTION_PROCESSOR_HPP diff --git a/src/language/node_processor/OStreamProcessor.hpp b/src/language/node_processor/OStreamProcessor.hpp index 8ef721696956a331b4c2ee4b35e31c6023f80a93..78dac8a47ce2c99c30ba51bf0cab37f8c9a7b9d4 100644 --- a/src/language/node_processor/OStreamProcessor.hpp +++ b/src/language/node_processor/OStreamProcessor.hpp @@ -36,7 +36,7 @@ class OStreamProcessor final : public INodeProcessor for (auto& child : m_node.children) { if ((child->m_data_type == ASTNodeDataType::type_name_id_t) or (child->m_data_type == ASTNodeDataType::function_t) or - (child->m_data_type == ASTNodeDataType::c_function_t)) { + (child->m_data_type == ASTNodeDataType::builtin_function_t)) { throw parse_error("invalid argument, cannot print a '" + dataTypeName(child->m_data_type) + "'", std::vector{child->begin()}); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 447146b6f0c06bc3e06f4bfb6d44fbe86debea29..3801492358455b25801deabedf2eed2fae6e6ac4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,7 +15,7 @@ add_executable (unit_tests test_ASTNodeAffectationExpressionBuilder.cpp test_ASTNodeArraySubscriptExpressionBuilder.cpp test_ASTNodeBinaryOperatorExpressionBuilder.cpp - test_ASTNodeCFunctionExpressionBuilder.cpp + test_ASTNodeBuiltinFunctionExpressionBuilder.cpp test_ASTNodeDataType.cpp test_ASTNodeDataTypeBuilder.cpp test_ASTNodeDataTypeChecker.cpp @@ -41,9 +41,9 @@ add_executable (unit_tests test_BinaryExpressionProcessor_equality.cpp test_BinaryExpressionProcessor_logic.cpp test_BiCGStab.cpp - test_CFunctionEmbedder.cpp - test_CFunctionEmbedderTable.cpp - test_CFunctionProcessor.cpp + test_BuiltinFunctionEmbedder.cpp + test_BuiltinFunctionEmbedderTable.cpp + test_BuiltinFunctionProcessor.cpp test_MathModule.cpp test_ContinueProcessor.cpp test_ConcatExpressionProcessor.cpp diff --git a/tests/test_ASTNodeCFunctionExpressionBuilder.cpp b/tests/test_ASTNodeBuiltinFunctionExpressionBuilder.cpp similarity index 79% rename from tests/test_ASTNodeCFunctionExpressionBuilder.cpp rename to tests/test_ASTNodeBuiltinFunctionExpressionBuilder.cpp index 99deff5cf5eb1043dca4a8b1db8cd9cfa4305853..d8ea59f78810c4e56811a85cbc63512a39da95dc 100644 --- a/tests/test_ASTNodeCFunctionExpressionBuilder.cpp +++ b/tests/test_ASTNodeBuiltinFunctionExpressionBuilder.cpp @@ -10,7 +10,7 @@ #include <ASTNodeFunctionExpressionBuilder.hpp> #include <ASTNodeTypeCleaner.hpp> -#include <CFunctionEmbedder.hpp> +#include <BuiltinFunctionEmbedder.hpp> #include <ASTSymbolTableBuilder.hpp> @@ -24,50 +24,51 @@ namespace test_only { -class CFunctionRegister +class BuiltinFunctionRegister { private: - std::unordered_map<std::string, std::shared_ptr<ICFunctionEmbedder>> m_name_cfunction_map; + std::unordered_map<std::string, std::shared_ptr<IBuiltinFunctionEmbedder>> m_name_builtin_function_map; void - _populateNameCFunctionMap() + _populateNameBuiltinFunctionMap() { - m_name_cfunction_map.insert( - std::make_pair("RtoR", std::make_shared<CFunctionEmbedder<double, double>>( + m_name_builtin_function_map.insert( + std::make_pair("RtoR", std::make_shared<BuiltinFunctionEmbedder<double, double>>( std::function<double(double)>{[](double x) -> double { return x + 1; }}))); - m_name_cfunction_map.insert( - std::make_pair("ZtoR", std::make_shared<CFunctionEmbedder<double, int64_t>>( + m_name_builtin_function_map.insert( + std::make_pair("ZtoR", std::make_shared<BuiltinFunctionEmbedder<double, int64_t>>( std::function<double(int64_t)>{[](int64_t z) -> double { return 0.5 * z; }}))); - m_name_cfunction_map.insert( - std::make_pair("NtoR", std::make_shared<CFunctionEmbedder<double, uint64_t>>( + m_name_builtin_function_map.insert( + std::make_pair("NtoR", std::make_shared<BuiltinFunctionEmbedder<double, uint64_t>>( std::function<double(uint64_t)>{[](uint64_t n) -> double { return 0.5 * n; }}))); - m_name_cfunction_map.insert( - std::make_pair("BtoR", std::make_shared<CFunctionEmbedder<double, bool>>( + m_name_builtin_function_map.insert( + std::make_pair("BtoR", std::make_shared<BuiltinFunctionEmbedder<double, bool>>( std::function<double(bool)>{[](bool b) -> double { return b; }}))); - m_name_cfunction_map.insert(std::make_pair("R2toB", std::make_shared<CFunctionEmbedder<bool, double, double>>( - std::function<bool(double, double)>{ - [](double x, double y) -> bool { return x > y; }}))); + m_name_builtin_function_map.insert( + std::make_pair("R2toB", + std::make_shared<BuiltinFunctionEmbedder<bool, double, double>>( + std::function<bool(double, double)>{[](double x, double y) -> bool { return x > y; }}))); - m_name_cfunction_map.insert( + m_name_builtin_function_map.insert( std::make_pair("StoB_invalid", - std::make_shared<CFunctionEmbedder<bool, std::string>>( + std::make_shared<BuiltinFunctionEmbedder<bool, std::string>>( std::function<bool(std::string)>{[](std::string s) -> bool { return s.size() > 0; }}))); } public: - CFunctionRegister(ASTNode& root_node) + BuiltinFunctionRegister(ASTNode& root_node) { SymbolTable& symbol_table = *root_node.m_symbol_table; - auto& c_function_embedder_table = symbol_table.cFunctionEmbedderTable(); + auto& builtin_function_embedder_table = symbol_table.builtinFunctionEmbedderTable(); - this->_populateNameCFunctionMap(); + this->_populateNameBuiltinFunctionMap(); - for (auto [symbol_name, c_function] : m_name_cfunction_map) { + for (auto [symbol_name, builtin_function] : m_name_builtin_function_map) { auto [i_symbol, success] = symbol_table.add(symbol_name, root_node.begin()); if (not success) { @@ -76,11 +77,11 @@ class CFunctionRegister throw parse_error(error_message.str(), root_node.begin()); } - i_symbol->attributes().setDataType(ASTNodeDataType::c_function_t); + i_symbol->attributes().setDataType(ASTNodeDataType::builtin_function_t); i_symbol->attributes().setIsInitialized(); - i_symbol->attributes().value() = c_function_embedder_table.size(); + i_symbol->attributes().value() = builtin_function_embedder_table.size(); - c_function_embedder_table.add(c_function); + builtin_function_embedder_table.add(builtin_function); } } }; @@ -95,7 +96,7 @@ class CFunctionRegister string_input input{data, "test.pgs"}; \ auto ast = ASTBuilder::build(input); \ \ - test_only::CFunctionRegister{*ast}; \ + test_only::BuiltinFunctionRegister{*ast}; \ \ ASTSymbolTableBuilder{*ast}; \ ASTNodeDataTypeBuilder{*ast}; \ @@ -118,7 +119,7 @@ class CFunctionRegister string_input input{data, "test.pgs"}; \ auto ast = ASTBuilder::build(input); \ \ - test_only::CFunctionRegister{*ast}; \ + test_only::BuiltinFunctionRegister{*ast}; \ \ ASTSymbolTableBuilder{*ast}; \ ASTNodeDataTypeBuilder{*ast}; \ @@ -127,7 +128,7 @@ class CFunctionRegister REQUIRE_THROWS_WITH(ASTNodeExpressionBuilder{*ast}, Catch::Matchers::Contains(expected_error)); \ } -TEST_CASE("ASTNodeCFunctionExpressionBuilder", "[language]") +TEST_CASE("ASTNodeBuiltinFunctionExpressionBuilder", "[language]") { SECTION("R -> R") { @@ -139,7 +140,7 @@ RtoR(1.); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:RtoR:NameProcessor) `-(language::real:1.:ValueProcessor) )"; @@ -155,7 +156,7 @@ RtoR(1); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:RtoR:NameProcessor) `-(language::integer:1:ValueProcessor) )"; @@ -172,7 +173,7 @@ RtoR(n); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:RtoR:NameProcessor) `-(language::name:n:NameProcessor) )"; @@ -188,7 +189,7 @@ RtoR(true); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:RtoR:NameProcessor) `-(language::true_kw:ValueProcessor) )"; @@ -207,7 +208,7 @@ ZtoR(1); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:ZtoR:NameProcessor) `-(language::integer:1:ValueProcessor) )"; @@ -224,7 +225,7 @@ ZtoR(n); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:ZtoR:NameProcessor) `-(language::name:n:NameProcessor) )"; @@ -240,7 +241,7 @@ ZtoR(true); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:ZtoR:NameProcessor) `-(language::true_kw:ValueProcessor) )"; @@ -259,7 +260,7 @@ NtoR(1); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:NtoR:NameProcessor) `-(language::integer:1:ValueProcessor) )"; @@ -276,7 +277,7 @@ NtoR(n); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:NtoR:NameProcessor) `-(language::name:n:NameProcessor) )"; @@ -292,7 +293,7 @@ NtoR(true); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:NtoR:NameProcessor) `-(language::true_kw:ValueProcessor) )"; @@ -311,7 +312,7 @@ BtoR(true); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:BtoR:NameProcessor) `-(language::true_kw:ValueProcessor) )"; @@ -328,7 +329,7 @@ R2toB(1., 0.); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:R2toB:NameProcessor) `-(language::function_argument_list:ASTNodeExpressionListProcessor) +-(language::real:1.:ValueProcessor) diff --git a/tests/test_ASTNodeDataType.cpp b/tests/test_ASTNodeDataType.cpp index 7771e3be391cd09e55a42d0dab4ea76749085c89..0bbf2507f56f34b713741bcf7155a02d91a89214 100644 --- a/tests/test_ASTNodeDataType.cpp +++ b/tests/test_ASTNodeDataType.cpp @@ -18,7 +18,7 @@ TEST_CASE("ASTNodeDataType", "[language]") REQUIRE(dataTypeName(ASTNodeDataType::typename_t) == "typename"); REQUIRE(dataTypeName(ASTNodeDataType::void_t) == "void"); REQUIRE(dataTypeName(ASTNodeDataType::function_t) == "function"); - REQUIRE(dataTypeName(ASTNodeDataType::c_function_t) == "c_function"); + REQUIRE(dataTypeName(ASTNodeDataType::builtin_function_t) == "builtin_function"); REQUIRE(dataTypeName(ASTNodeDataType::list_t) == "list"); REQUIRE(dataTypeName(ASTNodeDataType{ASTNodeDataType::vector_t, 1}) == "R^1"); REQUIRE(dataTypeName(ASTNodeDataType{ASTNodeDataType::vector_t, 2}) == "R^2"); diff --git a/tests/test_ASTNodeFunctionEvaluationExpressionBuilder.cpp b/tests/test_ASTNodeFunctionEvaluationExpressionBuilder.cpp index 02ef25691b75e6939bf52eb2da07cbc620680f00..f3dcdc37113ebaabf8553e04b59598e97ba46195 100644 --- a/tests/test_ASTNodeFunctionEvaluationExpressionBuilder.cpp +++ b/tests/test_ASTNodeFunctionEvaluationExpressionBuilder.cpp @@ -52,7 +52,7 @@ sin(3); std::string_view result = R"( (root:ASTNodeListProcessor) - `-(language::function_evaluation:CFunctionProcessor) + `-(language::function_evaluation:BuiltinFunctionProcessor) +-(language::name:sin:NameProcessor) `-(language::integer:3:ValueProcessor) )"; diff --git a/tests/test_CFunctionEmbedder.cpp b/tests/test_BuiltinFunctionEmbedder.cpp similarity index 82% rename from tests/test_CFunctionEmbedder.cpp rename to tests/test_BuiltinFunctionEmbedder.cpp index 8b8213f69ec8410fcf3865bf6288bff9472f0626..a640bb52efcefd99c427672eed9b654e40ffc1c2 100644 --- a/tests/test_CFunctionEmbedder.cpp +++ b/tests/test_BuiltinFunctionEmbedder.cpp @@ -1,14 +1,14 @@ #include <catch2/catch.hpp> -#include <CFunctionEmbedder.hpp> +#include <BuiltinFunctionEmbedder.hpp> -TEST_CASE("CFunctionEmbedder", "[language]") +TEST_CASE("BuiltinFunctionEmbedder", "[language]") { rang::setControlMode(rang::control::Off); SECTION("math") { - CFunctionEmbedder<double, double> embedded_sin{ + BuiltinFunctionEmbedder<double, double> embedded_sin{ std::function<double(double)>{[](double x) -> double { return std::sin(x); }}}; double arg = 2; @@ -27,7 +27,7 @@ TEST_CASE("CFunctionEmbedder", "[language]") { std::function<bool(double, uint64_t)> c = [](double x, uint64_t i) -> bool { return x > i; }; - CFunctionEmbedder<bool, double, uint64_t> embedded_c{c}; + BuiltinFunctionEmbedder<bool, double, uint64_t> embedded_c{c}; double d_arg = 2.3; uint64_t i_arg = 3; @@ -46,11 +46,12 @@ TEST_CASE("CFunctionEmbedder", "[language]") REQUIRE(embedded_c.getParameterDataTypes()[1] == ASTNodeDataType::unsigned_int_t); } - SECTION("ICFunctionEmbedder") + SECTION("IBuiltinFunctionEmbedder") { std::function<bool(double, uint64_t)> c = [](double x, uint64_t i) -> bool { return x > i; }; - std::unique_ptr<ICFunctionEmbedder> i_embedded_c = std::make_unique<CFunctionEmbedder<bool, double, uint64_t>>(c); + std::unique_ptr<IBuiltinFunctionEmbedder> i_embedded_c = + std::make_unique<BuiltinFunctionEmbedder<bool, double, uint64_t>>(c); double d_arg = 2.3; uint64_t i_arg = 3; @@ -73,7 +74,7 @@ TEST_CASE("CFunctionEmbedder", "[language]") { std::function<bool(double)> positive = [](double x) -> bool { return x >= 0; }; - CFunctionEmbedder<bool, double> embedded_positive{positive}; + BuiltinFunctionEmbedder<bool, double> embedded_positive{positive}; std::string arg = std::string{"2.3"}; diff --git a/tests/test_CFunctionEmbedderTable.cpp b/tests/test_BuiltinFunctionEmbedderTable.cpp similarity index 60% rename from tests/test_CFunctionEmbedderTable.cpp rename to tests/test_BuiltinFunctionEmbedderTable.cpp index e75155f76152abd96a7335fcfa61d638961d390b..0f66fbf3599ae5d84d32e1fdb03d92f6ee43b1a1 100644 --- a/tests/test_CFunctionEmbedderTable.cpp +++ b/tests/test_BuiltinFunctionEmbedderTable.cpp @@ -1,24 +1,25 @@ #include <catch2/catch.hpp> -#include <CFunctionEmbedder.hpp> +#include <BuiltinFunctionEmbedder.hpp> #include <EmbedderTable.hpp> -TEST_CASE("CFunctionEmbedderTable", "[language]") +TEST_CASE("BuiltinFunctionEmbedderTable", "[language]") { rang::setControlMode(rang::control::Off); - EmbedderTable<ICFunctionEmbedder> table; + EmbedderTable<IBuiltinFunctionEmbedder> table; REQUIRE(table.size() == 0); - std::shared_ptr<ICFunctionEmbedder> embedded_sin = std::make_shared<CFunctionEmbedder<double, double>>( + std::shared_ptr<IBuiltinFunctionEmbedder> embedded_sin = std::make_shared<BuiltinFunctionEmbedder<double, double>>( std::function<double(double)>{[](double x) -> double { return std::sin(x); }}); table.add(embedded_sin); REQUIRE(table.size() == 1); - std::shared_ptr<ICFunctionEmbedder> embedded_greater = std::make_shared<CFunctionEmbedder<bool, int, int>>( - std::function<bool(int, int)>{[](int i, int j) -> bool { return i > j; }}); + std::shared_ptr<IBuiltinFunctionEmbedder> embedded_greater = + std::make_shared<BuiltinFunctionEmbedder<bool, int, int>>( + std::function<bool(int, int)>{[](int i, int j) -> bool { return i > j; }}); table.add(embedded_greater); REQUIRE(table.size() == 2); diff --git a/tests/test_CFunctionProcessor.cpp b/tests/test_BuiltinFunctionProcessor.cpp similarity index 76% rename from tests/test_CFunctionProcessor.cpp rename to tests/test_BuiltinFunctionProcessor.cpp index 2a19c7263240e9ac7471d32a6a11198e078a24dc..f004d1981de9bde3522624f2781edc2828fc8542 100644 --- a/tests/test_CFunctionProcessor.cpp +++ b/tests/test_BuiltinFunctionProcessor.cpp @@ -14,7 +14,7 @@ #include <MathModule.hpp> -#define CHECK_CFUNCTION_EVALUATION_RESULT(data, variable_name, expected_value) \ +#define CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, variable_name, expected_value) \ { \ string_input input{data, "test.pgs"}; \ auto ast = ASTBuilder::build(input); \ @@ -51,7 +51,7 @@ REQUIRE(value == expected_value); \ } -TEST_CASE("CFunctionProcessor", "[language]") +TEST_CASE("BuiltinFunctionProcessor", "[language]") { SECTION("math module functions") { @@ -65,7 +65,7 @@ TEST_CASE("CFunctionProcessor", "[language]") import math; let x:R, x = sqrt(4); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::sqrt(4l)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::sqrt(4l)}); } { // abs @@ -74,7 +74,7 @@ let x:R, x = sqrt(4); import math; let x:R, x = abs(-3.4); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::abs(-3.4)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::abs(-3.4)}); } { // sin @@ -83,7 +83,7 @@ let x:R, x = abs(-3.4); import math; let x:R, x = sin(1.3); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::sin(1.3)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::sin(1.3)}); } { // cos @@ -92,7 +92,7 @@ let x:R, x = sin(1.3); import math; let x:R, x = cos(1.3); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::cos(1.3)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::cos(1.3)}); } { // tan @@ -101,7 +101,7 @@ let x:R, x = cos(1.3); import math; let x:R, x = tan(1.3); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::tan(1.3)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::tan(1.3)}); } { // asin @@ -110,7 +110,7 @@ let x:R, x = tan(1.3); import math; let x:R, x = asin(0.7); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::asin(0.7)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::asin(0.7)}); } { // acos @@ -119,7 +119,7 @@ let x:R, x = asin(0.7); import math; let x:R, x = acos(0.7); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::acos(0.7)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::acos(0.7)}); } { // atan @@ -128,7 +128,7 @@ let x:R, x = acos(0.7); import math; let x:R, x = atan(0.7); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::atan(0.7)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::atan(0.7)}); } { // atan2 @@ -137,7 +137,7 @@ let x:R, x = atan(0.7); import math; let x:R, x = atan2(0.7, 0.4); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::atan2(0.7, 0.4)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::atan2(0.7, 0.4)}); } { // sinh @@ -146,7 +146,7 @@ let x:R, x = atan2(0.7, 0.4); import math; let x:R, x = sinh(0.6); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::sinh(0.6)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::sinh(0.6)}); } { // cosh @@ -155,7 +155,7 @@ let x:R, x = sinh(0.6); import math; let x:R, x = cosh(1.7); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::cosh(1.7)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::cosh(1.7)}); } { // tanh @@ -164,7 +164,7 @@ let x:R, x = cosh(1.7); import math; let x:R, x = tanh(0.6); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::tanh(0.6)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::tanh(0.6)}); } { // asinh @@ -173,7 +173,7 @@ let x:R, x = tanh(0.6); import math; let x:R, x = asinh(0.6); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::asinh(0.6)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::asinh(0.6)}); } { // acosh @@ -182,7 +182,7 @@ let x:R, x = asinh(0.6); import math; let x:R, x = acosh(1.7); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::acosh(1.7)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::acosh(1.7)}); } { // tanh @@ -191,7 +191,7 @@ let x:R, x = acosh(1.7); import math; let x:R, x = atanh(0.6); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::atanh(0.6)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::atanh(0.6)}); } { // exp @@ -200,7 +200,7 @@ let x:R, x = atanh(0.6); import math; let x:R, x = exp(1.7); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::exp(1.7)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::exp(1.7)}); } { // log @@ -209,7 +209,7 @@ let x:R, x = exp(1.7); import math; let x:R, x = log(1.6); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::log(1.6)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::log(1.6)}); } { // pow @@ -218,7 +218,7 @@ let x:R, x = log(1.6); import math; let x:R, x = pow(1.6, 2.3); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "x", double{std::pow(1.6, 2.3)}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "x", double{std::pow(1.6, 2.3)}); } { // ceil @@ -227,7 +227,7 @@ let x:R, x = pow(1.6, 2.3); import math; let z:Z, z = ceil(-1.2); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "z", int64_t{-1}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "z", int64_t{-1}); } { // floor @@ -236,7 +236,7 @@ let z:Z, z = ceil(-1.2); import math; let z:Z, z = floor(-1.2); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "z", int64_t{-2}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "z", int64_t{-2}); } { // trunc @@ -245,7 +245,7 @@ let z:Z, z = floor(-1.2); import math; let z:Z, z = trunc(-0.2) + trunc(0.7); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "z", int64_t{0}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "z", int64_t{0}); } { // round @@ -254,15 +254,15 @@ let z:Z, z = trunc(-0.2) + trunc(0.7); import math; let z:Z, z = round(-1.2); )"; - CHECK_CFUNCTION_EVALUATION_RESULT(data, "z", int64_t{-1}); + CHECK_BUILTIN_FUNCTION_EVALUATION_RESULT(data, "z", int64_t{-1}); } MathModule math_module; bool missing_test = false; - for (auto [cmath_name, c_function] : math_module.getNameCFunctionMap()) { - if (tested_function_set.find(cmath_name) == tested_function_set.end()) { - UNSCOPED_INFO("function '" << cmath_name << "' is NOT tested"); + for (auto [function_name, builtin_function] : math_module.getNameBuiltinFunctionMap()) { + if (tested_function_set.find(function_name) == tested_function_set.end()) { + UNSCOPED_INFO("function '" << function_name << "' is NOT tested"); missing_test = true; } } diff --git a/tests/test_MathModule.cpp b/tests/test_MathModule.cpp index 69eb53643a6c5c568d3cc4b68c3c189a7e61f307..c168526481afb15471814afff58a05a5171df585 100644 --- a/tests/test_MathModule.cpp +++ b/tests/test_MathModule.cpp @@ -1,6 +1,6 @@ #include <catch2/catch.hpp> -#include <CFunctionEmbedder.hpp> +#include <BuiltinFunctionEmbedder.hpp> #include <EmbedderTable.hpp> #include <MathModule.hpp> @@ -10,9 +10,9 @@ TEST_CASE("MathModule", "[language]") rang::setControlMode(rang::control::Off); MathModule math_module; - const auto& name_cfunction = math_module.getNameCFunctionMap(); + const auto& name_builtin_function = math_module.getNameBuiltinFunctionMap(); - REQUIRE(name_cfunction.size() == 22); + REQUIRE(name_builtin_function.size() == 22); SECTION("double -> double") { @@ -22,11 +22,11 @@ TEST_CASE("MathModule", "[language]") SECTION("sqrt") { - auto i_function = name_cfunction.find("sqrt"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("sqrt"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::sqrt(arg); @@ -35,10 +35,10 @@ TEST_CASE("MathModule", "[language]") SECTION("abs") { - auto i_function = name_cfunction.find("abs"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("abs"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; { DataVariant result_variant = function_embedder.apply({arg_variant}); @@ -62,11 +62,11 @@ TEST_CASE("MathModule", "[language]") SECTION("sin") { - auto i_function = name_cfunction.find("sin"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("sin"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::sin(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -74,11 +74,11 @@ TEST_CASE("MathModule", "[language]") SECTION("cos") { - auto i_function = name_cfunction.find("cos"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("cos"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::cos(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -86,11 +86,11 @@ TEST_CASE("MathModule", "[language]") SECTION("tan") { - auto i_function = name_cfunction.find("tan"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("tan"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::tan(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -98,11 +98,11 @@ TEST_CASE("MathModule", "[language]") SECTION("asin") { - auto i_function = name_cfunction.find("asin"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("asin"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::asin(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -110,11 +110,11 @@ TEST_CASE("MathModule", "[language]") SECTION("acos") { - auto i_function = name_cfunction.find("acos"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("acos"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::acos(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -122,11 +122,11 @@ TEST_CASE("MathModule", "[language]") SECTION("atan") { - auto i_function = name_cfunction.find("atan"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("atan"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::atan(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -137,11 +137,11 @@ TEST_CASE("MathModule", "[language]") arg = 1.3; arg_variant = arg; - auto i_function = name_cfunction.find("sinh"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("sinh"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::sinh(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -149,11 +149,11 @@ TEST_CASE("MathModule", "[language]") SECTION("cosh") { - auto i_function = name_cfunction.find("cosh"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("cosh"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::cosh(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -161,11 +161,11 @@ TEST_CASE("MathModule", "[language]") SECTION("tanh") { - auto i_function = name_cfunction.find("tanh"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("tanh"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::tanh(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -173,11 +173,11 @@ TEST_CASE("MathModule", "[language]") SECTION("asinh") { - auto i_function = name_cfunction.find("asinh"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("asinh"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::asinh(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -188,11 +188,11 @@ TEST_CASE("MathModule", "[language]") arg = 10; arg_variant = arg; - auto i_function = name_cfunction.find("acosh"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("acosh"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::acosh(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -200,11 +200,11 @@ TEST_CASE("MathModule", "[language]") SECTION("atanh") { - auto i_function = name_cfunction.find("atanh"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("atanh"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::atanh(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -212,11 +212,11 @@ TEST_CASE("MathModule", "[language]") SECTION("exp") { - auto i_function = name_cfunction.find("exp"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("exp"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::exp(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -224,11 +224,11 @@ TEST_CASE("MathModule", "[language]") SECTION("log") { - auto i_function = name_cfunction.find("log"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("log"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); auto result = std::log(arg); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); @@ -243,11 +243,11 @@ TEST_CASE("MathModule", "[language]") SECTION("ceil") { - auto i_function = name_cfunction.find("ceil"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("ceil"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); int64_t result = std::ceil(arg); REQUIRE(std::get<decltype(result)>(result_variant) == result); @@ -255,11 +255,11 @@ TEST_CASE("MathModule", "[language]") SECTION("floor") { - auto i_function = name_cfunction.find("floor"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("floor"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); int64_t result = std::floor(arg); REQUIRE(std::get<decltype(result)>(result_variant) == result); @@ -267,11 +267,11 @@ TEST_CASE("MathModule", "[language]") SECTION("trunc") { - auto i_function = name_cfunction.find("trunc"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("trunc"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); int64_t result = std::trunc(arg); REQUIRE(std::get<decltype(result)>(result_variant) == result); @@ -279,11 +279,11 @@ TEST_CASE("MathModule", "[language]") SECTION("round") { - auto i_function = name_cfunction.find("round"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("round"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg_variant}); int64_t result = std::lround(arg); REQUIRE(std::get<decltype(result)>(result_variant) == result); @@ -300,11 +300,11 @@ TEST_CASE("MathModule", "[language]") SECTION("atan2") { - auto i_function = name_cfunction.find("atan2"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("atan2"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg0_variant, arg1_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg0_variant, arg1_variant}); auto result = std::atan2(arg0, arg1); REQUIRE(std::get<decltype(result)>(result_variant) == result); @@ -312,11 +312,11 @@ TEST_CASE("MathModule", "[language]") SECTION("pow") { - auto i_function = name_cfunction.find("pow"); - REQUIRE(i_function != name_cfunction.end()); + auto i_function = name_builtin_function.find("pow"); + REQUIRE(i_function != name_builtin_function.end()); - ICFunctionEmbedder& function_embedder = *i_function->second; - DataVariant result_variant = function_embedder.apply({arg0_variant, arg1_variant}); + IBuiltinFunctionEmbedder& function_embedder = *i_function->second; + DataVariant result_variant = function_embedder.apply({arg0_variant, arg1_variant}); auto result = std::pow(arg0, arg1); REQUIRE(std::get<decltype(result)>(result_variant) == Approx(result)); diff --git a/tests/test_SymbolTable.cpp b/tests/test_SymbolTable.cpp index 14b80ee4a0d12f9430028b49eff66db8eb0e983f..1eb87ebf816fb054c9a0b7f329477067a33929e2 100644 --- a/tests/test_SymbolTable.cpp +++ b/tests/test_SymbolTable.cpp @@ -3,7 +3,7 @@ #include <SymbolTable.hpp> #include <sstream> -#include <CFunctionEmbedder.hpp> +#include <BuiltinFunctionEmbedder.hpp> #include <pegtl/internal/iterator.hpp> @@ -201,20 +201,20 @@ TEST_CASE("SymbolTable", "[language]") REQUIRE(const_function_table.size() == 1); } - SECTION("CFunctionEmbedderTable") + SECTION("BuiltinFunctionEmbedderTable") { std::shared_ptr root_st = std::make_shared<SymbolTable>(); - auto& c_function_table = root_st->cFunctionEmbedderTable(); - REQUIRE(c_function_table.size() == 0); + auto& builtin_function_table = root_st->builtinFunctionEmbedderTable(); + REQUIRE(builtin_function_table.size() == 0); - const auto& const_c_function_table = static_cast<const SymbolTable&>(*root_st).cFunctionEmbedderTable(); - REQUIRE(const_c_function_table.size() == 0); + const auto& const_builtin_function_table = static_cast<const SymbolTable&>(*root_st).builtinFunctionEmbedderTable(); + REQUIRE(const_builtin_function_table.size() == 0); - c_function_table.add(std::make_shared<CFunctionEmbedder<double, double>>( + builtin_function_table.add(std::make_shared<BuiltinFunctionEmbedder<double, double>>( std::function<double(double)>{[](double) -> double { return 0; }})); - REQUIRE(c_function_table.size() == 1); - REQUIRE(const_c_function_table.size() == 1); + REQUIRE(builtin_function_table.size() == 1); + REQUIRE(const_builtin_function_table.size() == 1); } }