Added support for negative variables in the tokenizer and ptoken object.
This commit is contained in:
@@ -6,7 +6,6 @@ from collections import deque
|
||||
debug = False
|
||||
|
||||
def calculate_results(tokens):
|
||||
tokenizer.print_token_list(tokens)
|
||||
return process_tokens(convert_to_deque(tokens), True)
|
||||
|
||||
def process_tokens(tokens, top_level = False):
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
class Token:
|
||||
def __init__(self, value, type):
|
||||
def __init__(self, value, type, is_negative_variable = False):
|
||||
self.value = value
|
||||
self.type = type
|
||||
self.is_negative_variable = is_negative_variable
|
||||
|
||||
class TokenType:
|
||||
add = 0
|
||||
|
||||
+18
-6
@@ -9,6 +9,7 @@ def get_tokens_from_expression_string(expression_string):
|
||||
tmp = ""
|
||||
parsing_number = False
|
||||
symbols_dic = {} # Dictionary to keep track of the no. of times each variable appears.
|
||||
variable_is_negative = False
|
||||
|
||||
for i in range(0, len(expression_string)):
|
||||
c = expression_string[i]
|
||||
@@ -26,8 +27,7 @@ def get_tokens_from_expression_string(expression_string):
|
||||
continue
|
||||
|
||||
elif c == '-' and not parsing_number:
|
||||
if expression_string[i + 1] == '.' or expression_string[i + 1].isdigit():
|
||||
print(tokens[-1].value)
|
||||
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
|
||||
#print(tokens[-1].value)
|
||||
#tokens.append(token.Token('+', token.TokenType.add))
|
||||
@@ -40,6 +40,10 @@ def get_tokens_from_expression_string(expression_string):
|
||||
tokens.append(token.Token(c, token.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:
|
||||
variable_is_negative = True
|
||||
else:
|
||||
tokens.append(token.Token(c, token.TokenType.get_operator(c)))
|
||||
parsing_number = False
|
||||
@@ -71,7 +75,7 @@ def get_tokens_from_expression_string(expression_string):
|
||||
if len(tokens) > 1:
|
||||
lookBehind = tokens[-1]
|
||||
if lookBehind.type == token.TokenType.exp_end:
|
||||
tokens.append(token.Token("^", token.TokenType.power))
|
||||
tokens.append(token.Token("^", token.TokenType.power))
|
||||
|
||||
if (i + 1) < len(expression_string):
|
||||
lookAHead = expression_string[i + 1]
|
||||
@@ -88,7 +92,8 @@ def get_tokens_from_expression_string(expression_string):
|
||||
symbols_dic[c] += 1
|
||||
else:
|
||||
symbols_dic[c] = 1
|
||||
tokens.append(token.Token(c + str(symbols_dic[c]), token.TokenType.variable))
|
||||
tokens.append(token.Token(c + str(symbols_dic[c]), token.TokenType.variable, variable_is_negative))
|
||||
variable_is_negative = False
|
||||
parsing_number = False
|
||||
tmp = ""
|
||||
|
||||
@@ -104,7 +109,10 @@ def replace_variables(tokens, new_constants):
|
||||
newTokenList = []
|
||||
for t in tokens:
|
||||
if t.type == token.TokenType.variable:
|
||||
newTokenList.append(token.Token(new_constants.pop(0), token.TokenType.constant))
|
||||
if not t.is_negative_variable:
|
||||
newTokenList.append(token.Token(new_constants.pop(0), token.TokenType.constant))
|
||||
else:
|
||||
newTokenList.append(token.Token(new_constants.pop(0) * -1, token.TokenType.constant))
|
||||
else:
|
||||
newTokenList.append(token.Token(t.value, t.type))
|
||||
|
||||
@@ -112,7 +120,11 @@ def replace_variables(tokens, new_constants):
|
||||
|
||||
def print_token_list(tokens):
|
||||
for tk in tokens:
|
||||
print("%s " %(tk.value), end = "")#, token.TokenType.get_token_type_name(tk.type)), end = "")
|
||||
if tk.type == token.TokenType.variable:
|
||||
if tk.is_negative_variable:
|
||||
print("-%s " %(tk.value), end = "")
|
||||
else:
|
||||
print("%s " %(tk.value), end = "")
|
||||
print()
|
||||
|
||||
def stringify_token_list(tokens):
|
||||
|
||||
Reference in New Issue
Block a user