Commit fc774582 authored by Taddeus Kroes's avatar Taddeus Kroes

Merge branch 'master' of kompiler.org:trs

parents 513c04f3 7c83ea25
graph_drawing @ ec0a7e59
Subproject commit 45bee0e4fe53619176d85fc0bc5ae3b40d99716d Subproject commit ec0a7e59d3251d1e11d2efc877a77f236a7885b5
pybison @ d83b7274
Subproject commit 260359e31ceeaa46d26a26303e08cda37c7578f5 Subproject commit d83b7274f5a331c7c5611974c9576495d929c234
...@@ -28,4 +28,5 @@ endif ...@@ -28,4 +28,5 @@ endif
$(b)pybison/%.c: $(d)pybison/src/pyrex/%.pyx $(b)pybison/%.c: $(d)pybison/src/pyrex/%.pyx
$(py2c) -o $@ $< $(py2c) -o $@ $<
$(RM) $(@D)/*.so
...@@ -63,6 +63,9 @@ class Parser(BisonParser): ...@@ -63,6 +63,9 @@ class Parser(BisonParser):
# 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):
if self.file == sys.stdin and self.file.closed:
return ''
try: try:
return raw_input('>>> ') + '\n' return raw_input('>>> ') + '\n'
except EOFError: except EOFError:
...@@ -187,24 +190,24 @@ class Parser(BisonParser): ...@@ -187,24 +190,24 @@ class Parser(BisonParser):
""" """
if option in [0, 1]: # rule: exp IDENTIFIER | exp NUMBER if option in [0, 1]: # rule: exp IDENTIFIER | exp NUMBER
# NOTE: xy -> x*y # example: xy -> x*y
# NOTE: (x)4 -> x*4 # example: (x)4 -> x*4
val = int(values[1]) if option == 1 else values[1] val = int(values[1]) if option == 1 else values[1]
return Node('*', *(combine('*', values[0]) + [Leaf(val)])) return Node('*', *(combine('*', values[0]) + [Leaf(val)]))
if option == 2: # rule: exp LPAREN exp RPAREN if option == 2: # rule: exp LPAREN exp RPAREN
# NOTE: x(y) -> x*(y) # example: x(y) -> x*(y)
return Node('*', *(combine('*', values[0]) return Node('*', *(combine('*', values[0])
+ combine('*', values[2]))) + combine('*', values[2])))
if option == 3: if option == 3:
# NOTE: x4 -> x^4 # example: xy4 -> x*y^4
identifier, exponent = list(values[1]) identifier, exponent = list(values[1])
node = Node('^', Leaf(identifier), Leaf(int(exponent))) node = Node('^', Leaf(identifier), Leaf(int(exponent)))
return Node('*', values[0], node) return Node('*', values[0], node)
if option == 4: if option == 4:
# NOTE: x4 -> x^4 # example: x4 -> x^4
identifier, exponent = list(values[0]) identifier, exponent = list(values[0])
return Node('^', Leaf(identifier), Leaf(int(exponent))) return Node('^', Leaf(identifier), Leaf(int(exponent)))
......
...@@ -83,7 +83,7 @@ def run_expressions(base_class, expressions, keepfiles=1, fail=True, ...@@ -83,7 +83,7 @@ def run_expressions(base_class, expressions, keepfiles=1, fail=True,
try: try:
res = parser.run([exp]) res = parser.run([exp])
assert res == out assert res == out
except: except: # pragma: nocover
if not silent: if not silent:
print >>sys.stderr, 'error: %s = %s, but expected: %s' \ print >>sys.stderr, 'error: %s = %s, but expected: %s' \
% (exp, str(res), str(out)) % (exp, str(res), str(out))
......
# vim: set fileencoding=utf-8 :
import unittest
from src.parser import Parser
from tests.parser import ParserWrapper, run_expressions
class TestException(unittest.TestCase):
def test_raise(self):
try:
ParserWrapper(Parser).run(['raise'])
except RuntimeError:
return
# pragma: nocover
raise AssertionError('Expected a raised RuntimeError!')
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