Commit 65cc88c7 authored by Taddeus Kroes's avatar Taddeus Kroes

Added IMPLICIT_RULES list for rules that are not printed in verbose rewrite_all mode.

parent 51e123ec
...@@ -20,6 +20,7 @@ from node import ExpressionNode as Node, \ ...@@ -20,6 +20,7 @@ from node import ExpressionNode as Node, \
DEFAULT_LOGARITHM_BASE, OP_VALUE_MAP, SPECIAL_TOKENS, OP_INT, \ DEFAULT_LOGARITHM_BASE, OP_VALUE_MAP, SPECIAL_TOKENS, OP_INT, \
OP_INT_INDEF, negation_to_node OP_INT_INDEF, negation_to_node
from rules.utils import find_variable from rules.utils import find_variable
from rules.precedences import IMPLICIT_RULES
from strategy import find_possibilities from strategy import find_possibilities
from possibilities import apply_suggestion from possibilities import apply_suggestion
...@@ -239,9 +240,10 @@ class Parser(BisonParser): ...@@ -239,9 +240,10 @@ class Parser(BisonParser):
if not self.root_node: if not self.root_node:
raise RuntimeError('No expression') raise RuntimeError('No expression')
if self.possibilities != None: if self.possibilities is not None:
if self.verbose: if self.verbose:
print 'Expression has not changed, not updating possibilities' print 'Expression has not changed, not updating possibilities'
return return
self.possibilities = find_possibilities(self.root_node) self.possibilities = find_possibilities(self.root_node)
...@@ -261,29 +263,49 @@ class Parser(BisonParser): ...@@ -261,29 +263,49 @@ class Parser(BisonParser):
for i, p in enumerate(self.possibilities): for i, p in enumerate(self.possibilities):
print '%d %s' % (i, p) print '%d %s' % (i, p)
def rewrite(self, index=0, verbose=False): def rewrite(self, index=0, verbose=False, check_implicit=True):
self.find_possibilities() self.find_possibilities()
if not self.possibilities: if not self.possibilities:
return False return
suggestion = self.possibilities[index] suggestion = self.possibilities[index]
if self.verbose: if self.verbose:
print 'Applying suggestion:', suggestion print 'EXPLICIT:', suggestion
elif verbose: elif verbose:
print suggestion print suggestion
expression = apply_suggestion(self.root_node, suggestion) self.set_root_node(apply_suggestion(self.root_node, suggestion))
if self.verbose: if self.verbose:
print 'After application: ', expression print ' ', self.root_node
elif verbose:
print expression # Only apply any remaining implicit hints if the suggestion itself is
# not implicit
if check_implicit and suggestion.handler not in IMPLICIT_RULES:
self.find_possibilities()
while self.possibilities:
sugg = self.possibilities[0]
if sugg.handler not in IMPLICIT_RULES:
break
self.set_root_node(expression) if self.verbose:
print 'IMPLICIT:', sugg
return True self.set_root_node(apply_suggestion(self.root_node, sugg))
if self.verbose:
print ' ', self.root_node
self.find_possibilities()
if verbose and not self.verbose:
print self.root_node
return self.root_node
def rewrite_all(self, verbose=False): def rewrite_all(self, verbose=False):
i = 0 i = 0
...@@ -292,10 +314,10 @@ class Parser(BisonParser): ...@@ -292,10 +314,10 @@ class Parser(BisonParser):
i += 1 i += 1
if i > 100: if i > 100:
print 'Too many rewrite steps, aborting...' lines.append('Too many rewrite steps, aborting...')
break break
if not verbose: if not verbose or not i:
return self.root_node return self.root_node
#def hook_run(self, filename, retval): #def hook_run(self, filename, retval):
...@@ -354,8 +376,7 @@ class Parser(BisonParser): ...@@ -354,8 +376,7 @@ class Parser(BisonParser):
return return
if option == 5: # rule: REWRITE NEWLINE if option == 5: # rule: REWRITE NEWLINE
self.rewrite() return self.rewrite()
return self.root_node
if option == 6: # rule: REWRITE NUMBER NEWLINE if option == 6: # rule: REWRITE NUMBER NEWLINE
self.rewrite(int(values[1])) self.rewrite(int(values[1]))
......
...@@ -4,7 +4,7 @@ from .numerics import reduce_fraction_constants ...@@ -4,7 +4,7 @@ from .numerics import reduce_fraction_constants
from .logarithmic import factor_in_exponent_multiplicant, \ from .logarithmic import factor_in_exponent_multiplicant, \
factor_out_exponent, raised_base factor_out_exponent, raised_base
from .derivatives import chain_rule from .derivatives import chain_rule
from .negation import double_negation
# Functions to move to the beginning of the possibilities list. Pairs of within # Functions to move to the beginning of the possibilities list. Pairs of within
# the list itself are compared by their position in the list: lower in the list # the list itself are compared by their position in the list: lower in the list
...@@ -40,3 +40,10 @@ RELATIVE = [ ...@@ -40,3 +40,10 @@ RELATIVE = [
# Convert to dictionaries for efficient lookup # Convert to dictionaries for efficient lookup
HIGH = dict([(h, i) for i, h in enumerate(HIGH)]) HIGH = dict([(h, i) for i, h in enumerate(HIGH)])
LOW = dict([(h, i) for i, h in enumerate(LOW)]) LOW = dict([(h, i) for i, h in enumerate(LOW)])
# List of implicit rules. Implicit rules are condidererd trivial and are
# therefore not printed in verbose rewrite_all mode
IMPLICIT_RULES = [
double_negation,
]
...@@ -12,7 +12,10 @@ def tree(exp, **kwargs): ...@@ -12,7 +12,10 @@ def tree(exp, **kwargs):
def rewrite(exp, **kwargs): def rewrite(exp, **kwargs):
return ParserWrapper(Parser, **kwargs).run([exp, '@']) wrapper = ParserWrapper(Parser, **kwargs)
wrapper.run([exp])
return wrapper.parser.rewrite(check_implicit=False)
class RulesTestCase(unittest.TestCase): class RulesTestCase(unittest.TestCase):
......
...@@ -82,7 +82,6 @@ class TestLeidenOefenopgave(TestCase): ...@@ -82,7 +82,6 @@ class TestLeidenOefenopgave(TestCase):
'x ^ 2 + (1 + 1)(-x) + 1', 'x ^ 2 + (1 + 1)(-x) + 1',
'x ^ 2 + 2(-x) + 1', 'x ^ 2 + 2(-x) + 1',
'x ^ 2 - 2x + 1', 'x ^ 2 - 2x + 1',
'x ^ 2 - 2x + 1',
]) ])
def test_1_4_1(self): def test_1_4_1(self):
......
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