-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.py
75 lines (54 loc) · 1.71 KB
/
tree.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
#!/usr/bin/env python
class typeEnum:
scope, identifier = range(5, 15, 5)
class ObjDesc:
def __init__( self, type, name):
self.type = type
self.name = name
self.scope = None
self.items = None
def __repr__( self):
if self.type == typeEnum.scope:
if self.items:
return 'Scope: "%s" Items: %s ' % (self.name, len(self.items))
else:
return 'Scope: "%s" Empty' % self.name
def __iter__( self):
return ObjDescIterator( self)
class ObjDescIterator:
def __init__( self, obj):
self.obj = obj
def next( self):
if self.obj == None:
raise StopIteration
else:
t = self.obj
self.obj = self.obj.scope
return t
def newScope( name_):
return ObjDesc( type=typeEnum.scope, name=name_)
def newIdentifier( name_):
return ObjDesc( type=typeEnum.identifier, name=name_)
def buildTree( t):
t.items = [ newIdentifier('a'), newIdentifier('b'), newIdentifier('c')]
t.scope = newScope('locals')
t.scope.items = [ newIdentifier('d'), newIdentifier('e')]
def buildList( t):
t[0].items = [ newIdentifier('a'), newIdentifier('b'), newIdentifier('c')]
t.append( newScope('locals'))
t[1].items = [ newIdentifier('d'), newIdentifier('e')]
def scan( head):
print 'Head:'
for t in head:
print t
from itertools import ifilter
def findItem( head, name):
if list( ifilter( lambda x: x.name==name, head)):
print 'Found it'
tree = newScope('root')
buildTree( tree)
scan( tree)
listOfLists = [ newScope( 'root')]
buildList( listOfLists)
scan( listOfLists)
findItem( listOfLists, 'locals')