feat: add task 2 calculator draft
This commit is contained in:
28
task2/main.py
Normal file
28
task2/main.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from collections import deque
|
||||
from icecream import ic
|
||||
from typing import Iterable
|
||||
|
||||
from command_translation import translate
|
||||
from tokenizer import tokenize
|
||||
|
||||
from math_objects import Token, Integer, Operator
|
||||
|
||||
equation: str = "1+ 2+3/3-1"# input()
|
||||
|
||||
tokens: Iterable[Token] = tokenize(equation)
|
||||
sorted_tokens: Iterable[Token] = translate(tokens)
|
||||
|
||||
token_stack: deque[Token] = deque()
|
||||
|
||||
for token in sorted_tokens:
|
||||
if not isinstance(token, Operator):
|
||||
token_stack.append(token)
|
||||
continue
|
||||
b, a = token_stack.pop(), token_stack.pop()
|
||||
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