Commit bc8b6611 authored by Taddeüs Kroes's avatar Taddeüs Kroes

The use of brackets is now equal to that of parentheses.

parent 8fbf2e15
......@@ -110,6 +110,7 @@ class Parser(BisonParser):
('left', ('MINUS', 'PLUS', 'NEG')),
('left', ('INTEGRAL', 'DERIVATIVE')),
('left', ('TIMES', )),
('left', ('PRIME', )),
('left', ('DIVIDE', )),
('right', ('FUNCTION', )),
('right', ('POW', )),
......@@ -466,6 +467,7 @@ class Parser(BisonParser):
exp : NUMBER
| IDENTIFIER
| LPAREN exp RPAREN
| LBRACKET exp RBRACKET
| unary
| binary
| nary
......@@ -480,10 +482,10 @@ class Parser(BisonParser):
if option == 1: # rule: IDENTIFIER
return Leaf(values[0])
if option == 2: # rule: LPAREN exp RPAREN
if option in (2, 3): # rule: LPAREN exp RPAREN | LBRACKET exp RBRACKET
return values[1]
if 3 <= option <= 5: # rule: unary | binary | nary
if 4 <= option <= 6: # rule: unary | binary | nary
return values[0]
raise BisonSyntaxError('Unsupported option %d in target "%s".'
......@@ -495,7 +497,7 @@ class Parser(BisonParser):
| FUNCTION_LPAREN exp RPAREN
| FUNCTION exp
| DERIVATIVE exp
| bracket_derivative
| exp PRIME
| INTEGRAL exp
| integral_bounds TIMES exp %prec INTEGRAL
| LBRACKET exp RBRACKET lbnd ubnd
......@@ -535,8 +537,8 @@ class Parser(BisonParser):
# DERIVATIVE looks like 'd/d*x*' -> extract the 'x'
return Node(OP_DER, values[1], Leaf(values[0][-2]))
if option == 4: # rule: bracket_derivative
return values[0]
if option == 4: # rule: exp PRIME
return Node(OP_DER, values[0])
if option == 5: # rule: INTEGRAL exp
fx, x = find_integration_variable(values[1])
......@@ -599,21 +601,6 @@ class Parser(BisonParser):
raise BisonSyntaxError('Unsupported option %d in target "%s".'
% (option, target)) # pragma: nocover
def on_bracket_derivative(self, target, option, names, values):
"""
bracket_derivative : LBRACKET exp RBRACKET PRIME
| bracket_derivative PRIME
"""
if option == 0: # rule: LBRACKET exp RBRACKET PRIME
return Node(OP_DER, values[1])
if option == 1: # rule: bracket_derivative PRIME
return Node(OP_DER, values[0])
raise BisonSyntaxError('Unsupported option %d in target "%s".'
% (option, target)) # pragma: nocover
def on_binary(self, target, option, names, values):
"""
binary : exp PLUS exp
......
......@@ -72,8 +72,10 @@ class TestParser(RulesTestCase):
self.assertEqual(tree('2(a + b)'), tree('2 * (a + b)'))
self.assertEqual(tree('(a + b)2'), tree('(a + b) * 2'))
self.assertEqual(tree('(a)(b)'), tree('(a) * (b)'))
self.assertEqual(tree('(a)[b]\''), tree('(a) * [b]\''))
self.assertEqual(tree('(a)(b)'), tree('a * b'))
self.assertEqual(tree('(a)[b]'), tree('a * b'))
self.assertEqual(tree('[a](b)'), tree('a * b'))
self.assertEqual(tree('[a][b]'), tree('a * b'))
# FIXME: self.assertEqual(tree('(a)|b|'), tree('(a) * |b|'))
# FIXME: self.assertEqual(tree('|a|(b)'), tree('|a| * (b)'))
......@@ -94,11 +96,20 @@ class TestParser(RulesTestCase):
self.assertEqual(tree('sin cos x ^ 2'), sin(cos(x ** 2)))
self.assertEqual(tree('sin cos(x) ^ 2'), sin(cos(x) ** 2))
def test_bracket_derivative(self):
def test_brackets(self):
self.assertEqual(*tree('[x], x'))
self.assertEqual(*tree('[x], (x)'))
self.assertEqual(*tree('[x ^ 2], x ^ 2'))
self.assertEqual(*tree('[x ^ 2](x), x ^ 2 * x'))
def test_derivative(self):
x = tree('x')
self.assertEqual(tree('[x]\''), der(x))
self.assertEqual(tree('x\''), der(x))
self.assertEqual(tree('[x]\'\''), der(der(x)))
self.assertEqual(tree('(x)\'\''), der(der(x)))
self.assertEqual(tree('x\'\''), der(der(x)))
def test_delta_derivative(self):
exp, x, d = tree('x ^ 2, x, d')
......
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