-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
163 lines (146 loc) · 4.07 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# grammar for parsing
# e -> e0 '#'
# e0 -> e1 (('+'|'-') e1)*
# e1 -> e2 (('*'|'/'|'%') e2)*
# e2 -> (('+','-')* e2)
# e2 -> e3 ('**' e2)*
# e3 == tn
# tn -> number
# tn -> function '(' ')'
# tn -> function '(' l0 ')'
# tn -> constant
# tn -> '(' e0 ')'
# l0 -> e0
# l0 -> e0 ',' l0
import math
import random
functions = dict([(name.upper(), getattr(math, name)) for name in
"acos asin atan atan2 ceil cos cosh degrees exp fabs floor fmod frexp hypot ldexp log log10 modf pow radians sin sinh sqrt tan tanh".split(" ")])
functions['RANDINT'] = random.randint
functions['COMPLEX'] = complex
consts = dict(E=math.e, PI=math.pi, I=complex(0, 1), J=complex(0, 1))
def sorted(it, *args, **kwargs):
l = list(it)
l.sort(*args, **kwargs)
return l
def calc(expr):
if expr == "help":
h = ["Calculator. Numbers like 100, 100.0, 1e2, 1.0e2 all ",
"work. The operators +, -, *, /, %, ** are available. The ",
"constants available are: "]
h.append(", ".join(sorted(consts.iterkeys())))
h.append(". The functions available are: ")
h.append(", ".join(sorted(functions.iterkeys())))
return "".join(h)
x = [c for c in expr if not c.isspace()]
x.append('#')
x = "".join(x)
p = [0]
def require(b):
if not b:
raise Exception("parse error, unexpected %r" % peek())
# peek() and nom() are now doing lexing instead of just characters
def nom():
if peek() == '**':
p[0] = p[0] + 1
p[0] = p[0] + 1
def peek():
c = x[p[0]]
if c == '*' and x[p[0]+1] == '*':
return '**'
return c
def e():
v = e0()
require(peek() == '#')
return v
def e0():
v = e1()
while peek() in '+-':
o = peek()
nom()
v2 = e1()
if o == '+':
v = v + v2
else:
v = v - v2
return v
def e1():
v = e2()
while peek() in '*/%':
o = peek()
nom()
v2 = e2()
if o == '*':
v = v * v2
elif o == '/':
v = v / v2
else:
v = v % v2
return v
def e2():
z = 1
while peek() in "+-":
o = peek()
nom()
if o == '-':
z *= -1
# cheat, dropping out of the above while loop is
# equivalent to returning z*e2() in the grammar.
v = tn()
while peek() == '**':
nom()
v2 = e2()
v = v ** v2
return z*v
def tn():
tn_states = ('.e', '+-', '', '', 'e')
if peek().isdigit():
v = ''
state = 0
while peek().isdigit() or (peek() in tn_states[state]):
if peek() == 'e':
state = 1
# after eating e, single following char may be + or -.
elif state == 1:
state = 2
elif peek() == '.':
state = 3
# after eating ., single following char must be digit
elif state == 3:
state = 4
v += peek()
nom()
v = float(v)
return v
if peek().isalpha():
fn = ''
while peek().isalpha():
fn += peek()
nom()
fn = fn.upper()
if fn in consts:
return consts[fn]
require(peek() == '(')
nom()
if peek() == ')':
v = []
else:
v = l0()
require(peek() == ')')
nom()
return functions[fn](*v)
if peek() == '(':
nom()
v = e0()
require(peek() == ')')
nom()
return v
require(False)
def l0():
v = []
v.append(e0())
while peek() == ',':
nom()
v.append(e0())
return v
return e()