From fcd0076891b70f1d0900726dce69961af253199b Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 1 Jul 2020 19:28:09 -0500 Subject: [PATCH] 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. --- calc_engine.py | 2 +- parser.py | 13 +++++++------ tokenizer.py | 19 ++----------------- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/calc_engine.py b/calc_engine.py index fe3af66..4db0f4b 100644 --- a/calc_engine.py +++ b/calc_engine.py @@ -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 = [] diff --git a/parser.py b/parser.py index 900c5cd..84c4615 100644 --- a/parser.py +++ b/parser.py @@ -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)) diff --git a/tokenizer.py b/tokenizer.py index 61b6ea6..1f5047a 100644 --- a/tokenizer.py +++ b/tokenizer.py @@ -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.