7, 2 bytes
The characters that make up this program are:
240223
When viewing the file in an editor, it'll likely try to interpret it as ASCII, in which case it looks like this:
P$
Try it online!
The program takes input. I've assumed here that the input is at EOF; if you provide it input, it's possible it will crash.
Explanation
Zeroth iteration
240223
240223 Append 240223 to the top stack element
The entire program here is passive, so it'll append an active version of itself to the top stack element (which is initially empty). The program's in an implicit loop, which evaluates the top stack element (while leaving it on the stack) every iteration. So the active version will run. (Pretty much all 7 programs start like this.)
First iteration
240223
2 Copy the top stack element
40 Escape the second stack element, swapping it to the top
2 Copy the top stack element
23 Output the top stack element
Before the output command, the stack contains two copies of 240223
(i.e. passive). The top one gets output (with no observable effect other than selecting output format 2, "numbers"), the one below stays and becomes the program for the next iteration.
Second iteration
240223
240223 Append 240223 to the top stack element
The same as the zeroth iteration, right? Not quite; the stack had different contents. The stack is now 240223
below 240223240223
.
Third iteration
240223240223
240223 Append 240223 to the top stack element
2 Copy the top stack element
40 Escape the second stack element, swapping it to the top
2 Copy the top stack element
23 Output the top stack element
This outputs a stack element that has three more 6s-and-0s than it does 7s-and-1s. Output format 2 interprets this as a request to input a number. We read EOF, with the consequence that the next element on the stack is reduced to a zero-length string. However, due to a bug (which seems mildly useful and may be promoted to a feature?), this only happens after it starts executing. The stack once again has two elements, 240223
below 240223240223240223
, but the escaped version of this top element, 72402236240223240223
, is now executing.
Fourth iteration
72402236240223240223
72402236240223240223 Append 240223240223240223 to the top of stack
The top of stack was a zero-length string, so we're basically just unescaping the program that ended up there.
Fifth iteration
240223240223240223
240223 Append 240223 to the top stack element
2 Copy the top stack element
40 Escape the second stack element, swapping it to the top
2 Copy the top stack element
23 Output the top stack element
2 Copy the top stack element
40 Escape the second stack element, swapping it to the top
2 Copy the top stack element
23 Output the top stack element
This is very similar to the third iteration. There are two changes, though. First, there are now 4 more 0s-and-6s than there are 1s-and-7s, so we'll try to input a character from standard input rather than a number. We still get EOF, though, and still end up reducing the top of stack to a zero-length string as a result. Next, there's code running after the reduction, so the next iteration doesn't start immediately. Rather, we do a bunch of operations on that zero-length element, ending up with three zero-length elements. We output one, the other two disappear (because it's the end of an iteration), and we end up basically where we were at the end of the third iteration.
Sixth iteration
The program we copy from the top of stack is now 240223240223240223240223
. Everything works like in the previous iteration until we reach the first output instruction (the first 23
). This now has five more 0s-and-6s than there are 1s-and-7s, so it sets a mode in which the next output instruction will be interpretted as a request to change the output format. This also leads to a notable change in behaviour; as there was no input request, we didn't read EOF, and thus didn't trigger the consequence of reading EOF (the new top stack element being blanked), and as such the escaped version of the original stack element stays rather than being removed. This means that the next 2402
is no longer a no-op, creating two escaped copies of the escaped stack element (i.e. they're now double-escaped). We output one, setting the output format to 7 ("same as the input"); this output format has no I/O commands, and no commands to change the format, so we'll stay in it for the rest of the program. We also output 724022362402232402232402232402236
(in the same encoding as the input) to standard output.
Anyway, it's fairly clear what the program's going to be doing from this point onwards: it's a combination of appending various number of copies of variously escaped 240223
to the top of the stack, escaping the top of the stack, and outputting copies of the top of the stack. From this point onwards, the top stack element is never cleared (because we never read EOF), so it just grows and grows and grows. The periodic escaping ensures that the output never repeats (because it means that each iteration, the first output starts with at least one more 7
than it did on the previous iteration).
1\n12\n123
invalid? \$\endgroup\$