Updated exceptions to be more fitting, fixed a parse bug in the tokenizer and added the start of the calc engine.

This commit is contained in:
2020-04-08 00:08:20 -05:00
parent c760dc04b5
commit 528098d913
3 changed files with 28 additions and 17 deletions
+25
View File
@@ -0,0 +1,25 @@
import token
def calculate_results(tokens):
return 0
def peek_list(tokens):
if tokens:
return tokens[-1]
else:
raise IndexError("The token list is empty.", 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 TypeError("Invalid operator value " + str(tokenType) + ".", tokenType)