Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Created August 9, 2014 14:46
Show Gist options
  • Save Ratstail91/6a70783261a43313142e to your computer and use it in GitHub Desktop.
Save Ratstail91/6a70783261a43313142e to your computer and use it in GitHub Desktop.
This is an incomplete implementation of the ackermann function written in the brainfuck language.
ack(m, n):
m == 0 ? n + 1 : ;
n == 0 ? ack(m - 1, 1) : ;
ack(m, ack(m, n - 1))
Each stack frame is structured as (a,n,m,x), each referred to as arguments (@x). Everything to the right of the following stack frame is essentially a local variable, and must be cleared before the current frame "returns". Each stack frame is refered to as s@x where s is the current frame, s+1@x is the next, etc. Always assume the pointer will enter the next stage of the function by pointing to the top of a stack frame.
A way to test the precise value of a cell is like this. Assuming the cell @x has the value of 2:
[- start value minus one
[- start value minus two
[ start value is greater that two
this block executes if and only if the initial value is greater than two and can be used to counter the rest of the block
]
this can be used to preform varous actions but it must be reversable by the above block
I recommend leaving the pointer on the initial cell
+]
+]
this code can be used as a pseudo conditional.
@1 is a frame's return value, and is only used after it is finished. The value of @4 is important, and indicates what is to happen next.
brainfuck ackermann function:
>>,<,>>+ store the initial stack frame as 0 n m 1
[ @4
COMMENTS
two successive stack frames could exist in an intermediary state
if they do they are equal to
0 @m 1 0
0 @m @n-1 0
the second can be called and will return easily
the first must have either 2@m reduced by one or 2@n set to the return value of the nested call
@4 dictates what to do
0: ???
1: ???
2: an unnested call has been completed and must be cleaned up
3: an unnested call move down one frame
4: a nested call has completed and must be cleaned up
5: a nested call move down two stack frames
END COMMENTS
process @4
[- at least one
[- at least two
[- at least three
[- at least four
[- at least five
set back to four
++++
move two stack frames for a nested call
TODO
]
clean up the nested call
TODO
]
setup the arguments and move down one frame
TODO
]
clean up an unnested call
TODO
]
prepare to return
TODO
]
CREATE NEW INTERMEDIATE STACK FRAMES
<[> @m
<<[>> @n
Create a stack frame for a nested call to determine the next @n
copy @m
<[>>>> >>>> +>+< <<<< <<<< -]> moved @m to 3@m and 3@4 and leave ptr sitting on @4
>>>> >>>> [<<<< <<<< <+ >>>> >>>> >-] <<<< <<<< move 3@4 back to @m and leave ptr sitting on @4
copy @n
<<[>>>> >>>> +>>+<< <<<< <<<< -]>> moved @n to 3@n and 3@4 and leave ptr sitting on @4
>>>> >>>> [<<<< <<<< <<+ >>>> >>>> >>-] <<<< <<<< move 3@4 back to @n and leave ptr sitting on @4
reduce the 3@n by one
>>>> >>>> <<->> <<<< <<<<
give the instruction for a nested call
++
<<]>> @n
create a stack frame for an unnested call
copy @m
<[>>>> +>+< <<<< -]> moved @m to 2@m and 2@4 and leave ptr sitting on @4
>>>> [<<<< <+ >>>> >-] <<<< move 2@4 back to @m and leave ptr sitting on @4
set 2@n to one
>>+<<
give the instruction for an unnested call
++
<]> @m
if @4 is zero then return?
TODO
] @4
@Ratstail91
Copy link
Author

There's a - sign in the comments, that I missed. I'll need to remove the comments anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment