Fixed addition and subraction bug in the calc_engine.
This commit is contained in:
+23
-10
@@ -146,6 +146,12 @@ def handle_multiply_and_division(tokens):
|
|||||||
return running_value
|
return running_value
|
||||||
|
|
||||||
def handle_add_and_subtract(tokens):
|
def handle_add_and_subtract(tokens):
|
||||||
|
#
|
||||||
|
# Note: this is should be the first function called to calculate the result of the tokens.
|
||||||
|
# A bit hacky, and not the best name for this method, but it grew "orgranically" to be
|
||||||
|
# this way. For now this is fine, and for what the calc_engine does this may not
|
||||||
|
# need to change for the life of this project.
|
||||||
|
#
|
||||||
running_value = 0
|
running_value = 0
|
||||||
previous_token = token.CToken("", token.TokenType.unknown)
|
previous_token = token.CToken("", token.TokenType.unknown)
|
||||||
|
|
||||||
@@ -162,7 +168,7 @@ def handle_add_and_subtract(tokens):
|
|||||||
|
|
||||||
if current_token.type == token.TokenType.exp_end:
|
if current_token.type == token.TokenType.exp_end:
|
||||||
tokens.append(current_token)
|
tokens.append(current_token)
|
||||||
break
|
break
|
||||||
|
|
||||||
if current_token.type == token.TokenType.exp_start:
|
if current_token.type == token.TokenType.exp_start:
|
||||||
running_value = handle_parenthesis(tokens)
|
running_value = handle_parenthesis(tokens)
|
||||||
@@ -177,11 +183,11 @@ def handle_add_and_subtract(tokens):
|
|||||||
running_value = operate(previous_token.value, tmp, current_token.type)
|
running_value = operate(previous_token.value, tmp, current_token.type)
|
||||||
else:
|
else:
|
||||||
running_value = operate(running_value, tmp, current_token.type)
|
running_value = operate(running_value, tmp, current_token.type)
|
||||||
|
#Set the previous_token to an invalid state so its not used by mistake.
|
||||||
previous_token.type = token.TokenType.unknown
|
previous_token.type = token.TokenType.unknown
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if len(tokens) > 0:
|
elif len(tokens) > 0:
|
||||||
two_ahead = peek_list(tokens)
|
two_ahead = peek_list(tokens)
|
||||||
|
|
||||||
if two_ahead.type == token.TokenType.multiply or two_ahead.type == token.TokenType.divide:
|
if two_ahead.type == token.TokenType.multiply or two_ahead.type == token.TokenType.divide:
|
||||||
@@ -193,14 +199,17 @@ def handle_add_and_subtract(tokens):
|
|||||||
running_value = operate(previous_token.value, tmp, current_token.type)
|
running_value = operate(previous_token.value, tmp, current_token.type)
|
||||||
else:
|
else:
|
||||||
running_value = operate(running_value, tmp, current_token.type)
|
running_value = operate(running_value, tmp, current_token.type)
|
||||||
|
#Set the previous_token to an invalid state so its not used by mistake.
|
||||||
previous_token.type = token.TokenType.unknown
|
previous_token.type = token.TokenType.unknown
|
||||||
|
|
||||||
|
continue
|
||||||
|
|
||||||
|
if running_value == 0:
|
||||||
|
running_value = operate(previous_token.value, look_ahead.value, current_token.type)
|
||||||
else:
|
else:
|
||||||
if running_value == 0:
|
running_value = operate(running_value, look_ahead.value, current_token.type)
|
||||||
running_value = operate(previous_token.value, look_ahead.value, current_token.type)
|
#Set the previous_token to an invalid state so its not used by mistake.
|
||||||
else:
|
previous_token.type = token.TokenType.unknown
|
||||||
running_value = operate(running_value, look_ahead.value, current_token.type)
|
|
||||||
previous_token.type = token.TokenType.unknown
|
|
||||||
|
|
||||||
elif current_token.type == token.TokenType.multiply or current_token.type == token.TokenType.divide:
|
elif current_token.type == token.TokenType.multiply or current_token.type == token.TokenType.divide:
|
||||||
tokens.append(current_token)
|
tokens.append(current_token)
|
||||||
@@ -211,8 +220,12 @@ def handle_add_and_subtract(tokens):
|
|||||||
tokens.append(token.CToken(running_value, token.TokenType.constant))
|
tokens.append(token.CToken(running_value, token.TokenType.constant))
|
||||||
|
|
||||||
running_value = handle_multiply_and_division(tokens)
|
running_value = handle_multiply_and_division(tokens)
|
||||||
|
#Set the previous_token to an invalid state so its not used by mistake.
|
||||||
previous_token.type = token.TokenType.unknown
|
previous_token.type = token.TokenType.unknown
|
||||||
|
|
||||||
|
#elif current_token.type == token.TokenType.power:
|
||||||
|
#In theory, if we hit this branch the expression wuld look something like this: a ^ b.
|
||||||
|
|
||||||
|
|
||||||
return running_value
|
return running_value
|
||||||
|
|
||||||
|
|||||||
@@ -52,13 +52,13 @@ class TokenType:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_operator(character):
|
def get_operator(character):
|
||||||
"""
|
#
|
||||||
Accepts an individual character and returns either the math operator TokenType or unknown.
|
#Accepts an individual character and returns either the math operator TokenType or unknown.
|
||||||
"""
|
#
|
||||||
if not character in TokenType.OPERATORS:
|
if not character in TokenType.OPERATORS:
|
||||||
return TokenType.unknown
|
return TokenType.unknown
|
||||||
return TokenType.OPERATORS[character]
|
return TokenType.OPERATORS[character]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_operator_name(tokenType):
|
def get_token_type_name(tokenType):
|
||||||
return TokenType.TOKEN_NAMES[tokenType]
|
return TokenType.TOKEN_NAMES[tokenType]
|
||||||
|
|||||||
Reference in New Issue
Block a user