CODE-0006 · gpt-5.5-pro (high)
def eval_expr(s):
    if not isinstance(s, str):
        raise ValueError("expression must be a string")

    from fractions import Fraction

    n = len(s)
    values = []
    ops = []

    def is_digit(ch):
        return "0" <= ch <= "9"

    def read_number(index):
        start = index
        digits_before = 0
        while index < n and is_digit(s[index]):
            index += 1
            digits_before += 1

        has_dot = False
        digits_after = 0
        if index < n and s[index] == ".":
            has_dot = True
            index += 1
            while index < n and is_digit(s[index]):
                index += 1
                digits_after += 1

        if digits_before == 0 and digits_after == 0:
            raise ValueError("malformed number")

        text = s[start:index]
        if not has_dot:
            return Fraction(int(text), 1), index

        whole, frac = text.split(".", 1)
        numerator_text = (whole if whole else "0") + frac
        numerator = int(numerator_text) if numerator_text else 0
        denominator = 10 ** len(frac)
        return Fraction(numerator, denominator), index

    def precedence(op):
        if op in ("u+", "u-"):
            return 3
        if op in ("*", "/"):
            return 2
        if op in ("+", "-"):
            return 1
        return 0

    def apply_operator(op):
        if op == "u+":
            if not values:
                raise ValueError("malformed expression")
            values.append(+values.pop())
        elif op == "u-":
            if not values:
                raise ValueError("malformed expression")
            values.append(-values.pop())
        else:
            if len(values) < 2:
                raise ValueError("malformed expression")
            right = values.pop()
            left = values.pop()

            if op == "+":
                values.append(left + right)
            elif op == "-":
                values.append(left - right)
            elif op == "*":
                values.append(left * right)
            elif op == "/":
                if right == 0:
                    raise ValueError("division by zero")
                values.append(left / right)
            else:
                raise ValueError("malformed expression")

    expecting_operand = True
    i = 0

    while i < n:
        ch = s[i]

        if ch.isspace():
            i += 1
            continue

        if is_digit(ch) or ch == ".":
            if not expecting_operand:
                raise ValueError("malformed expression")
            value, i = read_number(i)
            values.append(value)
            expecting_operand = False
            continue

        if ch == "(":
            if not expecting_operand:
                raise ValueError("malformed expression")
            ops.append(ch)
            i += 1
            expecting_operand = True
            continue

        if ch == ")":
            if expecting_operand:
                raise ValueError("malformed expression")

            found_left_paren = False
            while ops:
                op = ops.pop()
                if op == "(":
                    found_left_paren = True
                    break
                apply_operator(op)

            if not found_left_paren:
                raise ValueError("mismatched parentheses")

            i += 1
            expecting_operand = False
            continue

        if ch in "+-*/":
            if expecting_operand:
                if ch in "+-":
                    ops.append("u" + ch)
                    i += 1
                    continue
                raise ValueError("malformed expression")

            while ops and ops[-1] != "(" and precedence(ops[-1]) >= precedence(ch):
                apply_operator(ops.pop())

            ops.append(ch)
            i += 1
            expecting_operand = True
            continue

        raise ValueError("unknown token")

    if expecting_operand:
        raise ValueError("malformed expression")

    while ops:
        op = ops.pop()
        if op == "(":
            raise ValueError("mismatched parentheses")
        apply_operator(op)

    if len(values) != 1:
        raise ValueError("malformed expression")

    result = values[0]
    if result.denominator == 1:
        return result.numerator
    return float(result)