43 I made a simplistic brainfuck optimizer compiler in rust in about 1 and a half hour.
It goes over the code in 2 passes. The first (which is done in parallel) turns the brainfuck "script" into tokens that are easier to work with. It also filters out everything that is not an instruction.
The second pass, combines instructions that are next to each other. Meaning that +++ becomes Add(3). It also "links" the start and end of every loop. Meaning it can simply become a jump instruction.
There are more optimizations possible like [-] which can become a single instruction to set the current cell to zero and [>]/ [<] which simply move the pointer left/right until it hits an cell containing 0.
But before I start doing any of that I first need to fix the bug that causes the last instruction to not be part of the program and make it so it can actually execute the code. After that I should probably also dig up my old lua version of it to compare the two. I suspect the lua version is slower HOWEVER that only needs 1 pass to optimize it, so it will be interesting to see how that changes things.
I probably should also keep the rust version that isn't multi threaded because that doesn't need to temporary make a Vec after the first pass and thus should be optimized into a single pass, just like my lua code.
It goes over the code in 2 passes. The first (which is done in parallel) turns the brainfuck "script" into tokens that are easier to work with. It also filters out everything that is not an instruction.
The second pass, combines instructions that are next to each other. Meaning that +++ becomes Add(3). It also "links" the start and end of every loop. Meaning it can simply become a jump instruction.
There are more optimizations possible like [-] which can become a single instruction to set the current cell to zero and [>]/ [<] which simply move the pointer left/right until it hits an cell containing 0.
But before I start doing any of that I first need to fix the bug that causes the last instruction to not be part of the program and make it so it can actually execute the code. After that I should probably also dig up my old lua version of it to compare the two. I suspect the lua version is slower HOWEVER that only needs 1 pass to optimize it, so it will be interesting to see how that changes things.
I probably should also keep the rust version that isn't multi threaded because that doesn't need to temporary make a Vec after the first pass and thus should be optimized into a single pass, just like my lua code.