feat(task2): process equations with "()"

This commit is contained in:
2026-04-09 18:25:10 +06:00
parent 391eaa30a8
commit bb5c00c528
3 changed files with 36 additions and 12 deletions

View File

@@ -2,13 +2,38 @@ from collections import deque
from typing import Iterable
from icecream import ic
from math_objects import Operator, Token
from math_objects import Operator, Parenthesis, Token
__left_parenthesis_error = "__left_parenthesis_error"
__right_parenthesis_error = "__right_parenthesis_error"
__empty_parentheses_error = "__empty_parentheses_error"
def translate(tokens: Iterable[Token]) -> Iterable[Token]:
operator_stack: deque[Operator] = deque()
previous_stacks: deque[deque[Operator]] = deque()
previous_precedence = 0
empty_stack = True
for token in tokens:
if isinstance(token, Parenthesis):
match token.value:
case "(":
previous_stacks.append(operator_stack)
operator_stack = deque()
empty_stack = True
case _:
if empty_stack:
raise SystemError(__empty_parentheses_error)
if operator_stack:
for item in reversed(operator_stack):
yield item
try:
operator_stack = previous_stacks.pop()
except IndexError:
raise SyntaxError(__right_parenthesis_error)
continue
empty_stack = False
if not isinstance(token, Operator):
yield token
continue
@@ -18,7 +43,8 @@ def translate(tokens: Iterable[Token]) -> Iterable[Token]:
operator_stack.clear()
previous_precedence = token.precedence
operator_stack.append(token)
ic(len(operator_stack))
if previous_stacks:
raise SyntaxError(__left_parenthesis_error)
if operator_stack:
for item in reversed(operator_stack):
yield item