summaryrefslogtreecommitdiff
path: root/tokenizer.py
diff options
context:
space:
mode:
Diffstat (limited to 'tokenizer.py')
-rw-r--r--tokenizer.py36
1 files changed, 16 insertions, 20 deletions
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))