summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew <saintruler@gmail.com>2021-07-11 22:05:57 +0400
committerAndrew <saintruler@gmail.com>2021-07-11 22:05:57 +0400
commitad804a6af1fa8fa19ade230d664a8aa3e3d4e0d2 (patch)
treef50f09d6916f258c4516e995c6efe0947d91e14f
parent9ce329611211d12d4ca6b98b18650194d1a1d0f7 (diff)
Added tests in makefile
-rw-r--r--Makefile17
-rw-r--r--tokenizer.py36
2 files changed, 33 insertions, 20 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..93f1a43
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+test: test1 test2 test3
+test1:
+ @echo "=== TEST 1 ==="
+ @printf "Expression is: %s\n" "(3 + 3 * 2) * 4 + 6 - 9 + -2 * (4 + 8 * 3) = -23"
+ @printf "Answer is: "
+ @python3 tokenizer.py "(3 + 3 * 2) * 4 + 6 - 9 + -2 * (4 + 8 * 3)"
+test2:
+ @echo "=== TEST 2 ==="
+ @printf "Expression is: %s\n" "3 + 3 * 2 + 6 - 9 + -2 * 4 + 8 * 3 = 22"
+ @printf "Answer is: "
+ @python3 tokenizer.py "3 + 3 * 2 + 6 - 9 + -2 * 4 + 8 * 3"
+test3:
+ @echo "=== TEST 3 ==="
+ @printf "Expression is: %s\n" "(2 + (3 * 5)) * 4 - 1 + 3 * (2 + 5 * (7 - 9 + (2 * 4 - 5 * (21 + 52)))) = -5312"
+ @printf "Answer is: "
+ @python3 tokenizer.py "(2 + (3 * 5)) * 4 - 1 + 3 * (2 + 5 * (7 - 9 + (2 * 4 - 5 * (21 + 52))))"
+
diff --git a/tokenizer.py b/tokenizer.py
index f8c2460..95af037 100644
--- a/tokenizer.py
+++ b/tokenizer.py
@@ -95,15 +95,15 @@ def tokenize(line):
tokens.append(Token(MULTIPLY, None))
state = STATE_NUMBER
else:
- print(1, tokens)
+ # print(1, tokens)
raise ValueError("Line is not a valid expression")
elif char in "-+0123456789":
val, i = parse_number(line, i)
tokens.append(Token(NUMBER, val))
state = STATE_OPERATOR
elif char != " ":
- print(char)
- print(2, tokens)
+ # print(char)
+ # print(2, tokens)
raise ValueError("Line is not a valid expression")
i += 1
@@ -111,11 +111,11 @@ def tokenize(line):
def parse_parenthesis(tokens, start):
- print(tokens)
+ # print(tokens)
i = start
end = start
depth = 0
- print(tokens[i])
+ # print(tokens[i])
while True:
token = tokens[i]
if token.type == RIGHT_PARENTHESIS:
@@ -126,10 +126,10 @@ def parse_parenthesis(tokens, start):
end = i + 1
break
i += 1
- print("=== BUILDING PARENTHESIS ===")
- print(tokens[start + 1 : end - 1])
+ # print("=== BUILDING PARENTHESIS ===")
+ # print(tokens[start + 1 : end - 1])
node = build_tree(tokens[start + 1 : end - 1])
- print("=== EXITED PARENTHESIS ===")
+ # print("=== EXITED PARENTHESIS ===")
return node, end - 1
@@ -146,11 +146,11 @@ def build_tree(tokens):
raise ValueError("Not a valid expression")
elif current_node.type in OPERATORS:
if current_node.left is None:
- print(f"Placed number {node} on the left of {current_node}")
+ # print(f"Placed number {node} on the left of {current_node}")
current_node.left = node
node.parent = current_node
elif current_node.right is None:
- print(f"Placed number {node} on the right of {current_node}")
+ # print(f"Placed number {node} on the right of {current_node}")
current_node.right = node
node.parent = current_node
current_node = node
@@ -159,15 +159,15 @@ def build_tree(tokens):
elif token.type == NUMBER:
node = Node(NUMBER, token.value)
if current_node is None:
- print(f"Number {node} is new root")
+ # print(f"Number {node} is new root")
current_node = node
elif current_node.type in OPERATORS:
if current_node.left is None:
- print(f"Placed number {node} on the left of {current_node}")
+ # print(f"Placed number {node} on the left of {current_node}")
current_node.left = node
node.parent = current_node
elif current_node.right is None:
- print(f"Placed number {node} on the right of {current_node}")
+ # print(f"Placed number {node} on the right of {current_node}")
current_node.right = node
node.parent = current_node
current_node = node
@@ -180,12 +180,12 @@ def build_tree(tokens):
elif current_node.type == NUMBER:
while current_node.parent is not None:
current_node = current_node.parent
- print(f"Operator {node} is new root with {current_node} on the left")
+ # print(f"Operator {node} is new root with {current_node} on the left")
current_node.parent = node
node.left = current_node
current_node = node
elif current_node.type == MULTIPLY:
- print(f"Placed {node} over {node.left}")
+ # print(f"Placed {node} over {node.left}")
current_node.parent = node
node.left = current_node
current_node = node
@@ -196,7 +196,7 @@ def build_tree(tokens):
if current_node is None:
raise ValueError("Not a valid expression")
elif current_node.type in [NUMBER, MULTIPLY]:
- print(f"Replaced with {node} node {current_node}")
+ # print(f"Replaced with {node} node {current_node}")
node.left = current_node
node.parent = current_node.parent
if node.parent is not None:
@@ -232,10 +232,6 @@ def main(*argv):
data = argv[0]
tokens = tokenize(data)
node = build_tree(tokens)
- # print(tokens)
- # print(node)
- # print(node.left)
- # print(node.right)
print(expr_eval(node))