Fixed addition and subraction bug in the calc_engine.

This commit is contained in:
2020-04-10 14:34:37 -05:00
parent ee9ba528c6
commit 9cf6952eb8
2 changed files with 27 additions and 14 deletions
+23 -10
View File
@@ -146,6 +146,12 @@ def handle_multiply_and_division(tokens):
return running_value
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
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:
tokens.append(current_token)
break
break
if current_token.type == token.TokenType.exp_start:
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)
else:
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
continue
if len(tokens) > 0:
elif len(tokens) > 0:
two_ahead = peek_list(tokens)
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)
else:
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
continue
if running_value == 0:
running_value = operate(previous_token.value, look_ahead.value, current_token.type)
else:
if running_value == 0:
running_value = operate(previous_token.value, look_ahead.value, current_token.type)
else:
running_value = operate(running_value, look_ahead.value, current_token.type)
previous_token.type = token.TokenType.unknown
running_value = operate(running_value, look_ahead.value, current_token.type)
#Set the previous_token to an invalid state so its not used by mistake.
previous_token.type = token.TokenType.unknown
elif current_token.type == token.TokenType.multiply or current_token.type == token.TokenType.divide:
tokens.append(current_token)
@@ -211,8 +220,12 @@ def handle_add_and_subtract(tokens):
tokens.append(token.CToken(running_value, token.TokenType.constant))
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
#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