-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathcalc.py
52 lines (39 loc) · 1.19 KB
/
calc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# calc.py - # A simple calculator without using eval
# A shorter and simpler re-implementation of http://www.dabeaz.com/ply/example.html
import operator as op
from plyplus import Grammar, STransformer
calc_grammar = Grammar("""
start: add;
// Rules
?add: (add add_symbol)? mul;
?mul: (mul mul_symbol)? atom;
@atom: neg | number | '\(' add '\)';
neg: '-' atom;
// Tokens
number: '[\d.]+';
mul_symbol: '\*' | '/';
add_symbol: '\+' | '-';
WS: '[ \t]+' (%ignore);
""")
class Calc(STransformer):
def _bin_operator(self, exp):
arg1, operator_symbol, arg2 = exp.tail
operator_func = { '+': op.add, '-': op.sub, '*': op.mul, '/': op.div }[operator_symbol]
return operator_func(arg1, arg2)
number = lambda self, exp: float(exp.tail[0])
neg = lambda self, exp: -exp.tail[0]
__default__ = lambda self, exp: exp.tail[0]
add = _bin_operator
mul = _bin_operator
def main():
calc = Calc()
while True:
try:
s = raw_input('> ')
except EOFError:
break
if s == '':
break
tree = calc_grammar.parse(s)
print(calc.transform(tree))
main()