<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 {

enum TokenType {
    NUMBER,
    ADD, 
    SUBTRACT,
    MULTIPLY,
    DIVIDE,
    OPENING_PARENTHESIS,
    CLOSING_PARENTHESIS
};

static unordered_map<char, TokenType> char_to_type_map;

class Token {
public:
    TokenType type;
    string value;
    Token(TokenType type, const string &value): type(type), value(value) {}
};


class Lexer {
    int pos;
    string input;

public:
    Lexer(): pos(0) {}

    queue<Token> tokenize(const string &input) {
        //* init
        pos = 0;
        this->input = input;

        queue<Token> q;
        while (pos < input.size()) {
            char curr = input[pos];
            if (isspace(curr)) {
                ++pos;
            } else if (isdigit(curr)) {
                q.push(Token(TokenType::NUMBER, readNumber()));
            } else if (char_to_type_map.count(curr)) {
                q.push(Token(char_to_type_map[curr], string(1, curr)));
                ++pos;
            } else {
                throw runtime_error("invalid character in input!");
            }
        }
        return q;
    }

private:
    string readNumber() {
        int start = pos;
        while (pos < input.size() && isdigit(input[pos])) ++pos;
        return input.substr(start, pos - start);
    }
};





class Node {
public:
    virtual int evaluate() = 0;
    virtual string to_string() = 0;
};


class UnaryNegateNode: public Node {
    Node *node;
public:
    UnaryNegateNode(Node *node = nullptr): node(node) {}
    
    virtual int evaluate() {
        return - node->evaluate();
    }

    virtual string to_string() {
        string res = "(";
        res += "-";
        res += node->to_string();
        res += ")";
        return res;
    }
};

class AddNode: public Node {
    Node *left, *right;
public:
    AddNode(Node *left = nullptr, Node *right = nullptr): left(left), right(right) {}

    virtual int evaluate() {
        return left->evaluate() + right->evaluate();
    }
    
    virtual string to_string() {
        string res = "(";
        res += left->to_string();
        res += "+";
        res += right->to_string();
        res += ")";
        return res;
    }
};

class SubtractNode: public Node {
    Node *left, *right;
public:
    SubtractNode(Node *left = nullptr, Node *right = nullptr): left(left), right(right) {}

    virtual int evaluate() {
        return left->evaluate() - right->evaluate();
    }
    
    virtual string to_string() {
        string res = "(";
        res += left->to_string();
        res += "-";
        res += right->to_string();
        res += ")";
        return res;
    }
};

class MultiplyNode: public Node {
    Node *left, *right;
public:
    MultiplyNode(Node *left = nullptr, Node *right = nullptr): left(left), right(right) {}
    
    virtual int evaluate() {
        return left->evaluate() * right->evaluate();
    }

    
    virtual string to_string() {
        string res = "(";
        res += left->to_string();
        res += "*";
        res += right->to_string();
        res += ")";
        return res;
    }
};

class DivideNode: public Node {
    Node *left, *right;
public:
    DivideNode(Node *left = nullptr, Node *right = nullptr): left(left), right(right) {}

    virtual int evaluate() {
        return left->evaluate() / right->evaluate();
    }

    
    virtual string to_string() {
        string res = "(";
        res += left->to_string();
        res += "/";
        res += right->to_string();
        res += ")";
        return res;
    }
};

class NumberNode: public Node {
    string val;
public:
    NumberNode(const string &val): val(val) {}
    virtual int evaluate() {
        return stoi(val);
    }

    
    virtual string to_string() {
        // cout << "(";
        return val;
        // cout << ")";
    }
};




class Parser {
    string input;
    queue<Token> q;    
public: 
    Parser(const string &input): input(input) {
        q = Lexer().tokenize(input); 
    }

    Node *parse() {
        return parseE();
    }

private:
    Token peek() {
        return q.front();
    }
    
    bool match(TokenType type) {
        return (q.size() && q.front().type == type);
    }

    Node *parseE() {
        Node *node = parseT();
        // add and subtract operators have equal precedence
        // if both are present left one should be executed first
        while (match(TokenType::ADD) || match(TokenType::SUBTRACT)) {
            TokenType oprtr = peek().type;
            q.pop(); //* consume
            Node *right_node = parseT();
            if (oprtr == TokenType::ADD) node = new AddNode(node, right_node);
            else node = new SubtractNode(node, right_node);
        }
        return node;
    }

    Node *parseT() {
        Node *node = parseF();
        // multiply and divide operators have equal precedence
        // if both are present left one should be executed first [that's why this while loop]
        while (match(TokenType::MULTIPLY) || match(TokenType::DIVIDE)) {
            TokenType oprtr = peek().type;
            q.pop(); //* consume
            Node *right_node = parseF();
            if (oprtr == TokenType::MULTIPLY) node = new MultiplyNode(node, right_node);
            else node = new DivideNode(node, right_node);
        }
        return node;
    }

    Node *parseF() {
        if (match(TokenType::SUBTRACT)) {
            q.pop();
            return new UnaryNegateNode(parseF());
        } else if (match(TokenType::OPENING_PARENTHESIS)) {
            q.pop();
            Node *node = parseE();
            if (!match(TokenType::CLOSING_PARENTHESIS)) 
                throw runtime_error("Missing closing parenthesis!");
            
            q.pop();
            return node;
        } else if (match(TokenType::NUMBER)) {
            Node* node = new NumberNode(peek().value);
            q.pop();
            return node;
        } else {
            throw runtime_error("Invalid token!");
        }
    }

};
public:
    int calculate(string s) {
        Parser parser(s);
        return parser.parse()->evaluate();
    }
};

unordered_map<char, Solution::TokenType> Solution::char_to_type_map = {
    {'+', Solution::ADD},
    {'-', Solution::SUBTRACT},
    {'*', Solution::MULTIPLY},
    {'/', Solution::DIVIDE},
    {'(', Solution::OPENING_PARENTHESIS},
    {')', Solution::CLOSING_PARENTHESIS},
};
42181