Skip to content

Commit

Permalink
Add exprtk_utility.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Oct 9, 2023
1 parent 8ba1a5c commit 6484210
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/share/exprtk_utility.hpp
Original file line number Diff line number Diff line change
@@ -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 <exprtk/exprtk.hpp>

namespace krbn {
namespace exprtk_utility {
inline double eval(const std::string& expression_string,
std::vector<std::pair<std::string, double>> constants) {
exprtk::symbol_table<double> symbol_table;
symbol_table.add_constants(); // pi, epsilon and inf
for (auto&& [name, value] : constants) {
symbol_table.add_constant(name, value);
}

exprtk::expression<double> expression;
expression.register_symbol_table(symbol_table);

exprtk::parser<double> parser;
parser.compile(expression_string, expression);

return expression.value();
}

inline exprtk::expression<double> compile(const std::string& expression_string,
std::vector<std::pair<std::string, double>> constants,
std::vector<std::pair<std::string, double&>> variables) {
exprtk::symbol_table<double> 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<double> expression;
expression.register_symbol_table(symbol_table);

exprtk::parser<double> parser;
parser.compile(expression_string, expression);

return expression;
}
} // namespace exprtk_utility
} // namespace krbn
21 changes: 21 additions & 0 deletions tests/src/exprtk_utility/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
6 changes: 6 additions & 0 deletions tests/src/exprtk_utility/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
all: build_make
./build/karabiner_test

clean: clean_builds

include ../Makefile.rules
36 changes: 36 additions & 0 deletions tests/src/exprtk_utility/src/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "exprtk_utility.hpp"
#include <boost/ut.hpp>

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;
}

0 comments on commit 6484210

Please sign in to comment.