Added comments in the code to provide a little bit more insight into what's happening.

This commit is contained in:
2020-04-27 11:01:27 -05:00
parent 5a585cf6f8
commit 6ba3414367
3 changed files with 35 additions and 9 deletions
+14 -5
View File
@@ -1,11 +1,10 @@
import ptoken as token
def get_tokens_from_expression_string(expression_string):
#Takes user input of an actuarial formula and parses the formula to id its components.
#:return: tokens (List of objects of CToken class).
# 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 CToken class
tokens = [] # List of objects of Token class
tmp = ""
parsing_number = False
symbols_dic = {} # Dictionary to keep track of the no. of times each variable appears.
@@ -41,7 +40,7 @@ def get_tokens_from_expression_string(expression_string):
parsing_number = False
tmp = ""
elif expression_string[i + 1].isalpha():
#Must be a variable.
# Must be a variable.
if tokens[-1].type == token.TokenType.exp_start:
variable_is_negative = True
elif token.TokenType.get_operator(tokens[-1].value) != token.TokenType.unknown:
@@ -94,10 +93,14 @@ def get_tokens_from_expression_string(expression_string):
if parsing_number:
tokens.append(token.Token(tmp, token.TokenType.constant))
tokens.append(token.Token("*", 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'.
if c in symbols_dic.keys():
symbols_dic[c] += 1
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))
variable_is_negative = False
parsing_number = False
@@ -106,12 +109,16 @@ def get_tokens_from_expression_string(expression_string):
return tokens
def peek_list(tokens):
# Returns the last Token object in the list, else throws an error.
if 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:
@@ -125,6 +132,7 @@ def replace_variables(tokens, new_constants):
return newTokenList
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.is_negative_variable:
@@ -136,6 +144,7 @@ def print_token_list(tokens):
print()
def stringify_token_list(tokens):
# Returns a string that represents the list of Tokens.
text = ''
for tk in tokens:
text += str(tk.value)