diff --git a/src/share/exprtk_utility.hpp b/src/share/exprtk_utility.hpp new file mode 100644 index 000000000..fc9cbd847 --- /dev/null +++ b/src/share/exprtk_utility.hpp @@ -0,0 +1,50 @@ +#pragma once + +#define exprtk_disable_string_capabilities +#define exprtk_disable_rtl_io +#define exprtk_disable_rtl_io_file +#define exprtk_disable_rtl_vecops + +#include + +namespace krbn { +namespace exprtk_utility { +inline double eval(const std::string& expression_string, + std::vector> constants) { + exprtk::symbol_table symbol_table; + symbol_table.add_constants(); // pi, epsilon and inf + for (auto&& [name, value] : constants) { + symbol_table.add_constant(name, value); + } + + exprtk::expression expression; + expression.register_symbol_table(symbol_table); + + exprtk::parser parser; + parser.compile(expression_string, expression); + + return expression.value(); +} + +inline exprtk::expression compile(const std::string& expression_string, + std::vector> constants, + std::vector> variables) { + exprtk::symbol_table symbol_table; + symbol_table.add_constants(); // pi, epsilon and inf + for (auto&& [name, value] : constants) { + symbol_table.add_constant(name, value); + } + for (auto&& [name, value] : variables) { + symbol_table.add_variable(name, value); + } + + exprtk::expression expression; + expression.register_symbol_table(symbol_table); + + exprtk::parser parser; + parser.compile(expression_string, expression); + + return expression; +} +} // namespace exprtk_utility +} // namespace krbn diff --git a/tests/src/exprtk_utility/CMakeLists.txt b/tests/src/exprtk_utility/CMakeLists.txt new file mode 100644 index 000000000..1f0c3be6d --- /dev/null +++ b/tests/src/exprtk_utility/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.9) + +include(../../tests.cmake) + +project(karabiner_test) + +include_directories(${CMAKE_CURRENT_LIST_DIR}/../../../src/vendor/duktape-2.7.0/src) +include_directories(${CMAKE_CURRENT_LIST_DIR}/../../../src/vendor/duktape-2.7.0/extras/console) +include_directories(${CMAKE_CURRENT_LIST_DIR}/../../../src/vendor/duktape-2.7.0/extras/module-node) + +# Suppress duktape warnings +add_compile_options(-Wno-deprecated-declarations) +add_compile_options(-Wno-unused-but-set-variable) + +add_executable( + karabiner_test + src/test.cpp + ../../../src/vendor/duktape-2.7.0/src/duktape.c + ../../../src/vendor/duktape-2.7.0/extras/console/duk_console.c + ../../../src/vendor/duktape-2.7.0/extras/module-node/duk_module_node.c +) diff --git a/tests/src/exprtk_utility/Makefile b/tests/src/exprtk_utility/Makefile new file mode 100644 index 000000000..2940b8362 --- /dev/null +++ b/tests/src/exprtk_utility/Makefile @@ -0,0 +1,6 @@ +all: build_make + ./build/karabiner_test + +clean: clean_builds + +include ../Makefile.rules diff --git a/tests/src/exprtk_utility/src/test.cpp b/tests/src/exprtk_utility/src/test.cpp new file mode 100644 index 000000000..b14bfde7d --- /dev/null +++ b/tests/src/exprtk_utility/src/test.cpp @@ -0,0 +1,36 @@ +#include "exprtk_utility.hpp" +#include + +int main(void) { + using namespace boost::ut; + using namespace boost::ut::literals; + using namespace std::literals; + + "exprtk_utility::eval"_test = [] { + auto actual = krbn::exprtk_utility::eval("cos(radian) * magnitude", + { + {"radian", 0.78539816339}, + {"magnitude", 3.5}, + }); + expect(2.47487_d == actual); + }; + + "exprtk_utility::compile"_test = [] { + double radian = 0.0; + double magnitude = 1.0; + + auto expression = krbn::exprtk_utility::compile("cos(radian) * magnitude", + {}, + { + {"radian", radian}, + {"magnitude", magnitude}, + }); + expect(1.0_d == expression.value()); + + radian = 0.78539816339; + magnitude = 3.5; + expect(2.47487_d == expression.value()); + }; + + return 0; +}