Changed 'assoc' to 'prec', because it's precedence.

parent a34e4894
...@@ -36,7 +36,8 @@ def generate_line(root): ...@@ -36,7 +36,8 @@ def generate_line(root):
('*', '/', 'mod'), ('*', '/', 'mod'),
('^', ) ('^', )
] ]
max_assoc = len(operators)
max_pred = len(operators)
def is_operator(node): def is_operator(node):
""" """
...@@ -47,9 +48,9 @@ def generate_line(root): ...@@ -47,9 +48,9 @@ def generate_line(root):
return reduce(either, map(lambda x: label in x, operators)) return reduce(either, map(lambda x: label in x, operators))
def assoc(node): def pred(node):
""" """
Get the associativity of an operator node. Get the precedence of an operator node.
""" """
# Check binary and n-ary operators # Check binary and n-ary operators
if not isinstance(node, Leaf) and len(node) > 1: if not isinstance(node, Leaf) and len(node) > 1:
...@@ -60,7 +61,7 @@ def generate_line(root): ...@@ -60,7 +61,7 @@ def generate_line(root):
return i return i
# Unary operator and leaves have highest precedence # Unary operator and leaves have highest precedence
return max_assoc return max_pred
def traverse(node): def traverse(node):
""" """
...@@ -84,7 +85,7 @@ def generate_line(root): ...@@ -84,7 +85,7 @@ def generate_line(root):
s += traverse(node[0]) s += traverse(node[0])
else: else:
# N-ary operator # N-ary operator
node_assoc = assoc(node) node_pred = pred(node)
e = [] e = []
for child in node: for child in node:
...@@ -92,7 +93,7 @@ def generate_line(root): ...@@ -92,7 +93,7 @@ def generate_line(root):
# Check if there is an precedence conflict. # Check if there is an precedence conflict.
# If so, add parentheses # If so, add parentheses
if assoc(child) < node_assoc: if pred(child) < node_pred:
exp = '(' + exp + ')' exp = '(' + exp + ')'
e.append(exp) e.append(exp)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment