<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p>

<p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;1 + 1&quot;
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot; 2-1 + 2 &quot;
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(1+(4+5+2)-3)+(6+8)&quot;
<strong>Output:</strong> 23
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
	<li><code>s</code> consists of digits, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, and <code>&#39; &#39;</code>.</li>
	<li><code>s</code> represents a valid expression.</li>
	<li><code>&#39;+&#39;</code> is <strong>not</strong> used as a unary operation (i.e., <code>&quot;+1&quot;</code> and <code>&quot;+(2 + 3)&quot;</code> is invalid).</li>
	<li><code>&#39;-&#39;</code> could be used as a unary operation (i.e., <code>&quot;-1&quot;</code> and <code>&quot;-(2 + 3)&quot;</code> is valid).</li>
	<li>There will be no two consecutive operators in the input.</li>
	<li>Every number and running calculation will fit in a signed 32-bit integer.</li>
</ul>

class Solution {
public:
    using Num = int64_t;
    using Nil = std::monostate;

    enum class UnOp {
        Negate
    };

    enum class BiOp {
        Add,
        Sub
    };

    using Ops = std::variant<Nil, UnOp, BiOp>;

    struct Expr;

    using ExprVariant = std::variant<
        Nil,
        Num,
        std::pair<UnOp, std::unique_ptr<Expr>>,
        std::tuple<BiOp, std::unique_ptr<Expr>, std::unique_ptr<Expr>>
    >;

    struct Expr {
        ExprVariant val;

        Expr() : val(Nil{}) {}
        Expr(Num n) : val(n) {}
        Expr(UnOp unop, std::unique_ptr<Expr> e) : val(std::make_pair(unop, std::move(e))) {}
        Expr(BiOp binop, std::unique_ptr<Expr> e1, std::unique_ptr<Expr> e2) : val(std::make_tuple(binop, std::move(e1), std::move(e2))) {}

        // Move constructor
        Expr(Expr&& other) noexcept : val(std::move(other.val)) {}

        // Move assignment operator
        Expr& operator=(Expr&& other) noexcept {
            if (this != &other) {
                val = std::move(other.val);
            }
            return *this;
        }

        // Delete copy constructor and copy assignment operator
        Expr(const Expr&) = delete;
        Expr& operator=(const Expr&) = delete;
    };

    struct Visitor {
        int64_t operator()(Nil) const {
            throw std::runtime_error("Can't perform ops with null");
        }

        int64_t operator()(Num n) const {
            return n;
        }

        int64_t operator()(const std::pair<UnOp, std::unique_ptr<Expr>>& unop) const {
            auto op = unop.first;
            int64_t v = std::visit(*this, unop.second->val);

            switch (op) {
                case UnOp::Negate:
                    return -v;
                default:
                    throw std::runtime_error("Unknown op");
            }
        }

        int64_t operator()(const std::tuple<BiOp, std::unique_ptr<Expr>, std::unique_ptr<Expr>>& biop) const {
            int64_t lhs = std::visit(*this, std::get<1>(biop)->val);
            int64_t rhs = std::visit(*this, std::get<2>(biop)->val);
            auto op = std::get<0>(biop);

            switch (op) {
                case BiOp::Add:
                    return lhs + rhs;
                case BiOp::Sub:
                    return lhs - rhs;
                default:
                    throw std::runtime_error("Unknown biop");
            }
        }
    };
    struct ToStringVisitor {
        std::string operator()(Nil) const {
            return "Nil";
        }

        std::string operator()(Num n) const {
            return std::to_string(n);
        }

        std::string operator()(const std::pair<UnOp, std::unique_ptr<Expr>>& unop) const {
            std::string opStr;
            switch (unop.first) {
                case UnOp::Negate:
                    opStr = "-";
                    break;
                default:
                    throw std::runtime_error("Unknown UnOp");
            }
            return opStr + "(" + std::visit(*this, unop.second->val) + ")";
        }

        std::string operator()(const std::tuple<BiOp, std::unique_ptr<Expr>, std::unique_ptr<Expr>>& biop) const {
            std::string opStr;
            switch (std::get<0>(biop)) {
                case BiOp::Add:
                    opStr = "+";
                    break;
                case BiOp::Sub:
                    opStr = "-";
                    break;
                default:
                    throw std::runtime_error("Unknown BiOp");
            }
            return "(" + std::visit(*this, std::get<1>(biop)->val) + " " + opStr + " " + std::visit(*this, std::get<2>(biop)->val) + ")";
        }
    };

    int64_t getNum(string&s, int& i, int end) {
        string num; 
        do {
            num += s[i];
        } while (++i < end && std::isdigit(s[i]));
        i--;
        return std::stoll(num);
    }

    Expr evaluate(std::string& s, int start, int end) {
        int i = start;
        std::vector<int> stack;
        Ops op = Nil{};
        Expr e;

        while (++i < end) {
            if (s[i] == '(') {
                stack.push_back(i);
                int start = stack.back();
                while (!stack.empty()) {
                    i++;
                    if (s[i] == '(') stack.push_back(i);
                    else if (s[i] == ')') {
                        start = stack.back(); stack.pop_back();
                    }
                }
                if (std::holds_alternative<BiOp>(op)) {
                    auto biop = std::get<BiOp>(op);
                    switch (biop) {
                        case BiOp::Add:
                        case BiOp::Sub: {
                            e = Expr(biop, std::make_unique<Expr>(std::move(e)), std::make_unique<Expr>(evaluate(s, start, i)));
                            break;
                        }
                        default:
                            throw std::runtime_error("Unknown biop");
                    }
                } else if (std::holds_alternative<UnOp>(op)) {
                    auto unop = std::get<UnOp>(op);
                    switch (unop) {
                        case UnOp::Negate: {
                            e = Expr(unop, std::make_unique<Expr>(evaluate(s, start, i)));
                            break;
                        }
                        default:
                            throw std::runtime_error("Unknown unop");
                    }
                } else {
                    e = evaluate(s, start, i);
                }
            } else if (s[i] == '+') {
                op = BiOp::Add;
            } else if (s[i] == '-') {
                if (std::holds_alternative<Nil>(e.val)) {
                    op = UnOp::Negate;
                } else {
                    op = BiOp::Sub;
                }
            } else {
                if (std::holds_alternative<BiOp>(op)) {
                    auto biop = std::get<BiOp>(op);
                    switch (biop) {
                        case BiOp::Add:
                        case BiOp::Sub: {
                            e = Expr(biop, std::make_unique<Expr>(std::move(e)), std::make_unique<Expr>(getNum(s, i, end)));
                            break;
                        }
                        default:
                            throw std::runtime_error("Unknown biop");
                    }
                } else if (std::holds_alternative<UnOp>(op)) {
                    auto unop = std::get<UnOp>(op);
                    switch (unop) {
                        case UnOp::Negate: {
                            e = Expr(unop, std::make_unique<Expr>(getNum(s, i, end)));
                            break;
                        }
                        default:
                            throw std::runtime_error("Unknown unop");
                    }
                } else {
                    e = Expr(getNum(s, i, end));
                    // ToStringVisitor toStringVisitor;
                    // string expressionStr = std::visit(toStringVisitor, e.val);
                    // cout << expressionStr << endl;
                }
            }
        }

        return e;
    }

    int calculate(string s) {
        s.erase(std::remove(s.begin(), s.end(), ' '), s.end()); 
        Expr line = evaluate(s, -1, s.length()); 
        // ToStringVisitor toStringVisitor;
        // string expressionStr = std::visit(toStringVisitor, line.val);
        // cout << "Expression: " << expressionStr << endl;

        Visitor v;
        return std::visit(v, line.val);
    }
};
41243