Updated the tokenizer to use a deque object instead of a list. The old calc_engine should still work, not tested. New parser code works with this change.
This commit is contained in:
+2
-17
@@ -1,10 +1,11 @@
|
||||
import ptoken as token
|
||||
from collections import deque
|
||||
|
||||
def get_tokens_from_expression_string(expression_string):
|
||||
# Takes user input (in the format of a string) of an actuarial formula and parses the formula to its components.
|
||||
# Returns the list of Token objects that represent the string expression.
|
||||
|
||||
tokens = [] # List of objects of Token class
|
||||
tokens = deque()
|
||||
tmp = ""
|
||||
parsing_number = False
|
||||
symbols_dic = {} # Dictionary to keep track of the no. of times each variable appears.
|
||||
@@ -114,22 +115,6 @@ def peek_list(tokens):
|
||||
return tokens[-1]
|
||||
else:
|
||||
raise IndexError("The token list is empty.", tokens)
|
||||
|
||||
def replace_variables(tokens, new_constants):
|
||||
# Searches and replaces variable Tokens with Constant TokenType Token objects. Doing so enables the calc_engine to never have
|
||||
# to even know variable token types exist.
|
||||
# Returns a list of Token objects with the variables replaced by the supplied list of new constants.
|
||||
newTokenList = []
|
||||
for t in tokens:
|
||||
if t.type == token.TokenType.variable:
|
||||
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))
|
||||
|
||||
return newTokenList
|
||||
|
||||
def print_token_list(tokens):
|
||||
# Prints the list of Tokens to standard out.
|
||||
|
||||
Reference in New Issue
Block a user