From c75905d13caadfab429d748c9ff5b54be3ef514b Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Mon, 13 Apr 2020 09:10:14 -0500 Subject: [PATCH] Updated the tokenizer to include a method to replace variable tokens with the values provided in a list. --- calc_engine.py | 7 +++++-- main.py | 18 ++++++++++-------- tokenizer.py | 14 ++++++++++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/calc_engine.py b/calc_engine.py index 7f9d76b..79771af 100644 --- a/calc_engine.py +++ b/calc_engine.py @@ -1,7 +1,10 @@ import token +import copy -def calculate_results(tokens): - return handle_add_and_subtract(tokens) +def calculate_results(tokens): + new_list = copy.deepcopy(tokens) + new_list.reverse() + return handle_add_and_subtract(new_list) def handle_parenthesis(tokens, call_has_priority = False): running_value = 0 diff --git a/main.py b/main.py index 0eaf87e..63e4429 100755 --- a/main.py +++ b/main.py @@ -4,22 +4,24 @@ import expression import calc_engine import json -fileLocation = input('File Path to JSON: ') +#fileLocation = input('File Path to JSON: ') #json = json.loads(open(fileLocation).read()) -#userInput = input('Enter your formula: ') -tokens = tokenizer.get_tokens_from_expression_string("2 + 6 / 2") -with open("expression.json", "r") as f: - j = json.load(f) - exp = expression.Expression(json.loads(j)) - print(exp) +userInput = input('Enter your formula: ') +tokens = tokenizer.get_tokens_from_expression_string(userInput) +#with open("expression.json", "r") as f: +# j = json.load(f) +# exp = expression.Expression(json.loads(j)) +# print(exp) #results = calc_engine.calculate_results(tokens.copy()) #print(results) +tokens = tokenizer.replace_variables(tokens, [23, 46, 89]) + for t in tokens: print("%s (%s) " %(t.value, token.TokenType.get_token_type_name(t.type)), end = "") print() -def factors(factor_object, starting_point, variable_type) +#def factors(factor_object, starting_point, variable_type) diff --git a/tokenizer.py b/tokenizer.py index 51e1aa6..17030c2 100644 --- a/tokenizer.py +++ b/tokenizer.py @@ -69,10 +69,16 @@ def get_tokens_from_expression_string(expression_string): tokens.append(token.CToken(c + str(symbols_dic[c]), token.TokenType.variable)) parsing_number = False tmp = "" - #When building this list we've effectively ordered the tokens in reverse order from how we want to - #process them. So, reverse the order of the list before returning them to the caller, we don't want - #the caller to have to worry about such a minor detail. - tokens.reverse() return tokens + +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)) + else: + newTokenList.append(t) + + return newTokenList