Files
calc_engine/token.py
T
2020-03-09 18:44:02 -05:00

47 lines
1.1 KiB
Python
Executable File

class Token:
def __init__(self, value, type):
self.value = value
self.type = type
class TokenType:
add = 0
subtract = 1
multiply = 2
divide = 3
power = 4
variable = 5
constant = 6
exp_start = 7
exp_end = 8
unknown = 9
OPERATOR_VERBS = {
0 : "Adding",
1 : "Subtracting",
2 : "Multiplying",
3 : "Dividing",
4 : "Raising"
}
OPERATORS = {
'+' : add,
'-' : subtract,
'*' : multiply,
'^' : power,
'/' : divide
}
def get_operator_verb(self, token):
if not token in self.OPERATOR_VERBS:
raise ValueError("An operator verb could not be found.", "token")
return self.OPERATOR_VERBS[token]
def get_operator(self, character):
if not character in self.OPERATORS:
raise ValueError(f"Invalid operator '{character}' could not be found in the operators list.", "character")
return self.OPERATORS[character]
#t = TokenType()
#t1 = Token("$", TokenType.Unknown)
#print(t.GetOperator(t1.value))