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