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:
+29
-29
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user