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

@@ -7,7 +7,10 @@ from tokenizer import tokenize
from math_objects import Token, Integer, Operator
equation: str = "1+ 2+3/3-1"# input()
equation: str = "1+0-1/(0+2)"# input()
if not equation.strip():
raise SyntaxError("Пустая строка")
tokens: Iterable[Token] = tokenize(equation)
sorted_tokens: Iterable[Token] = translate(tokens)
@@ -18,11 +21,12 @@ for token in sorted_tokens:
if not isinstance(token, Operator):
token_stack.append(token)
continue
b, a = token_stack.pop(), token_stack.pop()
try:
b, a = token_stack.pop(), token_stack.pop()
except IndexError:
raise SyntaxError
ic(a.value, token.value, b.value)
new_integer = Integer(eval(f"{a.value}{token.value}{b.value}"))
token_stack.append(new_integer)
ic(len(token_stack))
print(token_stack.pop().value)