-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
189 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import textwrap | ||
import sys | ||
|
||
class Program: | ||
def __init__(self): | ||
self.buffer = [] | ||
self.index = 0 | ||
self.waitfor = 'main' | ||
self.append("new-session -d 'read && tmux wait-for -S %s'" % self.waitfor) | ||
self.append("set -s focus-events on") | ||
self.append('set -s @input "$INPUT"') | ||
def next_window_index(self): | ||
self.index += 1 | ||
return self.index | ||
def to_string(self): | ||
return '\n'.join(self.buffer) | ||
def new_window(self, body, index = None): | ||
index = index or self.next_window_index() | ||
target = ':{end}' | ||
return textwrap.dedent("""\ | ||
# instruction number %s | ||
new-window -a -t '%s' 'tmux wait-for %s' | ||
set-hook -t '%s' pane-focus-in {\ | ||
""" % (index, target, self.waitfor, target) + body) + '}\n' | ||
def append(self, text): | ||
self.buffer.append(textwrap.dedent(text)) | ||
|
||
def move_right(self): | ||
self.append(self.new_window(""" | ||
run 'tmux rename-session "#{e|+:#S,1}"' | ||
run 'tmux next-window' | ||
""")) | ||
def move_left(self): | ||
self.append(self.new_window(""" | ||
run 'tmux rename-session "#{e|-:#S,1}"' | ||
run 'tmux next-window' | ||
""")) | ||
|
||
def inc(self): | ||
self.append(self.new_window(""" | ||
run 'tmux set -s "@data-#S" "#{e|+:#{E:##{@data-#S#}},1}"' | ||
run 'tmux next-window' | ||
""")) | ||
def dec(self): | ||
self.append(self.new_window(""" | ||
run 'tmux set -s "@data-#S" "#{e|-:#{E:##{@data-#S#}},1}"' | ||
run 'tmux next-window' | ||
""")) | ||
|
||
def read(self): | ||
self.append(self.new_window(""" | ||
run 'tmux set -s "@data-#S" "#{=1:@input}"' | ||
run 'tmux set -s "@input" "#{=-#{e|-:#{n:@input},1}:#{?#{e|==:#{n:@input},1},0,#{@input}}}"' | ||
run 'tmux next-window' | ||
""")) | ||
def write(self): | ||
self.append(self.new_window(""" | ||
run 'tmux send-keys -t ":=0" "#{E:##{@data-#S#}}"' | ||
run 'tmux set -s "@output" "#{@output}#{E:##{@data-#S#}}"' | ||
run 'tmux next-window' | ||
""")) | ||
|
||
def jump(self, position, foreward): | ||
if foreward: | ||
nz, z = self.index + 2, position + 1 | ||
else: | ||
nz, z = position, self.index + 2 | ||
|
||
self.append(self.new_window(""" | ||
run 'tmux select-window -t ":=#{?#{E:##{@data-#S#}},%s,%s}"' | ||
""" % (nz, z))) | ||
|
||
|
||
def compile(program): | ||
output = Program() | ||
stack = [] | ||
pairs = dict() | ||
for i, char in enumerate(program): | ||
if char == '[': | ||
stack.append(i) | ||
elif char == ']': | ||
o = stack.pop() | ||
pairs[o] = i + 1 | ||
pairs[i] = o + 1 | ||
if len(stack) != 0: | ||
raise Exception("mismatching brackets") | ||
for i, char in enumerate(program): | ||
if char == '+': | ||
output.inc() | ||
elif char == '-': | ||
output.dec() | ||
elif char == '>': | ||
output.move_right() | ||
elif char == '<': | ||
output.move_left() | ||
elif char == '.': | ||
output.write() | ||
elif char == ',': | ||
output.read() | ||
elif char == '[': | ||
output.jump(pairs[i], True) | ||
elif char == ']': | ||
output.jump(pairs[i], False) | ||
output.append('select-window -t :=0') | ||
return output.to_string() | ||
|
||
|
||
program = ',+>,+><<.>.' | ||
comp = compile(program) | ||
print(comp) | ||
with open(sys.argv[1], 'wt') as o: | ||
o.write(comp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,85 @@ | ||
new-session -d 'read && tmux wait-for -S main' | ||
set -s focus-events on | ||
set -s @input "$INPUT" | ||
# instruction number 1 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux set -s "@data-#S" "#{=1:@input}"' | ||
run 'tmux set -s "@input" "#{=-#{e|-:#{n:@input},1}:#{?#{e|==:#{n:@input},1},0,#{@input}}}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
set -g display-time 2000 | ||
set -g focus-events on | ||
|
||
# FUNCTION: main | ||
|
||
# Instruction num: 0 | ||
new-session -d -s main 'read && tmux wait-for -S main' | ||
|
||
# instruction number 2 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux set -s "@data-#S" "#{e|+:#{E:##{@data-#S#}},1}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
# Instruction num: 1 | ||
new-window -a -t 'main:{end}' 'tmux wait-for main' | ||
set-hook -t 'main:{end}' pane-focus-in { | ||
run "tmux set-buffer '2'" | ||
# instruction number 3 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux rename-session "#{e|+:#S,1}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
# instruction number 4 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux set -s "@data-#S" "#{=1:@input}"' | ||
run 'tmux set -s "@input" "#{=-#{e|-:#{n:@input},1}:#{?#{e|==:#{n:@input},1},0,#{@input}}}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
# Instruction num: 2 | ||
new-window -a -t 'main:{end}' 'tmux wait-for main' | ||
set-hook -t 'main:{end}' pane-focus-in { | ||
run "tmux set-buffer '3'" | ||
# instruction number 5 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux set -s "@data-#S" "#{e|+:#{E:##{@data-#S#}},1}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
# instruction number 6 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux rename-session "#{e|+:#S,1}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
# Instruction num: 3 | ||
new-window -a -t 'main:{end}' 'tmux wait-for main' | ||
set-hook -t 'main:{end}' pane-focus-in { | ||
run 'tmux rename-window -t :=4 "#{buffer_sample}"' | ||
run 'tmux delete-buffer' | ||
# instruction number 7 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux rename-session "#{e|-:#S,1}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
# instruction number 8 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux rename-session "#{e|-:#S,1}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
# Instruction num: 4 | ||
new-window -a -t 'main:{end}' 'tmux wait-for main' | ||
set-hook -t 'main:{end}' pane-focus-in { | ||
run 'tmux rename-window -t :=4 "#{e|*:#{buffer_sample},#{window_name}}"' | ||
run 'tmux delete-buffer' | ||
run 'tmux set-buffer "#{window_name}"' | ||
# instruction number 9 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux send-keys -t ":=0" "#{E:##{@data-#S#}}"' | ||
run 'tmux set -s "@output" "#{@output}#{E:##{@data-#S#}}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
# instruction number 10 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux rename-session "#{e|+:#S,1}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
# Instruction num: 5 | ||
new-window -a -t 'main:{end}' 'tmux wait-for main' | ||
set-hook -t 'main:{end}' pane-focus-in { | ||
run 'tmux display -F "#{buffer_sample}"' | ||
run 'tmux delete-buffer' | ||
# instruction number 11 | ||
new-window -a -t ':{end}' 'tmux wait-for main' | ||
set-hook -t ':{end}' pane-focus-in { | ||
run 'tmux send-keys -t ":=0" "#{E:##{@data-#S#}}"' | ||
run 'tmux set -s "@output" "#{@output}#{E:##{@data-#S#}}"' | ||
run 'tmux next-window' | ||
} | ||
|
||
attach -t main | ||
select-window -t :=0 |