From 0a79cdba54e91857287c6d589afa249d9cb91df2 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 16 Apr 2020 14:19:59 -0500 Subject: [PATCH] Fixed a grammar check bug in the tokenizer. --- main.py | 23 ++++++++++++----------- tokenizer.py | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 63e4429..2bc5061 100755 --- a/main.py +++ b/main.py @@ -1,27 +1,28 @@ import token import tokenizer -import expression +#import expression import calc_engine -import json +#import jsonpickle #fileLocation = input('File Path to JSON: ') #json = json.loads(open(fileLocation).read()) -userInput = input('Enter your formula: ') -tokens = tokenizer.get_tokens_from_expression_string(userInput) +#userInput = input('Enter your formula: ') +tokens = tokenizer.get_tokens_from_expression_string("5000(2(2(1.08^10)-1)/0.08)") #with open("expression.json", "r") as f: # j = json.load(f) -# exp = expression.Expression(json.loads(j)) +# exp = jsonpickle.decode(f.read()) + #exp = expression.Expression.from_json(json.loads(json.load(f))) # print(exp) -#results = calc_engine.calculate_results(tokens.copy()) -#print(results) +results = calc_engine.calculate_results(tokens.copy()) +print(results) -tokens = tokenizer.replace_variables(tokens, [23, 46, 89]) +#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() +#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) diff --git a/tokenizer.py b/tokenizer.py index 7d4717c..2b16fc2 100644 --- a/tokenizer.py +++ b/tokenizer.py @@ -13,7 +13,7 @@ def get_tokens_from_expression_string(expression_string): for i in range(0, len(expression_string)): c = expression_string[i] if c == '.' or c.isdigit(): - if tmp.count('.') == 1: + if c == '.' and tmp.count('.') == 1: raise SyntaxError("Invalid grammar, to many periods in number. Found at position " + str (i) + ".") if len(tokens) > 1: lookBehind = tokens[-1]