I have written by own BF Interpreter in Assembly, and now I'm writing a BF Compiler in Java that compiles to Assembly code.
I wanted to implement a little nice feature that detected if the array of memory cells was out of bounds. A traditional limitation for the array is to let the index be in [0, 30000)
, otherwise [0, inf)
is also commonly used. Another option is that memory is wrapped around, so in the first case it could mean that accessing index 30000 means accessing index 0.
In my compiler this detection would be done at the Semantic Analysis phase of a traditional compiler, so we have already obtained our AST (Abstract Syntax Tree) from the Syntax Analysis phase.
After trying to come up with a construct for this for a while I have found Detecting infinite loop in brainfuck program, together with BF's Wikipedia page I have found out that a BF program with as memory array [0, inf)
resembles a Turing Machine.
So my question is, for both the [0, max)
and [0, inf)
cases, is it possible to detect if the memory pointer goes below zero, and in the former case below max? Obviously without ending up in an infinite loop while checking it, and I'd rather avoid setting a maximum execution time as well.
Bonus question: Is it possible to detect the amount of times a loop construct [...]
loops?