Skip to content

Commit 918e475

Browse files
committed
Parser copying will now include the expression also
Closes #18
1 parent 99b402b commit 918e475

File tree

2 files changed

+73
-10
lines changed

2 files changed

+73
-10
lines changed

tests/tetests.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,11 @@ TEST_CASE("Main tests", "[main]")
683683
CHECK(tep.evaluate("average(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)") == 12);
684684
CHECK(tep.evaluate("average(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)") == 12.5);
685685
}
686+
}
687+
688+
TEST_CASE("Logic & Operators", "[main]")
689+
{
690+
te_parser tep;
686691

687692
SECTION("logical")
688693
{
@@ -1058,6 +1063,29 @@ TEST_CASE("Dynamic", "[dynamic]")
10581063
CHECK(tep.evaluate("sum7(2,3,4,5,6,7,8)") == 35);
10591064
}
10601065

1066+
TEST_CASE("Copy", "[copy]")
1067+
{
1068+
te_parser tep;
1069+
te_type x{ 8 }, y{ 2 };
1070+
tep.set_variables_and_functions({ {"x", &x}, {"y", &y} });
1071+
CHECK(tep.evaluate("trunc(9.57878423)") == 9);
1072+
CHECK(tep.evaluate() == 9);
1073+
{
1074+
// test operator== and copy CTOR
1075+
te_parser tep2 = tep;
1076+
CHECK(tep2.evaluate() == 9);
1077+
CHECK(te_parser{ tep }.evaluate() == 9);
1078+
}
1079+
1080+
CHECK(tep.evaluate("(x + y) * y") == 20);
1081+
// variable connection logic should be copied over too
1082+
{
1083+
te_parser tep2 = tep;
1084+
CHECK(tep2.evaluate() == 20);
1085+
CHECK(te_parser{ tep }.evaluate() == 20);
1086+
}
1087+
}
1088+
10611089
TEST_CASE("Inf", "[inf]")
10621090
{
10631091
te_parser tep;

tinyexpr.h

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ class te_string_less
253253
{
254254
public:
255255
[[nodiscard]]
256-
bool
257-
operator()(const std::string& lhv, const std::string& rhv) const
256+
bool operator()(const std::string& lhv, const std::string& rhv) const
258257
{
259258
const auto minStrLen = std::min(lhv.length(), rhv.length());
260259
for (size_t i = 0; i < minStrLen; ++i)
@@ -293,7 +292,8 @@ class te_expr
293292
explicit te_expr(const te_variable_flags type) noexcept : m_type(type) {}
294293

295294
/// @private
296-
te_expr() noexcept {};
295+
te_expr() noexcept {}
296+
297297
/// @private
298298
te_expr(const te_expr&) = delete;
299299
/// @private
@@ -319,8 +319,7 @@ class te_variable
319319

320320
/// @private
321321
[[nodiscard]]
322-
bool
323-
operator<(const te_variable& that) const
322+
bool operator<(const te_variable& that) const
324323
{
325324
return te_string_less{}(m_name, that.m_name);
326325
}
@@ -350,8 +349,23 @@ class te_parser
350349
: m_customFuncsAndVars(that.m_customFuncsAndVars),
351350
m_unknownSymbolResolve(that.m_unknownSymbolResolve),
352351
m_keepResolvedVariables(that.m_keepResolvedVariables),
353-
m_decimalSeparator(that.m_decimalSeparator), m_listSeparator(that.m_listSeparator)
352+
m_decimalSeparator(that.m_decimalSeparator), m_listSeparator(that.m_listSeparator),
353+
m_expression(that.m_expression)
354354
{
355+
try
356+
{
357+
if (!m_expression.empty())
358+
{
359+
[[maybe_unused]]
360+
const auto retVal = evaluate(m_expression);
361+
}
362+
}
363+
catch (const std::exception& expt)
364+
{
365+
m_parseSuccess = false;
366+
m_result = te_nan;
367+
m_lastErrorMessage = expt.what();
368+
}
355369
}
356370

357371
/// @private
@@ -362,9 +376,26 @@ class te_parser
362376
m_keepResolvedVariables = that.m_keepResolvedVariables;
363377
m_decimalSeparator = that.m_decimalSeparator;
364378
m_listSeparator = that.m_listSeparator;
379+
m_expression = that.m_expression;
365380

381+
// re-run the expression that was copied over
366382
reset_state();
367383

384+
try
385+
{
386+
if (!m_expression.empty())
387+
{
388+
[[maybe_unused]]
389+
const auto retVal = evaluate(m_expression);
390+
}
391+
}
392+
catch (const std::exception& expt)
393+
{
394+
m_parseSuccess = false;
395+
m_result = te_nan;
396+
m_lastErrorMessage = expt.what();
397+
}
398+
368399
return *this;
369400
}
370401

@@ -914,11 +945,13 @@ class te_parser
914945
}
915946

916947
#define TE_DEF_FUNCTION(n) \
917-
[[nodiscard]] constexpr static bool is_function##n(const te_variant_type& var) noexcept \
948+
[[nodiscard]] \
949+
constexpr static bool is_function##n(const te_variant_type& var) noexcept \
918950
{ \
919951
return std::holds_alternative<te_fun##n>(var); \
920952
} \
921-
[[nodiscard]] constexpr static te_fun##n get_function##n(const te_variant_type& var) \
953+
[[nodiscard]] \
954+
constexpr static te_fun##n get_function##n(const te_variant_type& var) \
922955
{ \
923956
assert(std::holds_alternative<te_fun##n>(var)); \
924957
return std::get<te_fun##n>(var); \
@@ -941,11 +974,13 @@ class te_parser
941974
}
942975

943976
#define TE_DEF_CLOSURE(n) \
944-
[[nodiscard]] constexpr static bool is_closure##n(const te_variant_type& var) noexcept \
977+
[[nodiscard]] \
978+
constexpr static bool is_closure##n(const te_variant_type& var) noexcept \
945979
{ \
946980
return std::holds_alternative<te_confun##n>(var); \
947981
} \
948-
[[nodiscard]] constexpr static te_confun##n get_closure##n(const te_variant_type& var) \
982+
[[nodiscard]] \
983+
constexpr static te_confun##n get_closure##n(const te_variant_type& var) \
949984
{ \
950985
assert(std::holds_alternative<te_confun##n>(var)); \
951986
return std::get<te_confun##n>(var); \

0 commit comments

Comments
 (0)