Trying to fix the negation AssertionError (work in progress).

parent 28644d89
...@@ -165,9 +165,6 @@ class ExpressionNode(Node, ExpressionBase): ...@@ -165,9 +165,6 @@ class ExpressionNode(Node, ExpressionBase):
self.type = TYPE_OPERATOR self.type = TYPE_OPERATOR
self.op = OP_MAP[args[0]] self.op = OP_MAP[args[0]]
if hasattr(self.op, '__call__'):
self.op = self.op(args)
def __str__(self): # pragma: nocover def __str__(self): # pragma: nocover
return generate_line(self) return generate_line(self)
......
...@@ -74,13 +74,20 @@ class Parser(BisonParser): ...@@ -74,13 +74,20 @@ class Parser(BisonParser):
BisonParser.__init__(self, **kwargs) BisonParser.__init__(self, **kwargs)
self.interactive = kwargs.get('interactive', 0) self.interactive = kwargs.get('interactive', 0)
self.timeout = kwargs.get('timeout', 0) self.timeout = kwargs.get('timeout', 0)
self.possibilities = self.last_possibilities = []
self.reset()
def reset(self):
self.read_buffer = '' self.read_buffer = ''
self.read_queue = Queue.Queue() self.read_queue = Queue.Queue()
self.subtree_map = {} self.subtree_map = {}
self.root_node = None self.root_node = None
self.possibilities = self.last_possibilities = []
def run(self, *args, **kwargs):
self.reset()
return super(Parser, self).run(*args, **kwargs)
# Override default read method with a version that prompts for input. # Override default read method with a version that prompts for input.
def read(self, nbytes): def read(self, nbytes):
...@@ -201,6 +208,7 @@ class Parser(BisonParser): ...@@ -201,6 +208,7 @@ class Parser(BisonParser):
handlers = [] handlers = []
if retval.negated: if retval.negated:
print retval, 'OP_NEG', retval.negated
handlers += RULES[OP_NEG] handlers += RULES[OP_NEG]
for handler in handlers: for handler in handlers:
...@@ -372,6 +380,10 @@ class Parser(BisonParser): ...@@ -372,6 +380,10 @@ class Parser(BisonParser):
if option == 4: # rule: exp MINUS exp if option == 4: # rule: exp MINUS exp
node = values[2] node = values[2]
node.negated += 1 node.negated += 1
# Explicit call the hook handler on the created unary negation.
node = self.hook_handler('binary', 4, names, values, node)
return Node('+', values[0], node) return Node('+', values[0], node)
raise BisonSyntaxError('Unsupported option %d in target "%s".' raise BisonSyntaxError('Unsupported option %d in target "%s".'
...@@ -415,8 +427,6 @@ class Parser(BisonParser): ...@@ -415,8 +427,6 @@ class Parser(BisonParser):
yylloc.first_column = yycolumn; \ yylloc.first_column = yycolumn; \
yylloc.last_column = yycolumn + yyleng; \ yylloc.last_column = yycolumn + yyleng; \
yycolumn += yyleng; yycolumn += yyleng;
/*[a-zA-Z][0-9]+ { returntoken(CONCAT_POW); }*/
%} %}
%option yylineno %option yylineno
......
from ..node import Scope, nary_node, OP_ADD, OP_MUL, OP_DIV from ..node import Scope, OP_ADD, OP_MUL, OP_DIV, TYPE_OPERATOR
from ..possibilities import Possibility as P, MESSAGES from ..possibilities import Possibility as P, MESSAGES
from ..translate import _ from ..translate import _
...@@ -46,7 +46,9 @@ def match_negate_polynome(node): ...@@ -46,7 +46,9 @@ def match_negate_polynome(node):
--a -> a --a -> a
-(a + b) -> -a - b -(a + b) -> -a - b
""" """
assert node.negated if not node.negated and node.type == TYPE_OPERATOR:
print 'operator\'s negated childs:', [n.negated for n in node]
assert node.negated, str(node.negated) + '; ' + str(node)
p = [] p = []
......
...@@ -23,3 +23,13 @@ class TestParser(unittest.TestCase): ...@@ -23,3 +23,13 @@ class TestParser(unittest.TestCase):
def test_line(self): def test_line(self):
self.assertEqual(line(Parser, '4-a'), '4 - a') self.assertEqual(line(Parser, '4-a'), '4 - a')
def test_reset_after_failure(self):
parser = ParserWrapper(Parser)
parser.run(['-(3a+6b)'])
possibilities = parser.parser.possibilities
print possibilities
parser.run(['5+2*6'])
possibilities = parser.parser.possibilities
print possibilities
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