forked from FrBrGeorge/PythonWinter2015-TPG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caalc.py
executable file
·196 lines (168 loc) · 6.26 KB
/
caalc.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
#!/usr/bin/python
# coding: utf
import readline
import sys
import tpg
import itertools
def make_op(s):
return {
'+': lambda x,y: x+y,
'-': lambda x,y: x-y,
'*': lambda x,y: x*y,
'/': lambda x,y: x/y,
'&': lambda x,y: x&y,
'|': lambda x,y: x|y,
}[s]
class Promise(object):
def get(self, Vars):
raise NotImplementedError()
class PromMatrix(Promise):
def __init__(self, m):
self.m = m
def get(self, Vars):
return Matrix([x.get(Vars) for x in row] for row in self.m)
class PromOp(Promise):
def __init__(self, op, *args):
self.op = op
self.args = args
def get(self, Vars):
return self.op(*(arg.get(Vars) for arg in self.args))
class PromVar(Promise):
def __init__(self, varName):
self.varName = varName
def get(self, Vars):
return Vars[self.varName]
class PromInt(Promise):
def __init__(self, val):
self.val = int(val)
def get(self, Vars):
return self.val
class PromFloat(Promise):
def __init__(self, val):
self.val = float(val)
def get(self, Vars):
return self.val
class PromFuncCall(Promise):
def __init__(self, varProm, args):
self.varProm = varProm
self.args = args
def get(self, Vars):
return self.varProm.get(Vars).call([arg.get(Vars) for arg in self.args])
class Matrix(list):
@staticmethod
def __blockMatrix(M):
s = [[((m.nrows, m.ncols) if isinstance(m, Matrix) else (1,1)) for m in row] for row in M]
for row in s:
for i in xrange(1, len(row)):
if row[i][0] != row[i-1][0]:
raise TypeError("Inconsistent sizes in block matrix construction")
for j in xrange(len(s[0])):
for i in xrange(1,len(s)):
if s[i][j][1] != s[i-1][j][1]:
raise TypeError("Inconsistent sizes in block matrix construction")
nrows = sum(row[0][0] for row in s)
ncols = sum(x[1] for x in s[0])
new = [[] for i in xrange(nrows)]
offi = 0
for rowidx, row in enumerate(M):
for m in row:
if isinstance(m, Matrix):
for i, row in enumerate(m):
new[offi + i].extend(row)
else:
new[offi].append(m)
offi += s[rowidx][0][0]
return new
def __init__(self, *argp, **argn):
list.__init__(self, *argp, **argn)
if len(self) != 0:
for row in self:
if len(row) != len(self[0]):
raise TypeError("Rows must have equal lengths")
list.__init__(self, Matrix.__blockMatrix(self))
self.nrows = len(self)
self.ncols = len(self[0])
else:
self.nrows = self.ncols = 0
def __str__(self):
widths = [max(len(str(self[i][j])) for i in xrange(self.nrows)) for j in xrange(self.ncols)]
fmt = " ".join(" {:>" + str(w) + "}" for w in widths)
return "[" + ";\n ".join(fmt.format(*row) for row in self) + " ]"
def __elwiseop(self, a, op):
if self.nrows != a.nrows or self.ncols != a.ncols:
raise TypeError("Matrices must be of equal sizes, given {}x{} and {}x{}".format(self.nrows, self.ncols, a.nrows, a.ncols))
return Matrix([op(x, y) for x,y in zip(row1, row2)] for row1, row2 in zip(self, a))
def __add__(self, a): return self.__elwiseop(a, lambda c,d: c+d)
def __sub__(self, a): return self.__elwiseop(a, lambda c,d: c-d)
def __mul__(self, a):
if self.ncols != a.nrows:
raise TypeError("Inconsistent matrix sizes: {}x{} and {}x{}".format(self.nrows, self.ncols, a.nrows, a.ncols))
return Matrix([sum(self[i][j] * a[j][k] for j in xrange(self.ncols))
for k in xrange(a.ncols)]
for i in xrange(self.nrows))
class Func(object):
def __init__(self, name, Vars, args, promise):
self.formal = args
self.promise = promise
self.Vars = Vars.copy()
self.Vars[name] = self
def call(self,actual):
if len(actual) != len(self.formal):
raise TypeError("Wrong number of arguments: given {}, expected {}".format(len(actual), len(self.formal)))
callVars = self.Vars.copy()
callVars.update(zip(self.formal, actual))
return self.promise.get(callVars)
class Calc(tpg.Parser):
r"""
separator spaces: '\s+' ;
separator comment: '#.*' ;
token fnumber: '\d+[.]\d*' PromFloat ;
token number: '-?\d+' PromInt ;
token op1: '[+-]' make_op ;
token op2: '[*/]' make_op ;
token id: '\w+' ;
START/e -> Operator $e=None$ | Expr/e | $e=None$ ;
Operator -> Assign | FuncDecl;
FuncDecl -> 'fun' id/f FormalArgs/a '=' Expr/prom $Vars[f] = Func(f, Vars, a, prom)$;
FormalArgs/a -> ('\(\s*\)' $a=[]$ | '\(' FormalArgList/a '\)') ;
FormalArgList/a -> id/i ',' FormalArgList/a $a=[i] + a$ | id/i $a=[i]$ ;
ActualArgs/a -> ('\(\s*\)' $a=[]$ | '\(' ActualArgList/a '\)') ;
ActualArgList/a -> Atom/i ',' ActualArgList/a $a=[i] + a$ | Atom/i $a=[i]$ ;
Assign -> id/i '=' Expr/e $Vars[i]=e.get(Vars) $ ;
Expr/t -> Fact/t ( op1/op Fact/f $t=PromOp(op, t,f)$ )* ;
Fact/f -> Atom/f ActualArgs/a $f=PromFuncCall(f,a)$ | Atom/f ( op2/op Atom/a $f=PromOp(op, f,a)$ )* ;
Atom/a -> Matrix/a
| id/i $a=PromVar(i)$
| fnumber/a
| number/a
| '\(' Expr/a '\)';
Matrix/$PromMatrix(a)$ -> '\[' '\]' $a=[]$ | '\[' Lines/a '\]' ;
Lines/l -> Atoms/v ';' Lines/l $l=[v]+l$ | Atoms/v $l=[v]$ ;
Atoms/v -> Atom/a Atoms/t $v=[a]+t$ | Atom/a $v=[a]$ ;
"""
calc = Calc()
Vars={}
PS1='--> '
def get_line():
if len(sys.argv) == 2:
# It's script file
with open(sys.argv[1], 'r') as f:
for l in iter(f.readline, ''):
yield l
else:
try:
while True: yield raw_input(PS1)
except EOFError:
print
return
for line in get_line():
try:
res = calc(line)
except tpg.Error as exc:
print >> sys.stderr, exc
res = None
except TypeError as e:
print >> sys.stderr, "Error: " + str(e)
res = None
if res != None:
print res.get(Vars)