Changed the token.py to ptoken.py (for Placeholder Token) to avoid naming conflicts with jsonpickle.
This commit is contained in:
+5
-5
@@ -1,4 +1,4 @@
|
|||||||
import token
|
import ptoken as token
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
def calculate_results(tokens):
|
def calculate_results(tokens):
|
||||||
@@ -8,7 +8,7 @@ def calculate_results(tokens):
|
|||||||
|
|
||||||
def handle_parenthesis(tokens, call_has_priority = False):
|
def handle_parenthesis(tokens, call_has_priority = False):
|
||||||
running_value = 0
|
running_value = 0
|
||||||
previous_token = token.CToken("", token.TokenType.unknown)
|
previous_token = token.Token("", token.TokenType.unknown)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if len(tokens) == 0:
|
if len(tokens) == 0:
|
||||||
@@ -40,7 +40,7 @@ def handle_parenthesis(tokens, call_has_priority = False):
|
|||||||
if not previous_token.type == token.TokenType.unknown:
|
if not previous_token.type == token.TokenType.unknown:
|
||||||
tokens.append(previous_token)
|
tokens.append(previous_token)
|
||||||
else:
|
else:
|
||||||
tokens.append(token.CToken(running_value, token.TokenType.constant))
|
tokens.append(token.Token(running_value, token.TokenType.constant))
|
||||||
|
|
||||||
running_value = handle_add_and_subtract(tokens)
|
running_value = handle_add_and_subtract(tokens)
|
||||||
#Set the previous_token to an invalid state so its not used by mistake.
|
#Set the previous_token to an invalid state so its not used by mistake.
|
||||||
@@ -103,7 +103,7 @@ def handle_parenthesis(tokens, call_has_priority = False):
|
|||||||
|
|
||||||
def handle_multiply_and_division(tokens):
|
def handle_multiply_and_division(tokens):
|
||||||
running_value = 0
|
running_value = 0
|
||||||
previous_token = token.CToken("", token.TokenType.unknown)
|
previous_token = token.Token("", token.TokenType.unknown)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if len(tokens) == 0:
|
if len(tokens) == 0:
|
||||||
@@ -156,7 +156,7 @@ def handle_add_and_subtract(tokens):
|
|||||||
# need to change for the life of this project.
|
# need to change for the life of this project.
|
||||||
#
|
#
|
||||||
running_value = 0
|
running_value = 0
|
||||||
previous_token = token.CToken("", token.TokenType.unknown)
|
previous_token = token.Token("", token.TokenType.unknown)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
class CToken:
|
class Token:
|
||||||
def __init__(self, value, type):
|
def __init__(self, value, type):
|
||||||
self.value = value
|
self.value = value
|
||||||
self.type = type
|
self.type = type
|
||||||
+18
-18
@@ -1,4 +1,4 @@
|
|||||||
import token
|
import ptoken as token
|
||||||
|
|
||||||
def get_tokens_from_expression_string(expression_string):
|
def get_tokens_from_expression_string(expression_string):
|
||||||
|
|
||||||
@@ -18,31 +18,31 @@ def get_tokens_from_expression_string(expression_string):
|
|||||||
if len(tokens) > 1:
|
if len(tokens) > 1:
|
||||||
lookBehind = tokens[-1]
|
lookBehind = tokens[-1]
|
||||||
if lookBehind.type == token.TokenType.exp_end:
|
if lookBehind.type == token.TokenType.exp_end:
|
||||||
tokens.append(token.CToken("^", token.TokenType.power))
|
tokens.append(token.Token("^", token.TokenType.power))
|
||||||
if (i + 1) == len(expression_string):
|
if (i + 1) == len(expression_string):
|
||||||
tokens.append(token.CToken(tmp + c, token.TokenType.constant))
|
tokens.append(token.Token(tmp + c, token.TokenType.constant))
|
||||||
parsing_number = True
|
parsing_number = True
|
||||||
tmp = tmp + c
|
tmp = tmp + c
|
||||||
continue
|
continue
|
||||||
elif c == '(':
|
elif c == '(':
|
||||||
if parsing_number:
|
if parsing_number:
|
||||||
tokens.append(token.CToken(tmp, token.TokenType.constant))
|
tokens.append(token.Token(tmp, token.TokenType.constant))
|
||||||
tokens.append(token.CToken("*", token.TokenType.multiply))
|
tokens.append(token.Token("*", token.TokenType.multiply))
|
||||||
tokens.append(token.CToken("(", token.TokenType.exp_start))
|
tokens.append(token.Token("(", 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.CToken(tmp, token.TokenType.constant))
|
tokens.append(token.Token(tmp, token.TokenType.constant))
|
||||||
tokens.append(token.CToken(")", token.TokenType.exp_end))
|
tokens.append(token.Token(")", token.TokenType.exp_end))
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
continue
|
continue
|
||||||
elif token.TokenType.get_operator(c) != token.TokenType.unknown:
|
elif token.TokenType.get_operator(c) != token.TokenType.unknown:
|
||||||
if parsing_number:
|
if parsing_number:
|
||||||
tokens.append(token.CToken(tmp, token.TokenType.constant))
|
tokens.append(token.Token(tmp, token.TokenType.constant))
|
||||||
tokens.append(token.CToken(c, token.TokenType.get_operator(c)))
|
tokens.append(token.Token(c, token.TokenType.get_operator(c)))
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
continue
|
continue
|
||||||
@@ -50,23 +50,23 @@ def get_tokens_from_expression_string(expression_string):
|
|||||||
if len(tokens) > 1:
|
if len(tokens) > 1:
|
||||||
lookBehind = tokens[-1]
|
lookBehind = tokens[-1]
|
||||||
if lookBehind.type == token.TokenType.exp_end:
|
if lookBehind.type == token.TokenType.exp_end:
|
||||||
tokens.append(token.CToken("^", token.TokenType.power))
|
tokens.append(token.Token("^", 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.CToken(c, token.TokenType.variable))
|
tokens.append(token.Token(c, token.TokenType.variable))
|
||||||
tokens.append(token.CToken("*", token.TokenType.multiply))
|
tokens.append(token.Token("*", token.TokenType.multiply))
|
||||||
tmp = ""
|
tmp = ""
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
continue
|
continue
|
||||||
if parsing_number:
|
if parsing_number:
|
||||||
tokens.append(token.CToken(tmp, token.TokenType.constant))
|
tokens.append(token.Token(tmp, token.TokenType.constant))
|
||||||
tokens.append(token.CToken("*", token.TokenType.multiply))
|
tokens.append(token.Token("*", token.TokenType.multiply))
|
||||||
if c in symbols_dic.keys():
|
if c in symbols_dic.keys():
|
||||||
symbols_dic[c] += 1
|
symbols_dic[c] += 1
|
||||||
else:
|
else:
|
||||||
symbols_dic[c] = 1
|
symbols_dic[c] = 1
|
||||||
tokens.append(token.CToken(c + str(symbols_dic[c]), token.TokenType.variable))
|
tokens.append(token.Token(c + str(symbols_dic[c]), token.TokenType.variable))
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
|
|
||||||
@@ -77,8 +77,8 @@ def replace_variables(tokens, new_constants):
|
|||||||
newTokenList = []
|
newTokenList = []
|
||||||
for t in tokens:
|
for t in tokens:
|
||||||
if t.type == token.TokenType.variable:
|
if t.type == token.TokenType.variable:
|
||||||
newTokenList.append(token.CToken(new_constants.pop(0), token.TokenType.constant))
|
newTokenList.append(token.Token(new_constants.pop(0), token.TokenType.constant))
|
||||||
else:
|
else:
|
||||||
newTokenList.append(token.CToken(t.value, t.type))
|
newTokenList.append(token.Token(t.value, t.type))
|
||||||
|
|
||||||
return newTokenList
|
return newTokenList
|
||||||
|
|||||||
Reference in New Issue
Block a user