Commit c2fb4035 authored by Taddeus Kroes's avatar Taddeus Kroes

Improved numeric rules match functions.

- Added two new match functions, that still have to filled in.
- Division of identifiers yielded a rewrite possibility before, this is now
  fixed so that only numerics will be divided.
parent 78227c82
from ..node import ExpressionLeaf as Leaf, TYPE_INTEGER, TYPE_FLOAT, OP_DIV from ..node import ExpressionLeaf as Leaf, OP_DIV, OP_MUL
from ..possibilities import Possibility as P from ..possibilities import Possibility as P
from .utils import nary_node from .utils import nary_node
...@@ -20,19 +20,15 @@ def match_divide_numerics(node): ...@@ -20,19 +20,15 @@ def match_divide_numerics(node):
assert node.is_op(OP_DIV) assert node.is_op(OP_DIV)
n, d = node n, d = node
n_int = n.type == TYPE_INTEGER
n_float = n.type == TYPE_FLOAT
d_int = d.type == TYPE_INTEGER
d_float = d.type == TYPE_FLOAT
nv, dv = n.value, d.value
divide = False divide = False
dv = d.value
if n_int and d_int: if n.is_int() and d.is_int():
# 6 / 2 -> 3 # 6 / 2 -> 3
# 3 / 2 -> 3 / 2 # 3 / 2 -> 3 / 2
divide = not divmod(nv, dv)[1] divide = not divmod(n.value, dv)[1]
else: elif n.is_numeric() and d.is_numeric():
if d_float and dv == 1.0: if d == 1.0:
# 3 / 1.0 -> 3 # 3 / 1.0 -> 3
dv = 1 dv = 1
...@@ -41,7 +37,29 @@ def match_divide_numerics(node): ...@@ -41,7 +37,29 @@ def match_divide_numerics(node):
# 3.0 / 2.0 -> 1.5 # 3.0 / 2.0 -> 1.5
divide = True divide = True
return [P(node, divide_numerics, (nv, dv))] if divide else [] return [P(node, divide_numerics, (n.value, dv))] if divide else []
def match_multiply_numerics(node):
"""
3 * 2 -> 6
3.0 * 2 -> 6.0 # FIXME: is this correct?
3 * 2.0 -> 6.0 # FIXME: is this correct?
3.0 * 2.0 -> 6.0
"""
# TODO: Finish
assert node.is_op(OP_MUL)
def match_subtract_numerics(node):
"""
3 - 2 -> 2.0
3.0 - 2 -> 1.0 # FIXME: is this correct?
3 - 2.0 -> 1.0 # FIXME: is this correct?
3.0 - 2.0 -> 1.0
"""
# TODO: Finish
assert node.is_op(OP_MUL)
def divide_numerics(root, args): def divide_numerics(root, args):
......
...@@ -10,8 +10,8 @@ class TestRulesNumerics(RulesTestCase): ...@@ -10,8 +10,8 @@ class TestRulesNumerics(RulesTestCase):
def test_match_divide_numerics(self): def test_match_divide_numerics(self):
# FIXME: Parser does not recognize floats # FIXME: Parser does not recognize floats
#i2, i3, i6, f1, f2, f3 = tree('2,3,6,1.0,2.0,3.0') #a, b, i2, i3, i6, f1, f2, f3 = tree('a,b,2,3,6,1.0,2.0,3.0')
i2, i3, i6 = tree('2,3,6') a, b, i2, i3, i6 = tree('a,b,2,3,6')
f1, f2, f3 = L(1.0), L(2.0), L(3.0) f1, f2, f3 = L(1.0), L(2.0), L(3.0)
root = i6 / i2 root = i6 / i2
...@@ -43,6 +43,10 @@ class TestRulesNumerics(RulesTestCase): ...@@ -43,6 +43,10 @@ class TestRulesNumerics(RulesTestCase):
self.assertEqualPos(possibilities, self.assertEqualPos(possibilities,
[P(root, divide_numerics, (3, 1))]) [P(root, divide_numerics, (3, 1))])
root = a / b
possibilities = match_divide_numerics(root)
self.assertEqualPos(possibilities, [])
def test_divide_numerics(self): def test_divide_numerics(self):
# FIXME: Parser does not recognize floats # FIXME: Parser does not recognize floats
#i2, i3, i6, f2, f3 = tree('2,3,6,2.0,3.0') #i2, i3, i6, f2, f3 = tree('2,3,6,2.0,3.0')
......
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