Inital commit.
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
import token
|
||||||
|
|
||||||
|
def flatten_input(input):
|
||||||
|
tokens = []
|
||||||
|
tmp = ""
|
||||||
|
parsing_number = False
|
||||||
|
|
||||||
|
for i in range(0, len(input)):
|
||||||
|
c = input[i]
|
||||||
|
if c == '.' or c.isdigit():
|
||||||
|
if len(tokens) > 1:
|
||||||
|
lookBehind = tokens[len(tokens) - 1]
|
||||||
|
|
||||||
|
if lookBehind == token.TokenType.exp_start:
|
||||||
|
tokens.append(token.Token("^", token.TokenType.power))
|
||||||
|
if (i + 1) == len(input):
|
||||||
|
tokens.append(token.Token(tmp + c, token.TokenType.constant))
|
||||||
|
|
||||||
|
parsing_number = True
|
||||||
|
tmp = tmp + c
|
||||||
|
continue
|
||||||
|
|
||||||
|
print(tokens)
|
||||||
|
|
||||||
|
def operate(n1, n2, tokenType):
|
||||||
|
if tokenType == token.TokenType.add:
|
||||||
|
print("Adding %d and %d to get %d." %(n1, n2, n1 + n2))
|
||||||
|
elif tokenType == token.TokenType.subtract:
|
||||||
|
print("Subtracting %d and %d to get %d." %(n1, n2, n1 - n2))
|
||||||
|
elif tokenType == token.TokenType.multiply:
|
||||||
|
print("Multiplying %d and %d to get %d" %(n1, n2, n1 * n2))
|
||||||
|
elif tokenType == token.TokenType.divide:
|
||||||
|
print("Dividing %d and %d to get %d" %(n1, n2, n1 / n2))
|
||||||
|
elif tokenType == token.TokenType.power:
|
||||||
|
print("Raising %d to the power of %d to get %d" %(n1, n2, n1**n2))
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Invalid operator value.")
|
||||||
|
|
||||||
|
#t1 = token.TokenType()
|
||||||
|
#t = token.Token("+", token.TokenType.add)
|
||||||
|
#print(t1.get_operator_verb(t.type))
|
||||||
|
operate(10, 2, 4)
|
||||||
|
operate(10, 2, 0)
|
||||||
|
operate(10, 2, 1)
|
||||||
|
operate(10, 2, 2)
|
||||||
|
operate(10, 2, 3)
|
||||||
|
flatten_input("THis is 96.")
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
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))
|
||||||
Reference in New Issue
Block a user