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, \
DEFAULT_LOGARITHM_BASE, OP_VALUE_MAP, SPECIAL_TOKENS, OP_INT, \
OP_INT_INDEF, negation_to_node
from rules.utils import find_variable
from rules.precedences import IMPLICIT_RULES
from strategy import find_possibilities
from possibilities import apply_suggestion
......@@ -239,9 +240,10 @@ class Parser(BisonParser):
if not self.root_node:
raise RuntimeError('No expression')
if self.possibilities != None:
if self.possibilities is not None:
if self.verbose:
print 'Expression has not changed, not updating possibilities'
return
self.possibilities = find_possibilities(self.root_node)
......@@ -261,29 +263,49 @@ class Parser(BisonParser):
for i, p in enumerate(self.possibilities):
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()
if not self.possibilities:
return False
return
suggestion = self.possibilities[index]
if self.verbose:
print 'Applying suggestion:', suggestion
print 'EXPLICIT:', suggestion
elif verbose:
print suggestion
expression = apply_suggestion(self.root_node, suggestion)
self.set_root_node(apply_suggestion(self.root_node, suggestion))
if self.verbose:
print 'After application: ', expression
elif verbose:
print expression
print ' ', self.root_node
# 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):
i = 0
......@@ -292,10 +314,10 @@ class Parser(BisonParser):
i += 1
if i > 100:
print 'Too many rewrite steps, aborting...'
lines.append('Too many rewrite steps, aborting...')
break
if not verbose:
if not verbose or not i:
return self.root_node
#def hook_run(self, filename, retval):
......@@ -354,8 +376,7 @@ class Parser(BisonParser):
return
if option == 5: # rule: REWRITE NEWLINE
self.rewrite()
return self.root_node
return self.rewrite()
if option == 6: # rule: REWRITE NUMBER NEWLINE
self.rewrite(int(values[1]))
......
......@@ -4,7 +4,7 @@ from .numerics import reduce_fraction_constants
from .logarithmic import factor_in_exponent_multiplicant, \
factor_out_exponent, raised_base
from .derivatives import chain_rule
from .negation import double_negation
# 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
......@@ -40,3 +40,10 @@ RELATIVE = [
# Convert to dictionaries for efficient lookup
HIGH = dict([(h, i) for i, h in enumerate(HIGH)])
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):
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):
......
......@@ -82,7 +82,6 @@ class TestLeidenOefenopgave(TestCase):
'x ^ 2 + (1 + 1)(-x) + 1',
'x ^ 2 + 2(-x) + 1',
'x ^ 2 - 2x + 1',
'x ^ 2 - 2x + 1',
])
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