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:
+2
-16
@@ -14,7 +14,7 @@ def get_tokens_from_expression_string(expression_string):
|
||||
c = expression_string[i]
|
||||
if c == '.' or c.isdigit():
|
||||
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:
|
||||
lookBehind = tokens[-1]
|
||||
if lookBehind.type == token.TokenType.exp_end:
|
||||
@@ -49,7 +49,7 @@ def get_tokens_from_expression_string(expression_string):
|
||||
elif c != ' ':
|
||||
if len(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))
|
||||
if (i + 1) < len(expression_string):
|
||||
lookAHead = expression_string[i + 1]
|
||||
@@ -72,17 +72,3 @@ def get_tokens_from_expression_string(expression_string):
|
||||
|
||||
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