<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 Token:
    def __init__(self, val, left=None, right=None):
        self.left = left
        self.right = right
        self.val = val

    def evaluate(self):
        left = right = 0
        if self.left:
            left = self.left.evaluate()
        if self.right:
            right = self.right.evaluate()

        if self.val == "(":
            return right
        elif self.val == "+":
            return left + right
        elif self.val == "-":
            return left - right
        return int(self.val)

    def __str__(self):
        left = self.left if self.left else ""
        right = self.right if self.right else ""
        return f"{left}{self.val}{right}"

def handle_simple_expr(tokens):
    char = tokens.pop()
    if char == "-":
        return Token("-", right=handle_simple_expr(tokens))
    if char == "(" or char == ")":
        new_token = handle_complex_expr(tokens)
        # Assumed to be )
        tokens.pop()
    elif char.isnumeric():
        new_token = Token(char)
    else:
        return None
    return new_token

def handle_complex_expr(tokens):
    left = handle_simple_expr(tokens)
    while tokens:
        # char = tokens.pop()
        next_char = tokens[-1] if tokens else None
        if next_char and next_char == "+" or next_char == "-":
            # this is wrong
            tokens.pop()
            right = handle_simple_expr(tokens)
            left = Token(next_char, left, right)
        else:
            break

    return left

class Solution:
    def calculate(self, s: str) -> int:
        tokens = list(reversed(re.findall(("[\d]+|[+\-()]"), s)))

        left = handle_complex_expr(tokens)

        return left.evaluate()
39091