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)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import token
|
import token
|
||||||
import tokenizer
|
import tokenizer
|
||||||
#userInput = input('Enter your formula: ')
|
#userInput = input('Enter your formula: ')
|
||||||
tokens = tokenizer.get_tokens_from_expression_string("1 + 2 / 3 (3 * 4) 5")
|
tokens = tokenizer.get_tokens_from_expression_string("i + 2 / i (3 * n) 5")
|
||||||
|
|
||||||
for t in tokens:
|
for t in tokens:
|
||||||
print("%s (%s) " %(t.value, token.TokenType.get_operator_name(t.type)), end = "")
|
print("%s (%s) " %(t.value, token.TokenType.get_operator_name(t.type)), end = "")
|
||||||
|
|||||||
+2
-16
@@ -14,7 +14,7 @@ def get_tokens_from_expression_string(expression_string):
|
|||||||
c = expression_string[i]
|
c = expression_string[i]
|
||||||
if c == '.' or c.isdigit():
|
if c == '.' or c.isdigit():
|
||||||
if tmp.count('.') == 1:
|
if tmp.count('.') == 1:
|
||||||
raise ValueError("Invalid grammar, to many periods in number!")
|
raise SyntaxError("Invalid grammar, to many periods in number. Found at position " + str (i) + ".")
|
||||||
if len(tokens) > 1:
|
if len(tokens) > 1:
|
||||||
lookBehind = tokens[-1]
|
lookBehind = tokens[-1]
|
||||||
if lookBehind.type == token.TokenType.exp_end:
|
if lookBehind.type == token.TokenType.exp_end:
|
||||||
@@ -49,7 +49,7 @@ def get_tokens_from_expression_string(expression_string):
|
|||||||
elif c != ' ':
|
elif c != ' ':
|
||||||
if len(tokens) > 1:
|
if len(tokens) > 1:
|
||||||
lookBehind = tokens[-1]
|
lookBehind = tokens[-1]
|
||||||
if lookBehind.type == TokenType.exp_end:
|
if lookBehind.type == token.TokenType.exp_end:
|
||||||
tokens.append(token.CToken("^", token.TokenType.power))
|
tokens.append(token.CToken("^", token.TokenType.power))
|
||||||
if (i + 1) < len(expression_string):
|
if (i + 1) < len(expression_string):
|
||||||
lookAHead = expression_string[i + 1]
|
lookAHead = expression_string[i + 1]
|
||||||
@@ -72,17 +72,3 @@ def get_tokens_from_expression_string(expression_string):
|
|||||||
|
|
||||||
return tokens
|
return 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("Invalid operator value.")
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user