OILS / opy / _regtest / src / opy / compiler2 / misc.py View on Github | oilshell.org

88 lines, 64 significant
1
2def flatten(tup):
3 elts = []
4 for elt in tup:
5 if isinstance(elt, tuple):
6 elts = elts + flatten(elt)
7 else:
8 elts.append(elt)
9 return elts
10
11class Set:
12 def __init__(self):
13 self.elts = {}
14 def __len__(self):
15 return len(self.elts)
16 def __contains__(self, elt):
17 return elt in self.elts
18 def add(self, elt):
19 self.elts[elt] = elt
20
21 def elements(self):
22 # BUG FIX: bytecode is NON-DETERMINISTIC without this.
23 #
24 # This fixes ./smoke.sh opy-determinism-loop. It runs for 100
25 # iterations successfully with this fix. It always fails within 100
26 # without this fix.
27
28 # I thought that Python 2.7's dictionary order was arbitrary but
29 # deterministic. PYTHONHASHSEED=random is opt-in; the default should
30 # be PYTHONHASHSEED=0.
31
32 # TODO: What happens if we get rid of Set() and use the builtin set()?
33
34 #return self.elts.keys()
35 return sorted(self.elts.iterkeys())
36
37 def has_elt(self, elt):
38 return elt in self.elts
39 def remove(self, elt):
40 del self.elts[elt]
41 def copy(self):
42 c = Set()
43 c.elts.update(self.elts)
44 return c
45
46class Stack:
47 def __init__(self):
48 self.stack = []
49 self.pop = self.stack.pop
50 def __len__(self):
51 return len(self.stack)
52 def push(self, elt):
53 self.stack.append(elt)
54 def top(self):
55 return self.stack[-1]
56 def __getitem__(self, index): # needed by visitContinue()
57 return self.stack[index]
58
59MANGLE_LEN = 256 # magic constant from compile.c
60
61def mangle(name, klass):
62 if not name.startswith('__'):
63 return name
64 if len(name) + 2 >= MANGLE_LEN:
65 return name
66 if name.endswith('__'):
67 return name
68 try:
69 i = 0
70 while klass[i] == '_':
71 i = i + 1
72 except IndexError:
73 return name
74 klass = klass[i:]
75
76 tlen = len(klass) + len(name)
77 if tlen > MANGLE_LEN:
78 klass = klass[:MANGLE_LEN-tlen]
79
80 return "_%s%s" % (klass, name)
81
82def set_filename(filename, tree):
83 """Set the filename attribute to filename on every node in tree"""
84 worklist = [tree]
85 while worklist:
86 node = worklist.pop(0)
87 node.filename = filename
88 worklist.extend(node.getChildNodes())