Commit 70714ec0 authored by Taddeus Kroes's avatar Taddeus Kroes

Added logical operators.

Conflicts:

	line.py
parent d3feae46
from traverse import traverse_depth_first from traverse import traverse_depth_first
OPERATORS = [ OPERATORS = (
('+', '-'), ('vv', ),
('*', '/', 'mod'), ('^^', ),
('^', '_') ('=', ),
] ('+', '-'),
('*', '/', 'mod'),
('^', '_'),
)
NEG_PRED = 3
MAX_PRED = len(OPERATORS) MAX_PRED = len(OPERATORS)
...@@ -110,7 +114,7 @@ def generate_line(root): ...@@ -110,7 +114,7 @@ def generate_line(root):
# -3 * 4 # -3 * 4
# --a # --a
if value.is_leaf \ if value.is_leaf \
or not ' ' in content[value] or pred(value) > 0: or not ' ' in content[value] or pred(value) > NEG_PRED:
return op + content[value] return op + content[value]
# -(a + b) # -(a + b)
......
...@@ -25,6 +25,13 @@ class TestLine(unittest.TestCase): ...@@ -25,6 +25,13 @@ class TestLine(unittest.TestCase):
times = N('*', plus, plus) times = N('*', plus, plus)
self.assertEquals(generate_line(times), '(1 + 2)(1 + 2)') self.assertEquals(generate_line(times), '(1 + 2)(1 + 2)')
def test_parentheses_no_equal_precedence(self):
a, b, c = L('a'), L('b'), L('c')
line = N('vv', a, N('^^', b, c))
self.assertEquals(generate_line(line), 'a vv b ^^ c')
line = N('^^', a, N('vv', b, c))
self.assertEquals(generate_line(line), 'a ^^ (b vv c)')
def test_parentheses_equal_precedence_right(self): def test_parentheses_equal_precedence_right(self):
l0, l1, l2, l3 = L(1), L(2), L(3), L(4) l0, l1, l2, l3 = L(1), L(2), L(3), L(4)
plus = N('+', l1, l2) plus = N('+', l1, l2)
......
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