OILS / demo / old / old_code.py View on Github | oilshell.org

114 lines, 46 significant
1"""
2old.py
3
4Saving some old code. mypy --strict doesn't like it!
5"""
6from __future__ import print_function
7
8import sys
9
10
11class DynamicTdopParser(TdopParser):
12 def __init__(self, *args):
13 TdopParser.__init__(self, *args)
14
15 # for precedence tweakins
16 # TODO: Don't peek inside the member? How to represent this in C++?
17 self.dynamic_led = dict(self.spec.led_lookup)
18 self.stack = [] # saved dynamic_led entries
19
20 def Push(self, token, v):
21 """
22 Temporarily adjust precedence of a token, or insert a new token.
23
24 In Python, this is used for commas, because sometimes it has greater
25 precedence than =, and sometimes less. For example:
26
27 x, y = x, y
28 (x, y) = (x, y)
29
30 vs.
31
32 f(x = 1, y = 1)
33
34 This is NOT:
35 f(x = (1, y )= 1)
36
37 So inside f(x,y), (t1, t2), [i, j], {i:1, i:2} we tweak it.
38
39 Why is it used for "in"?
40
41 for x in y: pass
42 # NOT VALID
43 for (x in y) in y:
44
45 [ x for x+1 in y ]
46 [ x for x in y ]
47
48 I think INSTEAD of tweak, we need something that's not an expression? Do
49 this later.
50 """
51 self.stack.append((token, self.dynamic_led[token])) # save old value
52 if v:
53 self.dynamic_led[token] = self.spec.LookupLed(token)
54 else:
55 self.dynamic_led[token] = LeftInfo()
56
57 def Pop(self):
58 """ Restore dynamic_led after p.Push(). """
59 k, v = self.stack.pop()
60 self.dynamic_led[k] = v
61
62 def _Led(self, token):
63 return self.dynamic_led[token]
64
65
66#
67# From osh/braces.py
68#
69
70
71# Possible optimization for later:
72def _TreeCount(tree_word):
73 """Count output size for allocation purposes.
74
75 We can count the number of words expanded into, and the max number of parts
76 in a word.
77
78 Every word can have a different number of parts, e.g. -{'a'b,c}- expands into
79 words of 4 parts, then 3 parts.
80 """
81 # TODO: Copy the structure of _BraceExpand and _BraceExpandOne.
82 for part in tree_word.parts:
83 if isinstance(part, word_part__BracedTuple):
84 for word in part.words:
85 pass
86 num_results = 2
87 max_parts = 5
88 return num_results, max_parts
89
90
91def _Cartesian(tuples):
92 if len(tuples) == 1:
93 for x in tuples[0]:
94 yield (x,)
95 else:
96 for x in tuples[0]:
97 for y in _Cartesian(tuples[1:]):
98 yield (x,) + y # join tuples
99
100
101def main(argv):
102 for t in _Cartesian([('a', 'b')]):
103 print(t)
104 print('--')
105 for t in _Cartesian([('a', 'b'), ('c', 'd', 'e'), ('f', 'g')]):
106 print(t)
107
108
109if __name__ == '__main__':
110 try:
111 main(sys.argv)
112 except RuntimeError as e:
113 print('FATAL: %s' % e, file=sys.stderr)
114 sys.exit(1)