Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
trs
Commits
1bf91074
Commit
1bf91074
authored
Nov 21, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed float overflow error in grammar and changed '**' -> '^'.
parent
3076dd4c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
14 deletions
+58
-14
src/calc.py
src/calc.py
+27
-14
tests/test_calc.py
tests/test_calc.py
+31
-0
No files found.
src/calc.py
View file @
1bf91074
...
...
@@ -106,21 +106,34 @@ class Parser(BisonParser):
print
'on_exp: got %s %s %s %s'
%
(
target
,
option
,
names
,
values
)
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
])
elif
option
==
1
:
if
option
==
7
:
return
values
[
1
]
try
:
if
option
==
1
:
return
values
[
0
]
+
values
[
2
]
elif
option
==
2
:
if
option
==
2
:
return
values
[
0
]
-
values
[
2
]
elif
option
==
3
:
if
option
==
3
:
return
values
[
0
]
*
values
[
2
]
elif
option
==
4
:
if
option
==
4
:
return
values
[
0
]
/
values
[
2
]
elif
option
==
5
:
if
option
==
5
:
return
-
values
[
1
]
elif
option
==
6
:
if
option
==
6
:
return
values
[
0
]
**
values
[
2
]
elif
option
==
7
:
return
values
[
1
]
except
OverflowError
:
print
>>
sys
.
stderr
,
'error: Overflow occured in "%s" %s %s %s'
\
%
(
target
,
option
,
names
,
values
)
# -----------------------------------------
# raw lex script, verbatim here
...
...
@@ -147,7 +160,7 @@ class Parser(BisonParser):
"+" { returntoken(PLUS); }
"-" { returntoken(MINUS); }
"*" { returntoken(TIMES); }
"
**"
{ returntoken(POW); }
"
^"
{ returntoken(POW); }
"/" { returntoken(DIVIDE); }
"quit" { printf("lex: got QUIT\n"); yyterminate(); returntoken(QUIT); }
...
...
tests/test_calc.py
View file @
1bf91074
import
sys
import
unittest
...
...
@@ -38,5 +39,35 @@ class TestCalc(unittest.TestCase):
def
tearDown
(
self
):
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
):
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment