Re-fixed an oversight in the tokenizer dealing with closed parentheses. Minor code clean up on the Calc Engine.
This commit is contained in:
+6
-24
@@ -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))
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ def get_tokens_from_expression_string(expression_string):
|
||||
|
||||
elif c == '-' and not parsing_number:
|
||||
if expression_string[i + 1] == '.' or expression_string[i + 1].isdigit():
|
||||
print(tokens[-1].value)
|
||||
if token.TokenType.get_operator(tokens[-1].value) != token.TokenType.unknown:#tokens[-1].type == token.TokenType
|
||||
#print(tokens[-1].value)
|
||||
#tokens.append(token.Token('+', token.TokenType.add))
|
||||
@@ -35,6 +36,10 @@ def get_tokens_from_expression_string(expression_string):
|
||||
elif tokens[-1].type == token.TokenType.exp_start:
|
||||
parsing_number = True
|
||||
tmp = "-"
|
||||
elif tokens[-1].type == token.TokenType.exp_end:
|
||||
tokens.append(token.Token(c, token.TokenType.get_operator(c)))
|
||||
parsing_number = False
|
||||
tmp = ""
|
||||
else:
|
||||
tokens.append(token.Token(c, token.TokenType.get_operator(c)))
|
||||
parsing_number = False
|
||||
|
||||
Reference in New Issue
Block a user