diff --git a/calc_engine.py b/calc_engine.py index d234578..7f9d76b 100644 --- a/calc_engine.py +++ b/calc_engine.py @@ -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 diff --git a/token.py b/token.py index f538c60..879d829 100755 --- a/token.py +++ b/token.py @@ -52,13 +52,13 @@ class TokenType: @staticmethod 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: return TokenType.unknown return TokenType.OPERATORS[character] @staticmethod - def get_operator_name(tokenType): + def get_token_type_name(tokenType): return TokenType.TOKEN_NAMES[tokenType]