Fixed float overflow error in grammar and changed '**' -> '^'.

parent 3076dd4c
...@@ -106,21 +106,34 @@ class Parser(BisonParser): ...@@ -106,21 +106,34 @@ class Parser(BisonParser):
print 'on_exp: got %s %s %s %s' % (target, option, names, values) print 'on_exp: got %s %s %s %s' % (target, option, names, values)
if option == 0: if option == 0:
# TODO: A bit hacky, this achieves long integers and floats.
# return float(values[0]) if '.' in values[0] else long(values[0])
return float(values[0]) return float(values[0])
elif option == 1:
if option == 7:
return values[1]
try:
if option == 1:
return values[0] + values[2] return values[0] + values[2]
elif option == 2:
if option == 2:
return values[0] - values[2] return values[0] - values[2]
elif option == 3:
if option == 3:
return values[0] * values[2] return values[0] * values[2]
elif option == 4:
if option == 4:
return values[0] / values[2] return values[0] / values[2]
elif option == 5:
if option == 5:
return - values[1] return - values[1]
elif option == 6:
if option == 6:
return values[0] ** values[2] return values[0] ** values[2]
elif option == 7: except OverflowError:
return values[1] print >>sys.stderr, 'error: Overflow occured in "%s" %s %s %s' \
% (target, option, names, values)
# ----------------------------------------- # -----------------------------------------
# raw lex script, verbatim here # raw lex script, verbatim here
...@@ -147,7 +160,7 @@ class Parser(BisonParser): ...@@ -147,7 +160,7 @@ class Parser(BisonParser):
"+" { returntoken(PLUS); } "+" { returntoken(PLUS); }
"-" { returntoken(MINUS); } "-" { returntoken(MINUS); }
"*" { returntoken(TIMES); } "*" { returntoken(TIMES); }
"**" { returntoken(POW); } "^" { returntoken(POW); }
"/" { returntoken(DIVIDE); } "/" { returntoken(DIVIDE); }
"quit" { printf("lex: got QUIT\n"); yyterminate(); returntoken(QUIT); } "quit" { printf("lex: got QUIT\n"); yyterminate(); returntoken(QUIT); }
......
import sys
import unittest import unittest
...@@ -38,5 +39,35 @@ class TestCalc(unittest.TestCase): ...@@ -38,5 +39,35 @@ class TestCalc(unittest.TestCase):
def tearDown(self): def tearDown(self):
pass pass
def run_expressions(self, expressions, fail=True):
for exp, out in expressions:
try:
res = TestParser([exp], keepfiles=1).run()
assert res == out
except:
print >>sys.stderr, 'error: %s = %s, but expected: %s' \
% (exp, str(res), str(out))
if fail:
raise
def test_constructor(self): def test_constructor(self):
assert TestParser(['1+4'], keepfiles=1).run() == 5.0 assert TestParser(['1+4'], keepfiles=1).run() == 5.0
def test_basic_on_exp(self):
expressions = [('4', 4.0),
('3+4', 7.0),
('3-4', -1.0),
('3/4', .75),
('-4', -4.0),
('3^4', 81.0),
('(4)', 4.0)]
self.run_expressions(expressions)
def test_infinity(self):
expressions = [('2^9999', None),
('2^-9999', 0.0),
('2^99999999999', None),
('2^-99999999999', 0.0)]
self.run_expressions(expressions, fail=False)
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