Updated the tokenizer to include a method to replace variable tokens with the values provided in a list.

This commit is contained in:
2020-04-13 09:10:14 -05:00
parent 7605bcdf68
commit c75905d13c
3 changed files with 25 additions and 14 deletions
+5 -2
View File
@@ -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
+10 -8
View File
@@ -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)
+10 -4
View File
@@ -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