feat(task2): process equations with "()"
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user