From 528098d9133e50eaacabda18a29565ad9c0c0fa6 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 8 Apr 2020 00:08:20 -0500 Subject: [PATCH] Updated exceptions to be more fitting, fixed a parse bug in the tokenizer and added the start of the calc engine. --- calc_engine.py | 25 +++++++++++++++++++++++++ main.py | 2 +- tokenizer.py | 18 ++---------------- 3 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 calc_engine.py diff --git a/calc_engine.py b/calc_engine.py new file mode 100644 index 0000000..8674efe --- /dev/null +++ b/calc_engine.py @@ -0,0 +1,25 @@ +import token + +def calculate_results(tokens): + + return 0 + +def peek_list(tokens): + if tokens: + return tokens[-1] + else: + raise IndexError("The token list is empty.", tokens) + +def operate(n1, n2, tokenType): + if tokenType == token.TokenType.add: + print("Adding %d and %d to get %d." %(n1, n2, n1 + n2)) + elif tokenType == token.TokenType.subtract: + print("Subtracting %d and %d to get %d." %(n1, n2, n1 - n2)) + elif tokenType == token.TokenType.multiply: + print("Multiplying %d and %d to get %d" %(n1, n2, n1 * n2)) + elif tokenType == token.TokenType.divide: + print("Dividing %d and %d to get %d" %(n1, n2, n1 / n2)) + elif tokenType == token.TokenType.power: + print("Raising %d to the power of %d to get %d" %(n1, n2, n1**n2)) + else: + raise TypeError("Invalid operator value " + str(tokenType) + ".", tokenType) diff --git a/main.py b/main.py index c40b03f..519a70c 100755 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import token import tokenizer #userInput = input('Enter your formula: ') -tokens = tokenizer.get_tokens_from_expression_string("1 + 2 / 3 (3 * 4) 5") +tokens = tokenizer.get_tokens_from_expression_string("i + 2 / i (3 * n) 5") for t in tokens: print("%s (%s) " %(t.value, token.TokenType.get_operator_name(t.type)), end = "") diff --git a/tokenizer.py b/tokenizer.py index 2ea9810..485f9a8 100644 --- a/tokenizer.py +++ b/tokenizer.py @@ -14,7 +14,7 @@ def get_tokens_from_expression_string(expression_string): c = expression_string[i] if c == '.' or c.isdigit(): if tmp.count('.') == 1: - raise ValueError("Invalid grammar, to many periods in number!") + raise SyntaxError("Invalid grammar, to many periods in number. Found at position " + str (i) + ".") if len(tokens) > 1: lookBehind = tokens[-1] if lookBehind.type == token.TokenType.exp_end: @@ -49,7 +49,7 @@ def get_tokens_from_expression_string(expression_string): elif c != ' ': if len(tokens) > 1: lookBehind = tokens[-1] - if lookBehind.type == TokenType.exp_end: + if lookBehind.type == token.TokenType.exp_end: tokens.append(token.CToken("^", token.TokenType.power)) if (i + 1) < len(expression_string): lookAHead = expression_string[i + 1] @@ -72,17 +72,3 @@ def get_tokens_from_expression_string(expression_string): return tokens -def operate(n1, n2, tokenType): - if tokenType == token.TokenType.add: - print("Adding %d and %d to get %d." %(n1, n2, n1 + n2)) - elif tokenType == token.TokenType.subtract: - print("Subtracting %d and %d to get %d." %(n1, n2, n1 - n2)) - elif tokenType == token.TokenType.multiply: - print("Multiplying %d and %d to get %d" %(n1, n2, n1 * n2)) - elif tokenType == token.TokenType.divide: - print("Dividing %d and %d to get %d" %(n1, n2, n1 / n2)) - elif tokenType == token.TokenType.power: - print("Raising %d to the power of %d to get %d" %(n1, n2, n1**n2)) - else: - raise ValueError("Invalid operator value.") -