#ifndef MCALC_NODE_H #define MCALC_NODE_H #include #include #include #include #include struct State; struct Node { enum class type { NUM, VAR, NEG, PLUS, MINUS, MUL, DIV, INVALID } ty = type::INVALID; double num = 0; std::string var{}; std::unique_ptr a{}, b{}; void used_vars(std::unordered_set*) const; std::optional eval(State*) const; }; using parser_var_ty = std::variant; template Node *new_node(const parser_var_ty& v) { auto *node = new Node { .ty = ty }; if constexpr (ty == Node::type::NUM) node->num = std::get(v); else if constexpr (ty == Node::type::VAR) node->var = std::get(v); else node->a.reset(std::get(v)); return node; } template Node *new_node(const parser_var_ty& a, const parser_var_ty& b) { auto *node = new Node { .ty = ty }; node->a.reset(std::get(a)); node->b.reset(std::get(b)); return node; } #endif //MCALC_NODE_H