Skip to content

Commit

Permalink
Add brainfuck compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
willhbr committed Mar 11, 2024
1 parent 6d86cab commit 95013dd
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 81 deletions.
112 changes: 112 additions & 0 deletions brainfuck.py
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)
13 changes: 13 additions & 0 deletions pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ containers:
- python
- main.py
workdir: /src
bf:
name: tmux-compiler-dev
image: docker.io/library/python:latest
interactive: true
autoremove: true
bind_mounts:
# binding the source directory lets us re-run changes without rebuilding
.: /src
run_flags:
entrypoint:
- python
- brainfuck.py
workdir: /src
97 changes: 64 additions & 33 deletions test.conf
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
48 changes: 0 additions & 48 deletions tmux.conf

This file was deleted.

0 comments on commit 95013dd

Please sign in to comment.