Commit 00abd696 authored by Sander Mathijs van Veen's avatar Sander Mathijs van Veen

Merge branch 'master' of kompiler.org:python/graph_drawing

parents d851ef2b 608dc099
def generate_line(root, node_type): from node import Node
def generate_line(root):
""" """
Print an expression tree in a single text line. Where needed, add Print an expression tree in a single text line. Where needed, add
parentheses. parentheses.
>>> from node import Node, Leaf >>> from node import Leaf
>>> from graph import generate_graph
>>> l0, l1 = Leaf(1), Leaf(2) >>> l0, l1 = Leaf(1), Leaf(2)
>>> plus = Node('+', l0, l1) >>> plus = Node('+', l0, l1)
>>> print generate_line(plus, Node) >>> print generate_line(plus)
1 + 2 1 + 2
>>> plus2 = Node('+', l0, l1) >>> plus2 = Node('+', l0, l1)
>>> times = Node('*', plus, plus2) >>> times = Node('*', plus, plus2)
>>> print generate_line(times, Node) >>> print generate_line(times)
(1 + 2) * (1 + 2) (1 + 2) * (1 + 2)
>>> l2 = Leaf(3) >>> l2 = Leaf(3)
>>> uminus = Node('-', l2) >>> uminus = Node('-', l2)
>>> times = Node('*', plus, uminus) >>> times = Node('*', plus, uminus)
>>> print generate_line(times, Node) >>> print generate_line(times)
(1 + 2) * -3 (1 + 2) * -3
>>> exp = Leaf('x') >>> exp = Leaf('x')
>>> inf = Leaf('oo') >>> inf = Leaf('oo')
>>> minus_inf = Node('-', inf) >>> minus_inf = Node('-', inf)
>>> integral = Node('int', exp, minus_inf, inf) >>> integral = Node('int', exp, minus_inf, inf)
>>> print generate_line(integral, Node) >>> print generate_line(integral)
int(x, -oo, oo) int(x, -oo, oo)
""" """
...@@ -52,7 +53,7 @@ def generate_line(root, node_type): ...@@ -52,7 +53,7 @@ def generate_line(root, node_type):
""" """
Get the associativity of an operator node. Get the associativity of an operator node.
""" """
if isinstance(node, node_type) and len(node) > 1: if isinstance(node, Node) and len(node) > 1:
op = node.title() op = node.title()
for i, group in enumerate(operators): for i, group in enumerate(operators):
...@@ -69,7 +70,7 @@ def generate_line(root, node_type): ...@@ -69,7 +70,7 @@ def generate_line(root, node_type):
""" """
s = node.title() s = node.title()
if not isinstance(node, node_type): if not isinstance(node, Node):
return s return s
arity = len(node) arity = len(node)
......
...@@ -15,22 +15,22 @@ class TestLine(unittest.TestCase): ...@@ -15,22 +15,22 @@ class TestLine(unittest.TestCase):
def test_simple(self): def test_simple(self):
l0, l1 = Leaf(1), Leaf(2) l0, l1 = Leaf(1), Leaf(2)
plus = Node('+', l0, l1) plus = Node('+', l0, l1)
assert generate_line(plus, Node) == '1 + 2' assert generate_line(plus) == '1 + 2'
def test_parentheses(self): def test_parentheses(self):
l0, l1 = Leaf(1), Leaf(2) l0, l1 = Leaf(1), Leaf(2)
plus = Node('+', l0, l1) plus = Node('+', l0, l1)
times = Node('*', plus, plus) times = Node('*', plus, plus)
assert generate_line(times, Node) == '(1 + 2) * (1 + 2)' assert generate_line(times) == '(1 + 2) * (1 + 2)'
def test_function(self): def test_function(self):
exp = Leaf('x') exp = Leaf('x')
inf = Leaf('oo') inf = Leaf('oo')
minus_inf = Node('-', inf) minus_inf = Node('-', inf)
integral = Node('int', exp, minus_inf, inf) integral = Node('int', exp, minus_inf, inf)
assert generate_line(integral, Node) == 'int(x, -oo, oo)' assert generate_line(integral) == 'int(x, -oo, oo)'
def test_mod(self): def test_mod(self):
l0, l1 = Leaf(1), Leaf(2) l0, l1 = Leaf(1), Leaf(2)
mod = Node('mod', l1, l0) mod = Node('mod', l1, l0)
assert generate_line(mod, Node) == '2 mod 1' assert generate_line(mod) == '2 mod 1'
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