From fac2b2d6254436f7c16e4986e54635426b3c27a8 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Sun, 26 Jul 2020 00:25:09 -0500 Subject: [PATCH] Refactored some code to try and make it more inline with best pratices. Renamed parser as it conflicted with a standard Python library. --- parser.py => ast_gen.py | 24 ++++++++-------- ptoken.py => base_unit.py | 0 calc_engine.py | 18 ++++++------ main.py | 18 ++++++------ tokenizer.py | 58 +++++++++++++++++++-------------------- 5 files changed, 58 insertions(+), 60 deletions(-) rename parser.py => ast_gen.py (78%) rename ptoken.py => base_unit.py (100%) diff --git a/parser.py b/ast_gen.py similarity index 78% rename from parser.py rename to ast_gen.py index 7498384..c6610a6 100644 --- a/parser.py +++ b/ast_gen.py @@ -1,6 +1,4 @@ -import ptoken as token -import tokenizer -import uuid +from base_unit import Token, TokenType from collections import deque class Op: @@ -10,7 +8,7 @@ class Op: self.right = right def __str__(self): - return "%s (%s) and (%s)" %(token.TokenType.get_operator_verb(self.op), self.left, self.right) + return "%s (%s) and (%s)" %(TokenType.get_operator_verb(self.op), self.left, self.right) class Num: def __init__(self, token): @@ -46,11 +44,11 @@ class Parser: new_list = [] for t in self.bak: - if t.type == token.TokenType.variable: + if t.type == TokenType.variable: if t.value in self.variables: - new_list.append(token.Token(self.variables[t.value], token.TokenType.constant)) + new_list.append(Token(self.variables[t.value], TokenType.constant)) else: - new_list.append(token.Token(t.value, t.type)) + new_list.append(Token(t.value, t.type)) return new_list def advance_current_token(self): @@ -60,16 +58,16 @@ class Parser: self.current_token = self.tokens.popleft() def factor(self): - if self.current_token.type == token.TokenType.constant: + if self.current_token.type == TokenType.constant: node = Num(self.current_token) self.advance_current_token() return node - elif self.current_token.type == token.TokenType.variable: + elif self.current_token.type == TokenType.variable: node = Variable(self.current_token) self.variables[self.current_token.value] = node self.advance_current_token() return node - elif self.current_token.type == token.TokenType.exp_start: + elif self.current_token.type == TokenType.exp_start: self.advance_current_token() # ( node = self.topLevel() self.advance_current_token() # ) @@ -78,7 +76,7 @@ class Parser: def lowLevel(self): node = self.factor() - while self.current_token.type == token.TokenType.power: + while self.current_token.type == TokenType.power: tmp = self.current_token self.advance_current_token() node = Op(left=node, op=tmp.type, right=self.factor()) @@ -88,7 +86,7 @@ class Parser: def midLevel(self): node = self.lowLevel() - while self.current_token.type in (token.TokenType.multiply, token.TokenType.divide): + while self.current_token.type in (TokenType.multiply, TokenType.divide): tmp = self.current_token self.advance_current_token() node = Op(left=node, op=tmp.type, right=self.lowLevel()) @@ -98,7 +96,7 @@ class Parser: def topLevel(self): node = self.midLevel() - while self.current_token.type in (token.TokenType.add, token.TokenType.subtract): + while self.current_token.type in (TokenType.add, TokenType.subtract): tmp = self.current_token self.advance_current_token() node = Op(left=node, op=tmp.type, right=self.midLevel()) diff --git a/ptoken.py b/base_unit.py similarity index 100% rename from ptoken.py rename to base_unit.py diff --git a/calc_engine.py b/calc_engine.py index 0786225..2d78389 100644 --- a/calc_engine.py +++ b/calc_engine.py @@ -1,13 +1,13 @@ -import ptoken -import parser +from base_unit import Token, TokenType +from ast_gen import Variable, Num debug = False def calculate_results(node): - if type(node) is parser.Num: + if type(node) is Num: return float(node.value) - elif type(node) is parser.Variable: + elif type(node) is Variable: return node.value left = calculate_results(node.left) @@ -21,19 +21,19 @@ def operate(n1, n2, tokenType): n1 = float(n1) n2 = float(n2) - if tokenType == ptoken.TokenType.add: + if tokenType == TokenType.add: if debug: print("Adding %f and %f to get %f." %(n1, n2, n1 + n2)) return n1 + n2 - elif tokenType == ptoken.TokenType.subtract: + elif tokenType == TokenType.subtract: if debug: print("Subtracting %f and %f to get %f." %(n1, n2, n1 - n2)) return n1 - n2 - elif tokenType == ptoken.TokenType.multiply: + elif tokenType == TokenType.multiply: if debug: print("Multiplying %f and %f to get %f" %(n1, n2, n1 * n2)) return n1 * n2 - elif tokenType == ptoken.TokenType.divide: + elif tokenType == TokenType.divide: if debug: print("Dividing %f and %f to get %f" %(n1, n2, n1 / n2)) return n1 / n2 - elif tokenType == ptoken.TokenType.power: + elif tokenType == TokenType.power: if debug: print("Raising %f to the power of %f to get %f" %(n1, n2, n1**n2)) return n1 ** n2 else: diff --git a/main.py b/main.py index 30e758d..1fbe782 100755 --- a/main.py +++ b/main.py @@ -1,12 +1,12 @@ -import ptoken as token -import tokenizer -import expression -import calc_engine +from base_unit import Token, TokenType +from calc_engine import calculate_results +from expression import Expression, Variable +from tokenizer import get_tokens_from_expression_string, stringify_token_list import factors import jsonpickle import itertools import copy -import parser +from ast_gen import Parser base_list = [1, 2, 4, 6, 12] tolerance = .05 @@ -151,8 +151,8 @@ json = jsonpickle.json.loads(open(fileLocation).read()) with open("expression.json", "r") as f: #j = jsonpickle.json.load(f) exp = jsonpickle.decode(f.read()) -tokens = tokenizer.get_tokens_from_expression_string(exp.expression) -tree = parser.Parser(tokens) +tokens = get_tokens_from_expression_string(exp.expression) +tree = Parser(tokens) #results = calc_engine.calculate_results(tokens.copy()) #print(results) @@ -187,7 +187,7 @@ for r in res: count +=1 t = tree.update_variables(vs)#tokenizer.replace_variables(tokens, list(r)) #tokenizer.print_token_list(t) - result = calc_engine.calculate_results(tree.ast) + result = calculate_results(tree.ast) if result <= int(exp.wrong_response * (1 + tolerance)) and result >= int(exp.wrong_response * (1 - tolerance)): if not result in answers: answers[result] = tree.get_tokens() @@ -201,6 +201,6 @@ with open("results.txt", "w") as f: print("Answers Found: %d" %(len(answers))) for k in answers: f.write("Result: %s for expression: " %(format(k, ".6f"))) - f.write(tokenizer.stringify_token_list(answers[k])) + f.write(stringify_token_list(answers[k])) diff --git a/tokenizer.py b/tokenizer.py index 1f5047a..5e7ea34 100644 --- a/tokenizer.py +++ b/tokenizer.py @@ -1,4 +1,4 @@ -import ptoken as token +from base_unit import Token, TokenType from collections import deque def get_tokens_from_expression_string(expression_string): @@ -18,82 +18,82 @@ def get_tokens_from_expression_string(expression_string): raise SyntaxError("Invalid grammar, to many periods in number. Found at position " + str (i) + ".") if len(tokens) > 1: lookBehind = tokens[-1] - if lookBehind.type == token.TokenType.exp_end: - tokens.append(token.Token("^", token.TokenType.power)) + if lookBehind.type == TokenType.exp_end: + tokens.append(token.Token("^", TokenType.power)) if (i + 1) == len(expression_string): - tokens.append(token.Token(tmp + c, token.TokenType.constant)) + tokens.append(token.Token(tmp + c, TokenType.constant)) parsing_number = True tmp = tmp + c continue elif c == '-' and not parsing_number: if expression_string[i + 1] == '.' or expression_string[i + 1].isdigit(): - if token.TokenType.get_operator(tokens[-1].value) != token.TokenType.unknown:#tokens[-1].type == token.TokenType + if token.TokenType.get_operator(tokens[-1].value) != TokenType.unknown:#tokens[-1].type == token.TokenType #print(tokens[-1].value) #tokens.append(token.Token('+', token.TokenType.add)) parsing_number = True tmp = "-" - elif tokens[-1].type == token.TokenType.exp_start: + elif tokens[-1].type == TokenType.exp_start: parsing_number = True tmp = "-" - elif tokens[-1].type == token.TokenType.exp_end: - tokens.append(token.Token(c, token.TokenType.get_operator(c))) + elif tokens[-1].type == TokenType.exp_end: + tokens.append(Token(c, TokenType.get_operator(c))) parsing_number = False tmp = "" elif expression_string[i + 1].isalpha(): # Must be a variable. - if tokens[-1].type == token.TokenType.exp_start: + if tokens[-1].type == TokenType.exp_start: variable_is_negative = True - elif token.TokenType.get_operator(tokens[-1].value) != token.TokenType.unknown: + elif token.TokenType.get_operator(tokens[-1].value) != TokenType.unknown: variable_is_negative = True - elif tokens[-1].type == token.TokenType.exp_end: - tokens.append(token.Token(c, token.TokenType.get_operator(c))) + elif tokens[-1].type == TokenType.exp_end: + tokens.append(Token(c, TokenType.get_operator(c))) parsing_number = False tmp = "" else: - tokens.append(token.Token(c, token.TokenType.get_operator(c))) + tokens.append(Token(c, TokenType.get_operator(c))) parsing_number = False tmp = "" elif c == '(': if parsing_number: - tokens.append(token.Token(tmp, token.TokenType.constant)) - tokens.append(token.Token("*", token.TokenType.multiply)) - tokens.append(token.Token("(", token.TokenType.exp_start)) + tokens.append(token.Token(tmp, TokenType.constant)) + tokens.append(token.Token("*", TokenType.multiply)) + tokens.append(Token("(", TokenType.exp_start)) parsing_number = False tmp = "" continue elif c == ')': if parsing_number: - tokens.append(token.Token(tmp, token.TokenType.constant)) - tokens.append(token.Token(")", token.TokenType.exp_end)) + tokens.append(token.Token(tmp, TokenType.constant)) + tokens.append(Token(")", TokenType.exp_end)) parsing_number = False tmp = "" continue - elif token.TokenType.get_operator(c) != token.TokenType.unknown: + elif TokenType.get_operator(c) != TokenType.unknown: if parsing_number: - tokens.append(token.Token(tmp, token.TokenType.constant)) - tokens.append(token.Token(c, token.TokenType.get_operator(c))) + tokens.append(Token(tmp, TokenType.constant)) + tokens.append(Token(c, TokenType.get_operator(c))) parsing_number = False tmp = "" continue elif c != ' ': if len(tokens) > 1: lookBehind = tokens[-1] - if lookBehind.type == token.TokenType.exp_end: - tokens.append(token.Token("^", token.TokenType.power)) + if lookBehind.type == TokenType.exp_end: + tokens.append(Token("^", TokenType.power)) if (i + 1) < len(expression_string): lookAHead = expression_string[i + 1] if lookAHead == '(': - tokens.append(token.Token(c, token.TokenType.variable)) - tokens.append(token.Token("*", token.TokenType.multiply)) + tokens.append(Token(c, TokenType.variable)) + tokens.append(Token("*", TokenType.multiply)) tmp = "" parsing_number = False continue if parsing_number: - tokens.append(token.Token(tmp, token.TokenType.constant)) - tokens.append(token.Token("*", token.TokenType.multiply)) + tokens.append(Token(tmp, TokenType.constant)) + tokens.append(Token("*", TokenType.multiply)) # Check to see if the encountered variable, which is effectively a single character, # has been seen before. If it has been then simply increment the number that represents how many times it's been seen # else, add a new entry for it in the dictonary 'symbols_dic'. @@ -102,7 +102,7 @@ def get_tokens_from_expression_string(expression_string): else: symbols_dic[c] = 1 # Afterwards, add the variable with the count of the times it's been seen as a token object to the list of tokens. - tokens.append(token.Token(c + str(symbols_dic[c]), token.TokenType.variable, variable_is_negative)) + tokens.append(Token(c + str(symbols_dic[c]), TokenType.variable, variable_is_negative)) variable_is_negative = False parsing_number = False tmp = "" @@ -119,7 +119,7 @@ def peek_list(tokens): def print_token_list(tokens): # Prints the list of Tokens to standard out. for tk in tokens: - if tk.type == token.TokenType.variable: + if tk.type == TokenType.variable: if tk.is_negative_variable: print("-%s " %(tk.value), end = "") else: