<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 Add:
    def __init__(self,left,right):
        self.left = left
        self.right = right
    
    def eval(self):
        return self.left.eval() + self.right.eval()

class Neg:
    def __init__(self,num):
        self.num = num
        
    def eval(self):
        return - self.num.eval()

class Num:
    def __init__(self,num):
        self.num = int(num)
        
    def eval(self):
        return self.num

class Parser:
    valid_ops = set(['+','-'])
    brackets = set(['(',')'])

    def is_op_or_bracket(self,char):
            return char in self.valid_ops or char in self.brackets

    def __init__(self,s):
        s = s.replace(" ","")
        symbols = []
        start = 0
        for i,char in enumerate(s):
            if char in self.valid_ops or char in self.brackets:
                if start != i:
                    symbols.append(s[start:i])
                symbols.append(s[i])
                start = i + 1
        if start < len(s):
            symbols.append(s[start:])
        self.symbols = symbols
        print(self.symbols)

    def find_closing_bracket(self,start):
        count = 1 if self.symbols[start] == '(' else 0
        start += 1
        while start < len (self.symbols) and count > 0:
            if self.symbols[start] == '(':
                count += 1
            if self.symbols[start] == ')':
                count -= 1
            if count > 0:
                start += 1
        return start

    def parse(self, start = None, end = None) -> Add|Neg|Num:
        if start == None:
            return self.parse(0,len(self.symbols))
        stack = []
        op_stack = []
        i = start
        while i < end:
            if self.symbols[i] == '-' and len(stack) == 0:
                stack.append(Num(0))
            if self.symbols[i] in self.valid_ops:
                op_stack.append(self.symbols[i])
            elif self.symbols[i] in self.brackets:
                j = self.find_closing_bracket(i)
                stack.append(self.parse(i+1,j))
                i = j
            else:
                stack.append(Num(self.symbols[i]))
            if len(op_stack) > 0 and len(stack) > 1:
                op = op_stack.pop()
                right = stack.pop()
                left = stack.pop()
                if op == '-':
                    stack.append(Add(left,Neg(right)))
                else:
                    stack.append(Add(left,right))
            i += 1
        return stack.pop()
            


class Solution:
    
    def calculate(self, s: str) -> int:
        
        parser = Parser(s)

        expr = parser.parse()

        return expr.eval()
40349