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