<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 Expr:
    def __init__(self, value: str, left: 'Expr' = None, right: 'Expr' = None):
        self.value = value
        self.left = left
        self.right = right

    # def toString(self):
    #     s = self.value
    #     hasLeft = self.left is not None
    #     hasRight = self.right is not None
    #     if not (hasLeft or hasRight):
    #         return s
    #     s += "("
    #     if hasLeft:
    #         s += self.left.toString()
    #     if hasRight:
    #         s += " "
    #         s += self.right.toString()
    #     s += ")"
    #     return s

class Solution:
    def calculate(self, s: str) -> int:
        root = Parser(s).parse()
        #print(root.toString())
        return self.evaluate(root)

    def evaluate(self, expr: Expr) -> int:
        if expr.value.isdigit():
            return int(expr.value)
        elif expr.value == '-':
            if expr.right is None:
                return -self.evaluate(expr.left)
            else:
                return self.evaluate(expr.left) - self.evaluate(expr.right)
        elif expr.value == '+':
            return self.evaluate(expr.left) + self.evaluate(expr.right)
        elif expr.value == '/':
            return self.evaluate(expr.left) / self.evaluate(expr.right)
        elif expr.value == '*':
            return self.evaluate(expr.left) * self.evaluate(expr.right)        

class Parser:
    def __init__(self, tokens: str):
        self.tokens = tokens
        self.current = 0

    def parse(self) -> Expr:
        return self.term()
            
    def term(self) -> Expr:
        expr = self.factor()
        matched, token = self.tryMatch("+-")
        while matched:
            right = self.factor()
            expr = Expr(token, expr, right)
            matched, token = self.tryMatch("+-")
        return expr

    def factor(self) -> Expr:
        expr = self.negative()
        matched, token = self.tryMatch("*/")
        while matched:
            right = self.negative()
            expr = Expr(token, expr, right)
            matched, token = self.tryMatch("*/")
        return expr

    def negative(self) -> Expr:
        if self.match("-"):
            return Expr("-", self.group())
        return self.group()

    def group(self) -> Expr:
        if self.match("("):
            expr = self.parse()
            self.pop()
            return expr
        return self.number()

    def number(self) -> Expr:
        s = ""
        while self.peek() is not None and self.peek().isdigit():
            s += self.pop()
        return Expr(s)
        
    def tryMatch(self, template:str) -> (bool, str):
        for c in template:
            if self.peek() == c:
                self.current += 1
                return True, c
        return False, None

    def match(self, token:str) -> str:
        if self.peek() == token:
            self.current += 1
            return True
        return False

    def pop(self) -> str:
        self.token = self.peek()
        self.current += 1
        return self.token

    def peek(self) -> str:
        self.trim()
        if self.current >= len(self.tokens):
            return None
        return self.tokens[self.current]

    def trim(self):
        while (self.current < len(self.tokens) and 
                self.tokens[self.current] == ' '):
            self.current += 1

38463