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:
@@ -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)
|
||||
Reference in New Issue
Block a user