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:
2020-07-01 19:28:09 -05:00
parent 212f3bfaf4
commit fcd0076891
3 changed files with 10 additions and 24 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ debug = False
def calculate_results(tokens):
# Main entry into the calc_engine.
# Returns the result of arthmetic as described by the token list.
return process_tokens(convert_to_deque(tokens), True)
return process_tokens(tokens, True)
def process_tokens(tokens, top_level = False):
pending_operations = []
+7 -6
View File
@@ -2,6 +2,7 @@ import ptoken as token
import tokenizer
import uuid
import calc_engine
from collections import deque
class Op:
def __init__(self, left, op, right):
@@ -22,14 +23,14 @@ class Num:
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.current_token = self.tokens.pop()
self.current_token = self.tokens.popleft()
def eat(self, token_type):
if self.current_token.type != token_type:
print("grammar")
if len(self.tokens) > 0:
self.current_token = self.tokens.pop()
self.current_token = self.tokens.popleft()
def factor(self):
node = self.current_token
@@ -140,7 +141,7 @@ def calc(node):
return operate(left, right, node.op)
debug = True
debug = False
def operate(n1, n2, tokenType):
# Utility function to handle dealing with the various mathmatical operations that the engine can process.
# Returns the result of any one of five mathmatical operations, else throws an error for unrecognized operations.
@@ -177,7 +178,7 @@ tokens = tokenizer.get_tokens_from_expression_string("5*2^(2+1)-(1+5)-2^5+5*(2/2
#tokens = tokenizer.get_tokens_from_expression_string("200((1-(1.08)^(-12))/0.08)")
#print(calc_engine.calculate_results(tokens))
#tokens = tokenizer.get_tokens_from_expression_string("1+6 / 2")
#n = Parser(tokens)
#s = n.parse()
n = Parser(tokens)
s = n.parse()
#Parser.p(s)
#print(calc(s))
print(calc(s))
+2 -17
View File
@@ -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.