In this challenge, you will write an interpreter for 2Ω (transcribed as TwoMega), a language based loosely on brainfuck with an infinite-dimensional storage space.
2Ω contains three pieces of state:
The Tape, which is an infinite list of bits, all initialized to 0. It has a leftmost element, but no rightmost element.
The Memory Pointer, which is a nonnegative integer that is an index of an element in the tape. A higher memory pointer refers to a tape cell further to the right; a memory pointer of 0 refers to the leftmost element. The memory pointer is initialized to 0.
The Hypercube, which is a conceptually ∞-dimensional "box" of cells, each of which contains a bit initialized to 0. The width of the Hypercube is bound in every dimension to only 2 cells, but the infinity of dimensions means the number of cells is uncountable.
An index into the hypercube is an infinite list of bits that refers to a cell in the hypercube (in the same way that a finite list of bits could be used to refer to a hypercube of finite dimension). Because the tape is an infinite list of bits, the entire tape always refers to an element of the Hypercube; this element is called the referent.
2Ω gives meaning to 7 different characters:
<decrements the memory pointer by 1. Decrementing it below 0 is undefined behavior, so you do not need to handle it.
>increments the memory pointer by 1.
!flips the bit at the referent.
.outputs the bit at the referent.
^replaces the bit at the cell pointed to by the memory pointer on the tape with the inverse of the bit at the referent.
[x]runs the code
xas long as the bit at the referent is 1.
Your task is to write a program that takes a string as input and executes that input as a 2Ω program.
This is code-golf, so the shortest valid answer (measured in bytes) wins.
- You can assume that the program will consist solely of the characters
will be properly nested.
- Your interpreter should only be limited by available memory on the system. It should be able to run the sample programs in a reasonable amount of time.
Print 0 forever:
Print 0 forever, or 1 forever if
! is prepended: