Commit 0551ec1b authored by Sander Mathijs van Veen's avatar Sander Mathijs van Veen

Merge branch 'master' of kompiler.org:trs

parents 94269a3f 87d0f49c
...@@ -6,7 +6,8 @@ from .powers import match_add_exponents, match_subtract_exponents, \ ...@@ -6,7 +6,8 @@ from .powers import match_add_exponents, match_subtract_exponents, \
match_multiply_exponents, match_duplicate_exponent, \ match_multiply_exponents, match_duplicate_exponent, \
match_remove_negative_exponent, match_exponent_to_root, \ match_remove_negative_exponent, match_exponent_to_root, \
match_extend_exponent match_extend_exponent
from .numerics import match_divide_numerics, match_multiply_numerics from .numerics import match_divide_numerics, match_multiply_numerics, \
match_multiply_zero
from .fractions import match_constant_division, match_add_constant_fractions, \ from .fractions import match_constant_division, match_add_constant_fractions, \
match_expand_and_add_fractions match_expand_and_add_fractions
from .negation import match_negate_group, match_negated_division from .negation import match_negate_group, match_negated_division
...@@ -15,7 +16,7 @@ RULES = { ...@@ -15,7 +16,7 @@ RULES = {
OP_ADD: [match_add_constant_fractions, match_combine_polynomes, \ OP_ADD: [match_add_constant_fractions, match_combine_polynomes, \
match_combine_groups], match_combine_groups],
OP_MUL: [match_multiply_numerics, match_expand, match_add_exponents, \ OP_MUL: [match_multiply_numerics, match_expand, match_add_exponents, \
match_expand_and_add_fractions], match_expand_and_add_fractions, match_multiply_zero],
OP_DIV: [match_subtract_exponents, match_divide_numerics, \ OP_DIV: [match_subtract_exponents, match_divide_numerics, \
match_constant_division, match_negated_division], match_constant_division, match_negated_division],
OP_POW: [match_multiply_exponents, match_duplicate_exponent, \ OP_POW: [match_multiply_exponents, match_duplicate_exponent, \
......
...@@ -106,6 +106,52 @@ def divide_numerics(root, args): ...@@ -106,6 +106,52 @@ def divide_numerics(root, args):
return Leaf(n / d) return Leaf(n / d)
def match_multiply_zero(node):
"""
a * 0 -> 0
0 * a -> 0
-0 * a -> -0
0 * -a -> -0
-0 * -a -> 0
"""
assert node.is_op(OP_MUL)
left, right = node
is_zero = lambda n: n.is_leaf() and n.value == 0
if is_zero(left):
negated = right.is_op(OP_NEG)
elif is_zero(right):
negated = left.is_op(OP_NEG)
elif left.is_op(OP_NEG) and is_zero(left[0]):
negated = not right.is_op(OP_NEG)
elif right.is_op(OP_NEG) and is_zero(right[0]):
negated = not left.is_op(OP_NEG)
else:
return []
return [P(node, multiply_zero, (negated,))]
def multiply_zero(root, args):
"""
a * 0 -> 0
0 * a -> 0
-0 * a -> -0
0 * -a -> -0
-0 * -a -> 0
"""
negated = args[0]
if negated:
return -Leaf(0)
else:
return Leaf(0)
MESSAGES[multiply_zero] = _('Multiplication with zero yields zero.')
def match_multiply_numerics(node): def match_multiply_numerics(node):
""" """
3 * 2 -> 6 3 * 2 -> 6
......
...@@ -102,8 +102,10 @@ class TestLeidenOefenopgave(TestCase): ...@@ -102,8 +102,10 @@ class TestLeidenOefenopgave(TestCase):
'-20x + 16 * x ^ 2 - 25 + 5 * 4x', '-20x + 16 * x ^ 2 - 25 + 5 * 4x',
'-20x + 16 * x ^ 2 - 25 + 20x', '-20x + 16 * x ^ 2 - 25 + 20x',
'(-20 + 20)x + 16 * x ^ 2 - 25', '(-20 + 20)x + 16 * x ^ 2 - 25',
'0x + 16 * x ^ 2 - 25',]) '0x + 16 * x ^ 2 - 25',
# FIXME: '16x^2 - 25']) '0 + 16 * x ^ 2 - 25',
'-25 + 16 * x ^ 2'])
# FIXME: '16 * x ^ 2 - 25'])
def test_2(self): def test_2(self):
pass pass
......
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