-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
executable file
·387 lines (329 loc) · 8.35 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/python
from math import *
import readline
import locale
import units
class ParseError(Exception):
def __init__(self,reason):
self.reason=reason
def __str__(self):
return "Parse Error: %s" % self.reason
class NoSuchSymbol(Exception):
def __init__(self,symbol):
self.symbol = symbol
def __str__(self):
return "Undefined symbol: %s" % self.symbol
class AST:
def getValue(self,symtab):
return self.eval(symtab).getValue(symtab)
class Value:
def __init__(self,value,unit=None):
self.value=value
self.unit=unit
def eval(self,symtab):
return self
def getValue(self,symtab):
return self.value
def getUnit(self,symtab):
return self.unit
def __str__(self):
if self.value-int(self.value)==0:
v= str(int(self.value))
elif type(self.value) in [ type(0.0), type(0)]:
v= locale.format("%f",self.value,True)
else:
v=str(self.value)
if self.unit:
return v+" "+self.unit
else:
return v
def __repr__(self):
if self.unit:
return "Value(%s,%s)" % (str(self.value),self.unit)
else:
return "Value(%s)" % str(self.value)
class Variable(AST):
def __init__(self,name):
self.name=name
def __str__(self):
return self.name
def __repr__(self):
return "Variable(%s)" % self.name
def eval(self,symtab):
if symtab.has_key(self.name):
return symtab[self.name].eval(symtab)
else:
raise NoSuchSymbol(self.name)
def getUnit(self,symtab):
if symtab.has_key(self.name):
return symtab[self.name].getUnit(symtab)
else:
raise NoSuchSymbol(self.name)
class BinOp(AST):
def __init__(self,lhs,rhs,op="?"):
self.lhs=lhs
self.rhs=rhs
self.op=op
def __str__(self):
return str(self.lhs)+self.op+str(self.rhs)
def eval(self,symtab):
lhs = self.lhs.eval(symtab)
rhs = self.rhs.eval(symtab)
a=Value(eval("(lhs.getValue(symtab) "+self.op+" rhs.getValue(symtab))"),self.getUnit(symtab))
return a
class MulOp(BinOp):
def __init__(self,lhs,rhs):
BinOp.__init__(self,lhs,rhs,"*")
def getUnit(self,symtab):
lhs=self.lhs.getUnit(symtab)
rhs=self.rhs.getUnit(symtab)
if lhs==None:
lhs=""
if rhs==None:
rhs=""
return lhs+" "+rhs
class DivOp(BinOp):
def __init__(self,lhs,rhs):
BinOp.__init__(self,lhs,rhs,"/")
def getUnit(self,symtab):
lhs=self.lhs.getUnit(symtab)
rhs=self.rhs.getUnit(symtab)
if lhs==None or lhs=="":
if rhs==None or rhs=="":
return None
else:
return "1/("+rhs+")"
else:
if rhs==None or rhs=="":
return lhs
else:
return "("+lhs+")/("+rhs+")"
return "("+lhs+")/("+rhs+")"
# For addition and subtraction
class TermOp(BinOp):
def getValue(self,symtab):
# Ok, we have to convert everything to be in the same units.
lhs = self.lhs.eval(symtab)
rhs = self.rhs.eval(symtab)
if rhs.getUnit(symtab)==None:
# If the rhs has no unit, we pretend it's in the
# same unit. People are lazy, and the alternative
# is to raise an error, which just infuriates users
pass
else:
# Ok, the rhs has a unit, we need to convert it to
# be the same as the left
rhs=units.convert(rhs,rhs.getUnit(symtab),lhs.getUnit(symtab))
a=Value(eval("(lhs.getValue(symtab) "+self.op+" rhs.getValue(symtab))"),lhs.getUnit(symtab))
return a
def getUnit(self,symtab):
return self.lhs.getUnit(symtab)
class AddOp(TermOp):
def __init__(self,lhs,rhs): TermOp.__init__(self,lhs,rhs,"+")
class SubOp(TermOp):
def __init__(self,lhs,rhs): TermOp.__init__(self,lhs,rhs,"+")
class DefinedNativeFunction(AST):
def __init__(self,name,func):
self.name=name
self.func=func
def __str__(self):
return self.name+"("+",".join(
[ "a%i" % x
for x in range(self.func.func_code.co_argcount)
]
)+")"
def eval(self,symtab):
pass
def evalFunction(self,symtab,args):
return Value(apply(self.func,map(lambda x:x.getValue(symtab),args)))
def __repr__(self):
return "NativeFunction(%s,%s)" % (self.name,self.func)
class DefinedFunction(AST):
def __init__(self,func,argsnames,expansion):
self.func=func
self.argnames=argsnames
self.expansion=expansion
def __str__(self):
return self.func+"("+",".join(map(str,self.argnames))+")="+str(self.expansion)
def eval(self,symtab):
raise "Evaluating a defined function"
def evalFunction(self,symtab,args):
newsymtab=symtab.copy()
for i in range(len(args)):
newsymtab[self.argnames[i]]=args[i]
return self.expansion.eval(newsymtab)
def getFunctionUnit(self,symtab,args):
newsymtab=symtab.copy()
for i in range(len(args)):
newsymtab[self.argnames[i]]=args[i]
return self.expansion.getUnit(newsymtab)
def __repr__(self):
return "DefinedFunction(%s)" % str(self)
class EvalFunction(AST):
def __init__(self,func,args):
self.func=func
self.args=args
def __str__(self):
return self.func+"("+",".join(map(str,self.args))+")"
def eval(self,symtab):
return symtab[self.func].evalFunction(
symtab,
map(lambda x:x.eval(symtab),
self.args))
def getUnit(self,symtab):
return symtab[self.func].getFunctionUnit(
symtab,
map(lambda x:x.eval(symtab),
self.args))
symtab = {
"inc" : DefinedFunction("inc",["x"],BinOp(Variable("x"),Value(1),"+")),
"sin" : DefinedNativeFunction("sin",sin),
"x" : Value(3),
}
def skip_white(x):
while x!="" and x[0] in " \t\n\r":
x=x[1:]
return x
def parse_value(x):
x=skip_white(x)
if x=="":
raise ParseError("Unexpected end of expression")
if x[0] == "(":
x,a = parse(x[1:])
if x=="":
raise ParseError("expected ')'")
if x[0]!=")":
raise ParseError("expected ')' got '%s' instead" % `x`)
return x[1:],a
if x[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_":
tok=""
while x!="" and x[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789":
tok=tok+x[0]
x=x[1:]
# TODO, if a ( follows it's a function call
x=skip_white(x)
if x!="" and x[0]=="(":
args=[]
x=x[1:]
while 1:
x,arg=parse(x)
args.append(arg)
x=skip_white(x)
if x[0]==")":
return x[1:],EvalFunction(tok,args)
if x[0]==",":
x=x[1:]
continue
raise ParseError("expected ',' or ')' got %s" % `x`)
return x,Variable(tok)
if x!="" and x[0] in "0123456789.":
tok=""
while x!="" and x[0] in "0123456789":
tok=tok+x[0]
x=x[1:]
if x=="" or x[0]!=".":
return x,Value(int(tok))
x=x[1:]
tok=tok+"."
while x!="" and x[0] in "0123456789":
tok=tok+x[0]
x=x[1:]
return x,Value(float(tok))
raise ParseError("Unexpected %s" % `x`)
def parse_exponent(x):
x,term = parse_value(x)
x = skip_white(x)
while x!="":
if x[0]=="^":
x,term2 = parse_value(x[1:])
term = BinOp(term,term2,"**")
else:
break
return x,term
def parse_factor(x):
x,term = parse_exponent(x)
x = skip_white(x)
while x!="":
if x[0]=="*":
x,term2 = parse_exponent(x[1:])
term = MulOp(term,term2)
elif x[0]=="/":
x,term2 = parse_exponent(x[1:])
term = DivOp(term,term2)
else:
break
return x,term
def parse_term(x):
x,term = parse_factor(x)
x=skip_white(x)
while x!="":
if x[0]=="+":
x,term2 = parse_factor(x[1:]) # Skip the +
term = AddOp(term,term2)
elif x[0]=="-":
x,term2 = parse_factor(x[1:]) # Skip the -
term = SubOp(term,term2)
else:
break
return x,term
parse=parse_term
id=0
def evaluate(x):
global id
x,exp=parse(x)
x=skip_white(x)
if x!="" and x[0]=="=":
func=exp
x=x[1:]
x,exp = parse(x)
x=skip_white(x)
if x!="":
raise ParseError("%s left over" % `x`)
if func.__class__==Variable:
symtab[func.name]=exp
return Variable(func.name)
elif func.__class__==EvalFunction:
symtab[func.func]=DefinedFunction(func.func,
map(lambda x:x.name,func.args),
exp)
else:
raise ParseError("can't assign to '%s'" % func)
return symtab[func.func]
else:
if x!="":
raise ParseError("Parse Error %s left over" % `x`)
r=exp.eval(symtab)
symtab["_"]=r
symtab["_%i" % id]=r
return exp
def evaluate_str(x):
assert(type(x)==type(""))
try:
a=evaluate(x)
except ParseError, e:
return str(e)
except NoSuchSymbol, e:
return str(e)
if a.__class__==DefinedFunction:
return str(a)
else:
a2=a.eval(symtab)
v,u=units.simplify(a2.getValue(symtab),a2.getUnit(symtab))
a2=Value(v,u)
return "%s = %s" % ( str(a),str(a2))
def expression_str(x):
try:
if x.__class__==Variable:
return str(x)+"="+str(symtab[x.name])
except e:
raise
return str(x)
def init():
units.load()
for k,v in units.units.items():
symtab[k]=Value(1,k)
if __name__=="__main__":
init()
while 1:
id=id+1
print evaluate_str(raw_input("%i> " % id))