Code cleanup and added interactie parser mode (for shells).

parent 93a5449d
......@@ -40,6 +40,12 @@ class Parser(BisonParser):
('right', ('POW', )),
)
interactive = 0
def __init__(self, **kwargs):
BisonParser.__init__(self, **kwargs)
self.interactive = kwargs.get('interactive', 0)
# ------------------------------------------------------------------
# override default read method with a version that prompts for input
# ------------------------------------------------------------------
......@@ -67,7 +73,11 @@ class Parser(BisonParser):
input :
| input line
"""
return
if option == 1:
if self.interactive:
print values[1]
return values[1]
def on_line(self, target, option, names, values):
"""
......@@ -75,7 +85,10 @@ class Parser(BisonParser):
| exp NEWLINE
"""
if option == 1:
print 'on_line: exp =', values[0]
if self.verbose:
print 'on_line: exp =', values[0]
return values[0]
def on_exp(self, target, option, names, values):
"""
......@@ -88,7 +101,9 @@ class Parser(BisonParser):
| exp POW exp
| LPAREN exp RPAREN
"""
print 'on_exp: got %s %s %s %s' % (target, option, names, values)
if self.verbose:
print 'on_exp: got %s %s %s %s' % (target, option, names, values)
if option == 0:
return float(values[0])
......@@ -146,5 +161,5 @@ class Parser(BisonParser):
"""
if __name__ == '__main__':
p = Parser(verbose=0, keepfiles=1)
p = Parser(verbose=0, keepfiles=1, interactive=1)
p.run(debug=0)
......@@ -12,7 +12,7 @@ endif
test: $(TESTS) build
coverage: ${COVERAGE}
coverage: ${COVERAGE} build
mkdir ${COVERAGE_OUTPUT_DIR} 2>/dev/null || true
${COVERAGE} erase
for t in ${TESTS}; do \
......@@ -26,4 +26,4 @@ ${COVERAGE}:
@echo "Install package 'python-coverage' to generate a coverage report."
@echo "On Debian/Ubuntu use: sudo apt-get install python-coverage"; false
$(TESTS): ; @python -m testrunner $@
$(TESTS): build; @python -m testrunner $@
import unittest
from src.calc import Parser
class TestParser(Parser):
def __init__(self, input_buffer, **kwargs):
Parser.__init__(self, **kwargs)
self.input_buffer = []
self.input_position = 0
map(self.append, input_buffer)
def append(self, input):
self.input_buffer.append(input + '\n')
def read(self, nbytes):
buffer = ''
try:
buffer = self.input_buffer[self.input_position]
except IndexError:
return ''
self.input_position += 1
return buffer
class TestCalc(unittest.TestCase):
def setUp(self):
......@@ -9,5 +38,5 @@ class TestCalc(unittest.TestCase):
def tearDown(self):
pass
def test_true(self):
assert True
def test_constructor(self):
assert TestParser(['1+4'], keepfiles=1).run() == 5.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