<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:
    def calculate(self, s: str) -> int:
        lexer = Lexer(s)
        parser = Parser(lexer)
        parse_tree = parser.parse(None)
        print(parse_tree)
        interpreter = Interpreter(parse_tree)
        return interpreter.eval()

class Lexer:
    def __init__(self, expr: str):
        self.expr = expr
        self.i = 0
    def parse_num(self) -> str:
        num = ""
        while self.i < len(self.expr) and self.expr[self.i].isdigit() :
            num += self.expr[self.i]
            self.i += 1
        return num
    def next_token(self) -> str:
        if self.i == len(self.expr):
            return ""

        ch = self.expr[self.i]
        if ch == " ":
            self.i += 1 ; return self.next_token()
        elif ch.isdigit():
            num = self.parse_num()
            return num
        elif ch in ['+', '-', '(', ')']:
            self.i += 1 ; return ch

class num_node:
    def __init__(self, num):
        self.num = num
    def __repr__(self):
        return f'{self.num}'
class add_node:
    def __init__(self, left, right):
        self.left = left
        self.right = right
    def __repr__(self):
        return f'{self.left}+{self.right}'
class subtract_node:
    def __init__(self, left, right):
        self.left = left
        self.right = right
    def __repr__(self):
        return f'{self.left}-{self.right}'
class negate_node:
    def __init__(self, parse_tree):
        self.parse_tree = parse_tree
    def __repr__(self):
        return f' -{self.parse_tree}'
class group_node:
    def __init__(self, parse_tree):
        self.parse_tree = parse_tree
    def __repr__(self):
        return f'({self.parse_tree})'
parse_node = num_node | add_node | subtract_node | negate_node | group_node
class Parser:
    def __init__(self, lexer):
        self.lexer = lexer
    def next_node(self, last_node) -> parse_node:
        token = self.lexer.next_token()
        if token == "":
            return None
        elif token == ")":
            return None
        elif token.isnumeric():
            return num_node(int(token))
        elif token == "+":
            return add_node(last_node, self.next_node(None))
        elif token == "-":
            if last_node is None:
                return negate_node(self.next_node(None))
            else:
                return subtract_node(last_node, self.next_node(None))
        elif token == "(":
            return group_node(self.parse(None))

    def parse(self, parse_tree: parse_node) -> parse_node:
        node = self.next_node(parse_tree)
        if node is None:
            return parse_tree
        else:
            return self.parse(node)
        
class Interpreter:
    def __init__(self, parse_tree):
        self.parse_tree = parse_tree
    def eval(self) -> int:
        return self.eval_pt(self.parse_tree)
    def eval_pt(self, parse_tree: parse_node) -> int:
        if isinstance(parse_tree, num_node):
            return parse_tree.num
        elif isinstance(parse_tree, add_node):
            return self.eval_pt(parse_tree.left) + self.eval_pt(parse_tree.right)
        elif isinstance(parse_tree, subtract_node):
            return self.eval_pt(parse_tree.left) - self.eval_pt(parse_tree.right)
        elif isinstance(parse_tree, negate_node):
            return -1 * self.eval_pt(parse_tree.parse_tree)
        elif isinstance(parse_tree, group_node):
            return self.eval_pt(parse_tree.parse_tree)
42864