Re-fixed an oversight in the tokenizer dealing with closed parentheses. Minor code clean up on the Calc Engine.

This commit is contained in:
2020-04-24 04:00:58 -05:00
parent 6a5de8b3e2
commit 00bd817a99
2 changed files with 11 additions and 24 deletions
+6 -24
View File
@@ -3,7 +3,7 @@ import tokenizer
import copy
from collections import deque
debug = True
debug = False
def calculate_results(tokens):
tokenizer.print_token_list(tokens)
@@ -12,7 +12,6 @@ def calculate_results(tokens):
def process_tokens(tokens, top_level = False):
pending_operations = []
running_total = 0
power = 0
p_ops_ran = False
while True:
@@ -34,8 +33,7 @@ def process_tokens(tokens, top_level = False):
if two_ahead.type == ptoken.TokenType.exp_start:
two_ahead = ptoken.Token(process_tokens(tokens), ptoken.TokenType.constant)
power = 5
running_total = operate(tmp, two_ahead.value, look_ahead.type)
return operate(tmp, two_ahead.value, look_ahead.type)
break
@@ -55,23 +53,7 @@ def process_tokens(tokens, top_level = False):
elif token.type == ptoken.TokenType.add or token.type == ptoken.TokenType.subtract:
if pending_operations and p_ops_ran:
tmp = convert_to_deque(pending_operations)
previous_token = None
while True:
if not tmp:
break
tmp_token = tmp.popleft()
if tmp_token.type == ptoken.TokenType.constant:
previous_token = tmp_token
elif tmp_token.type == ptoken.TokenType.add or tmp_token.type == ptoken.TokenType.subtract:
look_ahead = tmp.popleft()
running_total = operate(previous_token.value, look_ahead.value, tmp_token.type)
previous_token = ptoken.Token(running_total, ptoken.TokenType.constant)
previous_token = ptoken.Token(handle_pending(tmp), ptoken.TokenType.constant)
pending_operations = []
pending_operations.append(previous_token)
pending_operations.append(token)
@@ -104,10 +86,10 @@ def process_tokens(tokens, top_level = False):
pending_operations.append(ptoken.Token(running_total, ptoken.TokenType.constant))
p_ops_ran = True
tokenizer.print_token_list(pending_operations)
#tokenizer.print_token_list(pending_operations)
if pending_operations and power == 0:
if len(pending_operations) == 1 and power == 0:
if pending_operations:
if len(pending_operations) == 1:
return float(pending_operations.pop().value)
return handle_pending(convert_to_deque(pending_operations))