-
Notifications
You must be signed in to change notification settings - Fork 7
/
flclasses.py
335 lines (263 loc) · 9.21 KB
/
flclasses.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
from __future__ import print_function
from builtins import str
from builtins import object
debug = 0
class cBase(object):
def __init__(self):
self.type = ("Unknown","Unknown")
self.codedepth = 0
def setSubtype(self,newsubtype):
x, y = self.type
self.type = (x,newsubtype)
def setType(self,newtype):
x, y = self.type
self.type = (newtype,y)
def addCodeDepth(self):
self.codedepth += 1
def __len__(self):
return 1;
class cBaseItem(cBase):
def __init__(self,value):
cBase.__init__(self)
self.type = ("Item","Unknown")
self.value = value
def __str__(self):
if debug > 0 and debug < 1 and self.type == ("Item","Unknown"): return "*" + str(self.value) + "?"
return str(self.value)
class cBaseItemList(cBase):
def __init__(self,itemList,prefix,suffix,subtype="Unknown"):
cBase.__init__(self)
self.type = ("ItemList",subtype)
if not isinstance(itemList,cBaseList):
iList = cBaseListInline()
iList.addAuto(itemList,subtype=subtype)
itemList = iList
t,st = itemList.slice[0].type
#itemList.setSubtype(st)
itemList.setSubtype("OneItem")
if not isinstance(itemList,cBaseList):
raise NameError("itemList no es un cBaseList: %s" % repr(itemList))
self.itemList = itemList
self.prefix = prefix
self.suffix = suffix
if subtype=="Declaration":
for item in self.itemList.slice:
ctype, csubtype = item.type
ctype = subtype
item.type = (ctype, csubtype)
def __str__(self):
global debug
txt = str(self.prefix) + str(self.itemList) + str(self.suffix)
txt = txt.strip()
if debug>=2:
txt = "{%s/%s:" % self.itemList.type + txt + "}"
return txt
class cBaseVarSpec(cBase):
def __init__(self,name,vartype=None,value=None):
cBase.__init__(self)
self.type = ("Item","Variable")
self.value = value
self.vartype = vartype
self.name = name
def __str__(self):
txt=self.name
if self.vartype: txt+=":"+self.vartype
if self.value: txt+="="+str(self.value)
return txt
class cBaseList(cBase):
def __init__(self):
cBase.__init__(self)
self.type = ("List","Unknown")
self.slice = []
self.hidden = []
self.byType = {}
self.bySubtype = {}
self.byDefName = {}
def __len__(self):
return len(self.slice);
def includeItem(self,child):
if not isinstance(child,cBase):
raise NameError("Child is not an instance of Base Class!")
try:
ctype, csubtype = child.type
except:
raise NameError("Base Class doesn't have `type` atribute or is incorrect.")
if not hasattr(self.byType,ctype):
self.byType[ctype]=[]
if not hasattr(self.bySubtype,ctype):
self.bySubtype["%s:%s" % (ctype,csubtype)]=[]
self.byType[ctype].append(child)
self.bySubtype["%s:%s" % (ctype,csubtype)].append(child)
if ctype == "Declaration":
try:
cname = child.name
except:
raise NameError("Declaration Class doesn't have `name` atribute.")
if cname in self.byDefName:
print("#WARNING# Variable %s found, but previously defined in this block" % cname)
# self.byDefName[cname]=None
else:
self.byDefName[cname]=child
try:
child.addCodeDepth()
except:
print(repr(child))
raise NameError("Base Class doesn't have `addCodeDepth` function.")
if isinstance(child,cBaseItemList):
sslice = child.itemList.slice[:]
for e in child.itemList.hidden:
sslice.remove(e)
for item in sslice:
itype, isubtype = item.type
if not isinstance(item,cBaseItemList):
self.includeItem(item)
#self.hidden.append(item)
def addCodeDepth(self):
cBase.addCodeDepth(self)
sslice = self.slice[:]
for e in self.hidden:
sslice.remove(e)
for child in sslice:
child.addCodeDepth()
def addAuto(self,element,subtype=None):
if not isinstance(element,cBase):
element = cBaseItem(element)
if subtype:
element.setSubtype(subtype)
self.addChild(element)
def addChild(self,child,hidden=False):
self.includeItem(child)
self.slice.append(child)
if hidden: self.hidden.append(child)
def __str__(self):
global debug
sslice = self.slice[:]
for e in self.hidden:
sslice.remove(e)
if debug>=1:
txt = "\n"
# --------- DEBUG OUTPUT VARDECL ---------
if len(self.byDefName)>0:
txt += " " * self.codedepth + " /** Declared vars: "
for definition in self.byDefName:
txt += definition + "; "
txt += "**/\n"
n=0
for c in sslice:
t1, t2 = c.type
n+=1
txt += "%-17s:%d" % ("%-8s/%-8s" % (t1[:8],t2[:8]),n)+ ">" + ". " * c.codedepth + str(c)
txt += "\t<%d:\n" % n
return txt
if len(sslice) == 1:
c = str(sslice[0])
if "\n" not in c:
return " " + c + " "
txt = "\n"
#for child in self.slice:
# if str(child.type[0]) != "ItemList":
# txt += str(child.type) + "\n"
# txt += str(child) + "\n"
# txt += "____"+ "\n"
lastmargin = 0
for c in sslice:
t1, t2 = c.type
line = " " + str(c).replace("\n","\n ")
linecount = line.count("\n")
margin = 0
if linecount>2: margin += 1
if linecount>25: margin += 1
if linecount>50: margin += 1
topmargin = margin - lastmargin
if topmargin < 0: topmargin = 0
txt += "\n" * topmargin + line + "\n" * (margin+1)
lastmargin = margin
return txt
class cBaseListInline(cBaseList):
def __init__(self,separator=", "):
cBaseList.__init__(self)
self.separator = separator
def __str__(self):
global debug
txt = ""
for c in self.slice:
if debug>=3:
txt += "[%s/%s: " % c.type
txt += str(c)
if debug>=3:
txt += "]"
txt += self.separator
if len(self.separator) == 0:
return txt
else:
return txt[:-len(self.separator)]
class cStatementList(cBaseList):
def __init__(self):
cBaseList.__init__(self)
self.type = ("List","Statement")
class cBaseDecl(cBase):
def __init__(self,name):
cBase.__init__(self)
self.type = ("Declaration","Unknown")
self.name = name
def __str__(self):
return "@unknown declaration %s" % self.name
class cFuncDecl(cBaseDecl):
def __init__(self,name,arglist,rettype,source):
cBaseDecl.__init__(self,name=name)
self.type = ("Declaration","Function")
self.arglist = arglist
self.rettype = rettype
if not isinstance(source,cBaseList):
iList = cBaseList()
iList.addAuto(source)
source = iList
if not isinstance(source,cBaseList):
raise NameError("source no es un cBaseList: %s" % repr(source))
self.source = source
def addCodeDepth(self):
cBase.addCodeDepth(self)
try:
self.source.addCodeDepth()
except:
import traceback,sys
print("Exception in user code:")
print('-'*60)
traceback.print_exc(file=sys.stdout)
print('-'*60)
def __str__(self):
if self.rettype:
ret=" : " + self.rettype
else:
ret=""
return 'function %s(%s)%s {%s}' % (self.name,self.arglist,ret,self.source)
class cClassDecl(cBaseDecl):
def __init__(self,name,extends,source):
cBaseDecl.__init__(self,name=name)
self.type = ("Declaration","Class")
self.extends = extends
self.childclasses = None
self.constructor = None
if not isinstance(source,cBaseList):
iList = cBaseList()
iList.addAuto(source)
source = iList
if not isinstance(source,cBaseList):
raise NameError("source no es un cBaseList: %s" % repr(source))
self.source = source
def addCodeDepth(self):
cBase.addCodeDepth(self)
try:
self.source.addCodeDepth()
except:
import traceback,sys
print("Exception in user code:")
print('-'*60)
traceback.print_exc(file=sys.stdout)
print('-'*60)
def __str__(self):
if self.extends:
ext = " extends " + self.extends
else:
ext = ""
return 'class %s%s {%s}' % (self.name,ext,self.source)