-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
35 lines (27 loc) · 999 Bytes
/
Copy pathcontroller.py
File metadata and controls
35 lines (27 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from errors import Error
from errorStorage import ErrorStorage
from postfix.calculator import calculate
from postfix.postfix import Postfix
from tree.tree import create_tree
from tokens.parser import Parser
from tokens.token import TokenType
def calculate_values(expression, x_values) -> list | None:
if not expression:
return None
tokens = Parser(expression).parse()
if tokens is None:
return None
postfix = Postfix().create_postfix(tokens)
if len(postfix) == 1:
return fill_values(x_values, postfix[0])
root = create_tree(postfix)
result = calculate(root, x_values)
return result
# ------------------------------- INTERNAL ----------------------------------- #
def fill_values(x_values, token) -> list | None:
if token.type is TokenType.X:
return x_values
if token.type is TokenType.NUMBER:
return [token.data] * len(x_values)
ErrorStorage.put_error(Error.INTERNAL_EXCEPTION_SINGLE_TOKEN)
return None