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.
The Language
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 codex
as long as the bit at the referent is 1.
The Challenge
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.
Notes
- You can assume that the program will consist solely of the characters
<>!.^[]
and that[]
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.
Sample Programs
Print 1:
!.
Print 010:
.!.!.
Print 0 forever:
![!.!]
Print 0 forever, or 1 forever if !
is prepended:
[.]![!.!]
1
s on the tape is always finite. In fact, there is a fairly simple bijection between the natural numbers and the tape states (interpret the tape contents as a backwards binary number), which shows that the Hypercube is basically an infinite 1D array, accessed by flipping bits in an integer pointer value, instead of in/decrementing as in brainfuck. \$\endgroup\$cat
program: there doesn't seem to be an instruction for taking input. \$\endgroup\$.
- prints a single zero and then exists;!^!.
- prints a single one then exits. More would be good though. At the moment one must understand submissions in order to verify them (and hence upvote them!) \$\endgroup\$[0,0,0,0,0,0,0...]
(i.e. the presence of a!
at the start of the program). \$\endgroup\$[.]![!.!]
to print the value of that cell forever \$\endgroup\$