Refactored some code to try and make it more inline with best pratices. Renamed parser as it conflicted with a standard Python library.
This commit is contained in:
+11
-13
@@ -1,6 +1,4 @@
|
|||||||
import ptoken as token
|
from base_unit import Token, TokenType
|
||||||
import tokenizer
|
|
||||||
import uuid
|
|
||||||
from collections import deque
|
from collections import deque
|
||||||
|
|
||||||
class Op:
|
class Op:
|
||||||
@@ -10,7 +8,7 @@ class Op:
|
|||||||
self.right = right
|
self.right = right
|
||||||
|
|
||||||
def __str__(self):
|
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:
|
class Num:
|
||||||
def __init__(self, token):
|
def __init__(self, token):
|
||||||
@@ -46,11 +44,11 @@ class Parser:
|
|||||||
new_list = []
|
new_list = []
|
||||||
|
|
||||||
for t in self.bak:
|
for t in self.bak:
|
||||||
if t.type == token.TokenType.variable:
|
if t.type == TokenType.variable:
|
||||||
if t.value in self.variables:
|
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:
|
else:
|
||||||
new_list.append(token.Token(t.value, t.type))
|
new_list.append(Token(t.value, t.type))
|
||||||
return new_list
|
return new_list
|
||||||
|
|
||||||
def advance_current_token(self):
|
def advance_current_token(self):
|
||||||
@@ -60,16 +58,16 @@ class Parser:
|
|||||||
self.current_token = self.tokens.popleft()
|
self.current_token = self.tokens.popleft()
|
||||||
|
|
||||||
def factor(self):
|
def factor(self):
|
||||||
if self.current_token.type == token.TokenType.constant:
|
if self.current_token.type == TokenType.constant:
|
||||||
node = Num(self.current_token)
|
node = Num(self.current_token)
|
||||||
self.advance_current_token()
|
self.advance_current_token()
|
||||||
return node
|
return node
|
||||||
elif self.current_token.type == token.TokenType.variable:
|
elif self.current_token.type == TokenType.variable:
|
||||||
node = Variable(self.current_token)
|
node = Variable(self.current_token)
|
||||||
self.variables[self.current_token.value] = node
|
self.variables[self.current_token.value] = node
|
||||||
self.advance_current_token()
|
self.advance_current_token()
|
||||||
return node
|
return node
|
||||||
elif self.current_token.type == token.TokenType.exp_start:
|
elif self.current_token.type == TokenType.exp_start:
|
||||||
self.advance_current_token() # (
|
self.advance_current_token() # (
|
||||||
node = self.topLevel()
|
node = self.topLevel()
|
||||||
self.advance_current_token() # )
|
self.advance_current_token() # )
|
||||||
@@ -78,7 +76,7 @@ class Parser:
|
|||||||
def lowLevel(self):
|
def lowLevel(self):
|
||||||
node = self.factor()
|
node = self.factor()
|
||||||
|
|
||||||
while self.current_token.type == token.TokenType.power:
|
while self.current_token.type == TokenType.power:
|
||||||
tmp = self.current_token
|
tmp = self.current_token
|
||||||
self.advance_current_token()
|
self.advance_current_token()
|
||||||
node = Op(left=node, op=tmp.type, right=self.factor())
|
node = Op(left=node, op=tmp.type, right=self.factor())
|
||||||
@@ -88,7 +86,7 @@ class Parser:
|
|||||||
def midLevel(self):
|
def midLevel(self):
|
||||||
node = self.lowLevel()
|
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
|
tmp = self.current_token
|
||||||
self.advance_current_token()
|
self.advance_current_token()
|
||||||
node = Op(left=node, op=tmp.type, right=self.lowLevel())
|
node = Op(left=node, op=tmp.type, right=self.lowLevel())
|
||||||
@@ -98,7 +96,7 @@ class Parser:
|
|||||||
def topLevel(self):
|
def topLevel(self):
|
||||||
node = self.midLevel()
|
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
|
tmp = self.current_token
|
||||||
self.advance_current_token()
|
self.advance_current_token()
|
||||||
node = Op(left=node, op=tmp.type, right=self.midLevel())
|
node = Op(left=node, op=tmp.type, right=self.midLevel())
|
||||||
+9
-9
@@ -1,13 +1,13 @@
|
|||||||
import ptoken
|
from base_unit import Token, TokenType
|
||||||
import parser
|
from ast_gen import Variable, Num
|
||||||
|
|
||||||
debug = False
|
debug = False
|
||||||
|
|
||||||
def calculate_results(node):
|
def calculate_results(node):
|
||||||
|
|
||||||
if type(node) is parser.Num:
|
if type(node) is Num:
|
||||||
return float(node.value)
|
return float(node.value)
|
||||||
elif type(node) is parser.Variable:
|
elif type(node) is Variable:
|
||||||
return node.value
|
return node.value
|
||||||
|
|
||||||
left = calculate_results(node.left)
|
left = calculate_results(node.left)
|
||||||
@@ -21,19 +21,19 @@ def operate(n1, n2, tokenType):
|
|||||||
n1 = float(n1)
|
n1 = float(n1)
|
||||||
n2 = float(n2)
|
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))
|
if debug: print("Adding %f and %f to get %f." %(n1, n2, n1 + n2))
|
||||||
return 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))
|
if debug: print("Subtracting %f and %f to get %f." %(n1, n2, n1 - n2))
|
||||||
return 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))
|
if debug: print("Multiplying %f and %f to get %f" %(n1, n2, n1 * n2))
|
||||||
return 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))
|
if debug: print("Dividing %f and %f to get %f" %(n1, n2, n1 / n2))
|
||||||
return 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))
|
if debug: print("Raising %f to the power of %f to get %f" %(n1, n2, n1**n2))
|
||||||
return n1 ** n2
|
return n1 ** n2
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import ptoken as token
|
from base_unit import Token, TokenType
|
||||||
import tokenizer
|
from calc_engine import calculate_results
|
||||||
import expression
|
from expression import Expression, Variable
|
||||||
import calc_engine
|
from tokenizer import get_tokens_from_expression_string, stringify_token_list
|
||||||
import factors
|
import factors
|
||||||
import jsonpickle
|
import jsonpickle
|
||||||
import itertools
|
import itertools
|
||||||
import copy
|
import copy
|
||||||
import parser
|
from ast_gen import Parser
|
||||||
|
|
||||||
base_list = [1, 2, 4, 6, 12]
|
base_list = [1, 2, 4, 6, 12]
|
||||||
tolerance = .05
|
tolerance = .05
|
||||||
@@ -151,8 +151,8 @@ json = jsonpickle.json.loads(open(fileLocation).read())
|
|||||||
with open("expression.json", "r") as f:
|
with open("expression.json", "r") as f:
|
||||||
#j = jsonpickle.json.load(f)
|
#j = jsonpickle.json.load(f)
|
||||||
exp = jsonpickle.decode(f.read())
|
exp = jsonpickle.decode(f.read())
|
||||||
tokens = tokenizer.get_tokens_from_expression_string(exp.expression)
|
tokens = get_tokens_from_expression_string(exp.expression)
|
||||||
tree = parser.Parser(tokens)
|
tree = Parser(tokens)
|
||||||
#results = calc_engine.calculate_results(tokens.copy())
|
#results = calc_engine.calculate_results(tokens.copy())
|
||||||
#print(results)
|
#print(results)
|
||||||
|
|
||||||
@@ -187,7 +187,7 @@ for r in res:
|
|||||||
count +=1
|
count +=1
|
||||||
t = tree.update_variables(vs)#tokenizer.replace_variables(tokens, list(r))
|
t = tree.update_variables(vs)#tokenizer.replace_variables(tokens, list(r))
|
||||||
#tokenizer.print_token_list(t)
|
#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 result <= int(exp.wrong_response * (1 + tolerance)) and result >= int(exp.wrong_response * (1 - tolerance)):
|
||||||
if not result in answers:
|
if not result in answers:
|
||||||
answers[result] = tree.get_tokens()
|
answers[result] = tree.get_tokens()
|
||||||
@@ -201,6 +201,6 @@ with open("results.txt", "w") as f:
|
|||||||
print("Answers Found: %d" %(len(answers)))
|
print("Answers Found: %d" %(len(answers)))
|
||||||
for k in answers:
|
for k in answers:
|
||||||
f.write("Result: %s for expression: " %(format(k, ".6f")))
|
f.write("Result: %s for expression: " %(format(k, ".6f")))
|
||||||
f.write(tokenizer.stringify_token_list(answers[k]))
|
f.write(stringify_token_list(answers[k]))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+29
-29
@@ -1,4 +1,4 @@
|
|||||||
import ptoken as token
|
from base_unit import Token, TokenType
|
||||||
from collections import deque
|
from collections import deque
|
||||||
|
|
||||||
def get_tokens_from_expression_string(expression_string):
|
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) + ".")
|
raise SyntaxError("Invalid grammar, to many periods in number. Found at position " + str (i) + ".")
|
||||||
if len(tokens) > 1:
|
if len(tokens) > 1:
|
||||||
lookBehind = tokens[-1]
|
lookBehind = tokens[-1]
|
||||||
if lookBehind.type == token.TokenType.exp_end:
|
if lookBehind.type == TokenType.exp_end:
|
||||||
tokens.append(token.Token("^", token.TokenType.power))
|
tokens.append(token.Token("^", TokenType.power))
|
||||||
if (i + 1) == len(expression_string):
|
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
|
parsing_number = True
|
||||||
tmp = tmp + c
|
tmp = tmp + c
|
||||||
continue
|
continue
|
||||||
|
|
||||||
elif c == '-' and not parsing_number:
|
elif c == '-' and not parsing_number:
|
||||||
if expression_string[i + 1] == '.' or expression_string[i + 1].isdigit():
|
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)
|
#print(tokens[-1].value)
|
||||||
#tokens.append(token.Token('+', token.TokenType.add))
|
#tokens.append(token.Token('+', token.TokenType.add))
|
||||||
parsing_number = True
|
parsing_number = True
|
||||||
tmp = "-"
|
tmp = "-"
|
||||||
elif tokens[-1].type == token.TokenType.exp_start:
|
elif tokens[-1].type == TokenType.exp_start:
|
||||||
parsing_number = True
|
parsing_number = True
|
||||||
tmp = "-"
|
tmp = "-"
|
||||||
elif tokens[-1].type == token.TokenType.exp_end:
|
elif tokens[-1].type == TokenType.exp_end:
|
||||||
tokens.append(token.Token(c, token.TokenType.get_operator(c)))
|
tokens.append(Token(c, TokenType.get_operator(c)))
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
elif expression_string[i + 1].isalpha():
|
elif expression_string[i + 1].isalpha():
|
||||||
# Must be a variable.
|
# Must be a variable.
|
||||||
if tokens[-1].type == token.TokenType.exp_start:
|
if tokens[-1].type == TokenType.exp_start:
|
||||||
variable_is_negative = True
|
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
|
variable_is_negative = True
|
||||||
elif tokens[-1].type == token.TokenType.exp_end:
|
elif tokens[-1].type == TokenType.exp_end:
|
||||||
tokens.append(token.Token(c, token.TokenType.get_operator(c)))
|
tokens.append(Token(c, TokenType.get_operator(c)))
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
else:
|
else:
|
||||||
tokens.append(token.Token(c, token.TokenType.get_operator(c)))
|
tokens.append(Token(c, TokenType.get_operator(c)))
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
|
|
||||||
elif c == '(':
|
elif c == '(':
|
||||||
if parsing_number:
|
if parsing_number:
|
||||||
tokens.append(token.Token(tmp, token.TokenType.constant))
|
tokens.append(token.Token(tmp, TokenType.constant))
|
||||||
tokens.append(token.Token("*", token.TokenType.multiply))
|
tokens.append(token.Token("*", TokenType.multiply))
|
||||||
tokens.append(token.Token("(", token.TokenType.exp_start))
|
tokens.append(Token("(", TokenType.exp_start))
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
continue
|
continue
|
||||||
elif c == ')':
|
elif c == ')':
|
||||||
if parsing_number:
|
if parsing_number:
|
||||||
tokens.append(token.Token(tmp, token.TokenType.constant))
|
tokens.append(token.Token(tmp, TokenType.constant))
|
||||||
tokens.append(token.Token(")", token.TokenType.exp_end))
|
tokens.append(Token(")", TokenType.exp_end))
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
continue
|
continue
|
||||||
elif token.TokenType.get_operator(c) != token.TokenType.unknown:
|
elif TokenType.get_operator(c) != TokenType.unknown:
|
||||||
if parsing_number:
|
if parsing_number:
|
||||||
tokens.append(token.Token(tmp, token.TokenType.constant))
|
tokens.append(Token(tmp, TokenType.constant))
|
||||||
tokens.append(token.Token(c, token.TokenType.get_operator(c)))
|
tokens.append(Token(c, TokenType.get_operator(c)))
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
continue
|
continue
|
||||||
elif c != ' ':
|
elif c != ' ':
|
||||||
if len(tokens) > 1:
|
if len(tokens) > 1:
|
||||||
lookBehind = tokens[-1]
|
lookBehind = tokens[-1]
|
||||||
if lookBehind.type == token.TokenType.exp_end:
|
if lookBehind.type == TokenType.exp_end:
|
||||||
tokens.append(token.Token("^", token.TokenType.power))
|
tokens.append(Token("^", TokenType.power))
|
||||||
|
|
||||||
if (i + 1) < len(expression_string):
|
if (i + 1) < len(expression_string):
|
||||||
lookAHead = expression_string[i + 1]
|
lookAHead = expression_string[i + 1]
|
||||||
if lookAHead == '(':
|
if lookAHead == '(':
|
||||||
tokens.append(token.Token(c, token.TokenType.variable))
|
tokens.append(Token(c, TokenType.variable))
|
||||||
tokens.append(token.Token("*", token.TokenType.multiply))
|
tokens.append(Token("*", TokenType.multiply))
|
||||||
tmp = ""
|
tmp = ""
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
continue
|
continue
|
||||||
if parsing_number:
|
if parsing_number:
|
||||||
tokens.append(token.Token(tmp, token.TokenType.constant))
|
tokens.append(Token(tmp, TokenType.constant))
|
||||||
tokens.append(token.Token("*", token.TokenType.multiply))
|
tokens.append(Token("*", TokenType.multiply))
|
||||||
# Check to see if the encountered variable, which is effectively a single character,
|
# 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
|
# 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'.
|
# 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:
|
else:
|
||||||
symbols_dic[c] = 1
|
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.
|
# 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
|
variable_is_negative = False
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
@@ -119,7 +119,7 @@ def peek_list(tokens):
|
|||||||
def print_token_list(tokens):
|
def print_token_list(tokens):
|
||||||
# Prints the list of Tokens to standard out.
|
# Prints the list of Tokens to standard out.
|
||||||
for tk in tokens:
|
for tk in tokens:
|
||||||
if tk.type == token.TokenType.variable:
|
if tk.type == TokenType.variable:
|
||||||
if tk.is_negative_variable:
|
if tk.is_negative_variable:
|
||||||
print("-%s " %(tk.value), end = "")
|
print("-%s " %(tk.value), end = "")
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user