From da9eb2ea2ed6a027acbb595b53f52a48f894a5ba Mon Sep 17 00:00:00 2001 From: Huamonarch Date: Fri, 15 May 2026 12:25:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ExpressionEngine=20=E6=A0=B8=E5=BF=83?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=EF=BC=88=E6=B3=A8=E5=86=8C=20+=20=E6=B1=82?= =?UTF-8?q?=E5=80=BC=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eqpalg/utility/expression_engine.cpp | 72 ++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 eqpalg/utility/expression_engine.cpp diff --git a/eqpalg/utility/expression_engine.cpp b/eqpalg/utility/expression_engine.cpp new file mode 100644 index 0000000..0260c29 --- /dev/null +++ b/eqpalg/utility/expression_engine.cpp @@ -0,0 +1,72 @@ +// eqpalg/utility/expression_engine.cpp +#include +#include +#include +#include +#include + +ExpressionEngine::ExpressionEngine(std::map& mm_vars, + std::vector& m_tags) + : mm_vars_(mm_vars), m_tags_(m_tags) {} + +// 惰性初始化 VarsCache(首次变量刷新时调用) +static void ensureVarCache(VarsCache& vc, size_t tag_count) { + if (vc.tag_num == 0 && tag_count > 0) { + vc.init(tag_count, 6); + } +} + +int ExpressionEngine::registerExpression(const std::string& name, + const std::string& raw_exp_str) { + if (exps_.count(name)) return 0; // 已注册,幂等 + + auto& entry = exps_[name]; + entry.raw_str = raw_exp_str; + + // 通过 FunVars 替换状态函数(KeepC/KeepT/RiseEdge/Detect)为 funN 变量 + auto [ok, processed] = fun_vars_.add_exp_str(raw_exp_str, &mm_vars_); + if (!ok) return -1; + entry.processed_str = processed; + + // 编译表达式 + try { + entry.ptr = std::make_unique(processed, &mm_vars_); + } catch (const std::exception& e) { + return -1; + } + + // 从表达式中提取 hold(n,T) 模式 + initHoldExpStr(processed); + + return 0; +} + +int ExpressionEngine::registerRawExpression(const std::string& name, + const std::string& raw_exp_str) { + if (exps_.count(name)) return 0; + + auto& entry = exps_[name]; + entry.raw_str = raw_exp_str; + entry.processed_str = raw_exp_str; + + try { + entry.ptr = std::make_unique(raw_exp_str, &mm_vars_); + } catch (const std::exception& e) { + return -1; + } + return 0; +} + +double ExpressionEngine::evaluate(const std::string& name) { + auto it = exps_.find(name); + if (it == exps_.end()) return 0.0; + return it->second.ptr->evaluate(); +} + +bool ExpressionEngine::evaluateBool(const std::string& name) { + return static_cast(evaluate(name)); +} + +int ExpressionEngine::initHoldExpStr(const std::string& exp_str) { + return 0; // stub — Task 5 will implement +}