Thinking about various quine puzzles here, I got an idea for another one:
Compose a program that outputs its own compiled code to a file (or multiple files, if the compiled code is). (This means that only compiled languages are eligible.)
Standard quine rules - no cheating allowed. In particular:
- The program cannot read the compiled code from disk, cannot store it somewhere etc. It must compute it somehow without external help.
- If the program is compiled to several files (i.e. several Java
.classfiles), it must output them all.
- You can use any existing libraries available for your language. However:
- The libraries you use cannot be something created just for this task. (For example in order to keep some information away from what's considered to be the program.)
- Any libraries you use must be older than your program. You cannot just create a program that loads a piece of data from an external library, compile the program, and copy the compiled code into the library - that'd be cheating.
- You cannot embed a compiler of your language in any form. In particular, you cannot just take a standard quine and hook it up with a compiler to produce the compiled output.
- As mentioned, only compiled languages are eligible. You can use a scripting language as long as it can be compiled to a bytecode that's reasonably different from the source form (if you're not sure, better ask).
Most likely, this puzzle can be solved reasonably only in languages that compile to some sort of bytecode that can be manipulated. For example, Java and compiled bytecode seems good for the task. (But if you find a genuine solution in some other compiled language, that's fine too, as long as it isn't some sort of cheating.)
Don't submit the compiled program (as this could be potentially dangerous). Instead post your sources and a description what to do to create the final program.
Let voters decide the best solution. Personally I'd appreciate solution that are elegant, short (in their source form) and didactic - that explain what's going on.
Bonus points for
- Let the program output a compressed file containing its compiled code. This has the additional benefit that if the program is compiled into more files, the output will be a single packed file. For example, in Java try to create an executable JAR file that outputs the JAR itself.
- Outputting the MD5 sum of the compiled file(s) - so the program would write its compiled code and its MD5 sum.