Skip to content

Commit

Permalink
I think this works
Browse files Browse the repository at this point in the history
  • Loading branch information
willhbr committed Mar 14, 2024
1 parent f505a2f commit 36bc61c
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 38 deletions.
5 changes: 4 additions & 1 deletion brainfuck.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ def compile(program):
elif char == ',':
output.read()
elif char == '[':
# TODO: this won't work if there are other characters in the input
output.jump(pairs[i], True)
elif char == ']':
output.jump(pairs[i], False)
output.append('select-window -t :=0')
return output.to_string()


program = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.'
with open(sys.argv[1]) as f:
program = f.read()

comp = compile(program)
print(comp)
with open(sys.argv[1], 'wt') as o:
Expand Down
9 changes: 9 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,14 @@ def compile_if(self, 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):
Expand All @@ -201,8 +204,10 @@ def compile_while(self, 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):
Expand Down Expand Up @@ -231,6 +236,7 @@ def compile_func(self, program):
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
Expand All @@ -246,6 +252,7 @@ def compile_assign(self, func):
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):
Expand All @@ -254,6 +261,7 @@ def compile_name(self, func):

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('+')
Expand Down Expand Up @@ -303,6 +311,7 @@ def compile_module(self, program):
expr.compile_to(program)
else:
expr.compile_to(program.main)
program.main.pop_stack()

program.main.write('select-window -t :=0')

Expand Down
12 changes: 9 additions & 3 deletions program.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
def func(a):
return a * 2
def is_prime(num):
i = 2
while i < num:
if num % i == 0:
return 0
i = i + 1

print(func(5))
return 1

print(is_prime(13))
Loading

0 comments on commit 36bc61c

Please sign in to comment.