-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
334 lines (288 loc) · 9.42 KB
/
main.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
import ast
import sys
import textwrap
class Function:
def __init__(self, name, program):
self.program = program
self.waitfor = 'main'
self.name = name
self.buffer = []
self.arg_count = 0
def next_window_index(self):
return len(self.buffer)
def write(self, contents, index=None):
if contents is None:
self.buffer.append(None)
elif index is None:
self.buffer.append(textwrap.dedent(contents))
elif self.buffer[index] is not None:
raise Exception("Invalid instruction overwrite")
else:
self.buffer[index] = textwrap.dedent(contents)
return len(self.buffer) - 1
def to_string(self):
return '\n'.join(self.buffer)
def new_window(self, body, index = None):
index = index or self.next_window_index()
target = self.name + ':{end}'
colour = 16 + (index % (231 - 16))
return textwrap.dedent("""\
# index: %s
new-window -a -t '%s' 'tmux wait-for %s'
set -w -t '%s' window-style bg=colour%s
set-hook -t '%s' pane-focus-in {\
""" % (index, target, self.waitfor, target, colour, target) + body) + '}\n'
def push_constant(self, value, index=None):
self.write(self.new_window("""
run "tmux set-buffer '%s'"
run 'tmux next-window'
""" % value, index), index)
def pop_stack(self):
self.write(self.new_window("""
run "tmux delete-buffer"
run 'tmux next-window'
"""))
def store_top(self):
index = self.next_window_index()
self.write(self.new_window("""
run 'tmux rename-window -t :=%s "#{buffer_sample}"'
run 'tmux delete-buffer'
run 'tmux next-window'
""" % (index + 1), index))
def maths_op(self, op):
self.store_top()
index = self.next_window_index()
self.write(self.new_window("""
run 'tmux rename-window -t :=%s "#{e|%s:#{buffer_sample},#{window_name}}"'
run 'tmux delete-buffer'
run 'tmux set-buffer "#{window_name}"'
run 'tmux next-window'
""" % (index, op), index))
def print(self):
self.write(self.new_window("""
run 'tmux display -F "#{buffer_sample}"'
run 'tmux delete-buffer'
run 'tmux next-window'
"""))
def print_str(self, text):
self.write(self.new_window("""\
run 'tmux display -F "%s"'
run 'sleep 2'
run 'tmux next-window'
""" % text))
def cond(self, invert=False):
instruction_index = self.next_window_index()
pos = self.write(None)
def add_with_indexes_later():
indexes = (instruction_index + 1, self.next_window_index())
if invert:
indexes = tuple(reversed(indexes))
self.write(self.new_window("""
run 'tmux select-window -t "#{?#{buffer_sample},:=%s,:=%s}"'
run 'tmux delete-buffer'
""" % indexes, pos), pos)
return add_with_indexes_later
def set_variable(self, name):
self.write(self.new_window("""
run 'tmux set-option -s '@%s' "#{buffer_sample}"'
run 'tmux delete-buffer'
run 'tmux next-window'
""" % name))
def get_variable(self, name):
self.write(self.new_window("""
run 'tmux set-buffer "#{@%s}"'
run 'tmux next-window'
""" % name))
def switch_session(self, name):
self.write(self.new_window("""
run 'tmux switch-client -t %s:1'
""" % name))
def switch_back(self):
self.write(self.new_window("""
# the value to return
run 'tmux rename-session -- "#{buffer_sample}"'
run 'tmux delete-buffer'
# the location to return to
run 'tmux rename-window -- "#{buffer_sample}"'
run 'tmux delete-buffer'
# put return value back on stack
run 'tmux set-buffer "#S"'
# restore session name
run 'tmux rename-session -- "%s"'
run 'tmux switch-client -t "#{window_name}"'
""" % self.name))
def push_return(self):
index = self.write(None)
def cb():
self.push_constant(self.name + ':' + str(self.next_window_index()), index)
return cb
def goto(self):
index = self.write(None)
def doit():
self.write(self.new_window("""
run 'tmux select-window -t :=%s'
""" % self.next_window_index(), index), index)
return doit
def jump(self, index):
self.write(self.new_window("""
run 'tmux select-window -t :=%s'
""" % index))
class Program:
def __init__(self):
self.functions = dict()
func = self.main = self.add_func('main')
func.write("""\
# FUNCTION: %s
new-session -d -s %s 'tmux wait-for %s'
""" % (func.name, func.name, func.waitfor))
def add_func(self, name):
if name in self.functions:
raise Exception("function already defined: " + name)
func = Function(name, self)
self.functions[func.name] = func
return func
def to_string(self):
GLOBAL = textwrap.dedent('''
set -g display-time 2000
set -g focus-events on
''')
FOOTER = '\nattach -t main'
return GLOBAL + '\n'.join(f.to_string() for f in self.functions.values()) + FOOTER
def compile_if(self, func):
self.test.compile_to(func)
cb = func.cond()
for expr in self.body:
expr.compile_to(func)
func.pop_stack()
goto = func.goto()
cb()
for expr in self.orelse:
expr.compile_to(func)
func.pop_stack()
goto()
func.push_constant(0)
ast.If.compile_to = compile_if
def compile_boolop(self, func):
if len(self.values) != 2:
raise Exception("oh no boolop!")
lhs = self.values[0]
rhs = self.values[1]
raise Exception("not implemented")
# if type(self.op) is ast.And:
# # compile_and(func, lhs, rhs)
# elif type(self.op) is ast.Or:
# # compile_or(func, lhs, rhs)
# else:
# raise Exception("unknown op:", self)
ast.BoolOp.compile_to = compile_boolop
def compile_while(self, func):
startindex = func.next_window_index()
self.test.compile_to(func)
cb = func.cond()
for expr in self.body:
expr.compile_to(func)
func.pop_stack()
func.jump(startindex)
cb()
func.push_constant(0)
ast.While.compile_to = compile_while
def compile_compare(self, func):
self.left.compile_to(func)
if len(self.comparators) != 1:
raise Exception("only compare one thing!")
self.comparators[0].compile_to(func)
if len(self.ops) != 1:
raise Exception("only compare one op!")
self.ops[0].compile_to(func)
ast.Compare.compile_to = compile_compare
def compile_func(self, program):
func = program.add_func(self.name)
func.write("""\
# FUNCTION: %s
new-session -d -s %s 'tmux wait-for %s'
""" % (func.name, func.name, func.waitfor))
# args added to stack from first to last
for arg in reversed(self.args.args):
func.arg_count += 1
func.set_variable(arg.arg)
for expr in self.body:
expr.compile_to(func)
func.pop_stack()
if type(self.body[-1]) is not ast.Return:
func.push_constant(0)
func.switch_back()
func.write('select-window -t :=0')
ast.FunctionDef.compile_to = compile_func
def compile_return(self, func):
self.value.compile_to(func)
func.switch_back()
ast.Return.compile_to = compile_return
def compile_assign(self, func):
if len(self.targets) != 1:
raise Exception("only one assignment target pls")
self.value.compile_to(func)
name = self.targets[0].id
func.set_variable(name)
func.push_constant(0)
ast.Assign.compile_to = compile_assign
def compile_name(self, func):
func.get_variable(self.id)
ast.Name.compile_to = compile_name
ast.Eq.compile_to = lambda self, func: func.maths_op('==')
ast.NotEq.compile_to = lambda self, func: func.maths_op('!=')
ast.Mod.compile_to = lambda self, func: func.maths_op('%')
ast.Lt.compile_to = lambda self, func: func.maths_op('<')
ast.Gt.compile_to = lambda self, func: func.maths_op('>')
ast.Add.compile_to = lambda self, func: func.maths_op('+')
ast.Sub.compile_to = lambda self, func: func.maths_op('-')
ast.Mult.compile_to = lambda self, func: func.maths_op('*')
ast.Div.compile_to = lambda self, func: func.maths_op('/')
def compile_const(self, func):
func.push_constant(self.value)
ast.Constant.compile_to = compile_const
def compile_binop(self, func):
self.left.compile_to(func)
self.right.compile_to(func)
self.op.compile_to(func)
ast.BinOp.compile_to = compile_binop
def compile_call(self, func):
if self.func.id == 'print':
if len(self.args) != 1:
raise Exception("print only takes one argument")
for arg in self.args:
arg.compile_to(func)
func.print()
func.push_constant('0')
return
if self.func.id not in func.program.functions:
raise Exception("unknown func: " + self.func.id)
newfunc = func.program.functions[self.func.id]
if newfunc.arg_count != len(self.args):
raise Exception("Wrong number of args to call {} got {} expected {}".format(
self.func.id, len(self.args), newfunc.arg_count))
on_return = func.push_return()
for arg in self.args:
arg.compile_to(func)
func.switch_session(self.func.id)
on_return()
ast.Call.compile_to = compile_call
def compile_expr(self, func):
self.value.compile_to(func)
ast.Expr.compile_to = compile_expr
def compile_module(self, program):
for expr in self.body:
if type(expr) is ast.FunctionDef:
expr.compile_to(program)
else:
expr.compile_to(program.main)
program.main.pop_stack()
program.main.write('select-window -t :=0')
ast.Module.compile_to = compile_module
p = Program()
with open(sys.argv[1]) as f:
tree = ast.parse(f.read())
print(ast.dump(tree, indent=2))
tree.compile_to(p)
print(p.to_string())
with open(sys.argv[2], 'wt') as o:
o.write(p.to_string())