diff --git a/calc_engine.py b/calc_engine.py index 79771af..771c2a8 100644 --- a/calc_engine.py +++ b/calc_engine.py @@ -1,4 +1,4 @@ -import token +import ptoken as token import copy def calculate_results(tokens): @@ -8,7 +8,7 @@ def calculate_results(tokens): def handle_parenthesis(tokens, call_has_priority = False): running_value = 0 - previous_token = token.CToken("", token.TokenType.unknown) + previous_token = token.Token("", token.TokenType.unknown) while True: if len(tokens) == 0: @@ -40,7 +40,7 @@ def handle_parenthesis(tokens, call_has_priority = False): if not previous_token.type == token.TokenType.unknown: tokens.append(previous_token) else: - tokens.append(token.CToken(running_value, token.TokenType.constant)) + tokens.append(token.Token(running_value, token.TokenType.constant)) running_value = handle_add_and_subtract(tokens) #Set the previous_token to an invalid state so its not used by mistake. @@ -103,7 +103,7 @@ def handle_parenthesis(tokens, call_has_priority = False): def handle_multiply_and_division(tokens): running_value = 0 - previous_token = token.CToken("", token.TokenType.unknown) + previous_token = token.Token("", token.TokenType.unknown) while True: if len(tokens) == 0: @@ -156,7 +156,7 @@ def handle_add_and_subtract(tokens): # need to change for the life of this project. # running_value = 0 - previous_token = token.CToken("", token.TokenType.unknown) + previous_token = token.Token("", token.TokenType.unknown) while True: diff --git a/token.py b/ptoken.py similarity index 98% rename from token.py rename to ptoken.py index 879d829..6cd5292 100755 --- a/token.py +++ b/ptoken.py @@ -1,4 +1,4 @@ -class CToken: +class Token: def __init__(self, value, type): self.value = value self.type = type diff --git a/tokenizer.py b/tokenizer.py index 2b16fc2..8ea7744 100644 --- a/tokenizer.py +++ b/tokenizer.py @@ -1,4 +1,4 @@ -import token +import ptoken as token def get_tokens_from_expression_string(expression_string): @@ -18,31 +18,31 @@ def get_tokens_from_expression_string(expression_string): if len(tokens) > 1: lookBehind = tokens[-1] if lookBehind.type == token.TokenType.exp_end: - tokens.append(token.CToken("^", token.TokenType.power)) + tokens.append(token.Token("^", token.TokenType.power)) if (i + 1) == len(expression_string): - tokens.append(token.CToken(tmp + c, token.TokenType.constant)) + tokens.append(token.Token(tmp + c, token.TokenType.constant)) parsing_number = True tmp = tmp + c continue elif c == '(': if parsing_number: - tokens.append(token.CToken(tmp, token.TokenType.constant)) - tokens.append(token.CToken("*", token.TokenType.multiply)) - tokens.append(token.CToken("(", token.TokenType.exp_start)) + tokens.append(token.Token(tmp, token.TokenType.constant)) + tokens.append(token.Token("*", token.TokenType.multiply)) + tokens.append(token.Token("(", token.TokenType.exp_start)) parsing_number = False tmp = "" continue elif c == ')': if parsing_number: - tokens.append(token.CToken(tmp, token.TokenType.constant)) - tokens.append(token.CToken(")", token.TokenType.exp_end)) + tokens.append(token.Token(tmp, token.TokenType.constant)) + tokens.append(token.Token(")", token.TokenType.exp_end)) parsing_number = False tmp = "" continue elif token.TokenType.get_operator(c) != token.TokenType.unknown: if parsing_number: - tokens.append(token.CToken(tmp, token.TokenType.constant)) - tokens.append(token.CToken(c, token.TokenType.get_operator(c))) + tokens.append(token.Token(tmp, token.TokenType.constant)) + tokens.append(token.Token(c, token.TokenType.get_operator(c))) parsing_number = False tmp = "" continue @@ -50,23 +50,23 @@ def get_tokens_from_expression_string(expression_string): if len(tokens) > 1: lookBehind = tokens[-1] if lookBehind.type == token.TokenType.exp_end: - tokens.append(token.CToken("^", token.TokenType.power)) + tokens.append(token.Token("^", token.TokenType.power)) if (i + 1) < len(expression_string): lookAHead = expression_string[i + 1] if lookAHead == '(': - tokens.append(token.CToken(c, token.TokenType.variable)) - tokens.append(token.CToken("*", token.TokenType.multiply)) + tokens.append(token.Token(c, token.TokenType.variable)) + tokens.append(token.Token("*", token.TokenType.multiply)) tmp = "" parsing_number = False continue if parsing_number: - tokens.append(token.CToken(tmp, token.TokenType.constant)) - tokens.append(token.CToken("*", token.TokenType.multiply)) + tokens.append(token.Token(tmp, token.TokenType.constant)) + tokens.append(token.Token("*", token.TokenType.multiply)) if c in symbols_dic.keys(): symbols_dic[c] += 1 else: symbols_dic[c] = 1 - tokens.append(token.CToken(c + str(symbols_dic[c]), token.TokenType.variable)) + tokens.append(token.Token(c + str(symbols_dic[c]), token.TokenType.variable)) parsing_number = False tmp = "" @@ -77,8 +77,8 @@ def replace_variables(tokens, new_constants): newTokenList = [] for t in tokens: if t.type == token.TokenType.variable: - newTokenList.append(token.CToken(new_constants.pop(0), token.TokenType.constant)) + newTokenList.append(token.Token(new_constants.pop(0), token.TokenType.constant)) else: - newTokenList.append(token.CToken(t.value, t.type)) + newTokenList.append(token.Token(t.value, t.type)) return newTokenList