Skip to content

Commit

Permalink
Added lambda with capture group support to USRs
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
Blake-Madden committed Nov 1, 2023
1 parent dbf65df commit f003821
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
14 changes: 14 additions & 0 deletions tests/tetests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,20 @@ TEST_CASE("Unknown symbol resolve funct pointer purge resolved 2", "[usr]")
CHECK(parser.evaluate("id.temperature") == 53);
}

TEST_CASE("Unknown symbol resolve lambda with capture", "[usr]")
{
std::string str("id.temperature < 51");
double temperature = 49.0;
te_parser parser;
parser.set_unknown_symbol_resolver([&](std::string_view symbol)
{
return temperature += 1.0;
}, false);
CHECK(parser.compile(str)); // 50
CHECK_FALSE(parser.evaluate(str)); // 51
CHECK_FALSE(parser.evaluate(str)); // 52
CHECK(parser.evaluate("id.temperature") == 53);
}

TEST_CASE("Unknown symbol resolve 1 param purged", "[usr]")
{
Expand Down
7 changes: 4 additions & 3 deletions tinyexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include <cctype>
#include <set>
#include <utility>
#include <functional>

class te_expr;

Expand All @@ -90,9 +91,9 @@ using te_confun6 = double (*)(const te_expr*, double, double, double, double, do
using te_confun7 = double (*)(const te_expr*, double, double, double, double, double, double, double);

// functions for unknown symbol resolution
using te_usr_noop = void (*)();
using te_usr_fun0 = double (*)(std::string_view);
using te_usr_fun1 = double (*)(std::string_view, std::string&);
using te_usr_noop = std::function<void ()>;
using te_usr_fun0 = std::function<double (std::string_view)>;
using te_usr_fun1 = std::function<double (std::string_view, std::string&)>;

using te_usr_variant_type = std::variant<te_usr_noop, te_usr_fun0, te_usr_fun1>;

Expand Down

0 comments on commit f003821

Please sign in to comment.