Implementation notes Multiquine in C/C++/C++11. ---------------------------------------------------------------------- 0. Concept "Date A Live" has some of the best characters, all very cute and charming in both the novel series and the anime series. Yoshino is my favorite, and I thought it would be great to make a quine that included both Yoshino and Yoshinon. This would be a quine whose output is decided by which compiler is used. C and C++ would be fitting for this task (because C++ is fitting for any task, of course!) Actually, I had this exact same thought back in 2007 when I made youmu.c and yuyuko.c, so this time I added C++11 for variety. C++11 will represent Zadkiel, Yoshino's angel. ---------------------------------------------------------------------- 1. Polyglot Instead of "multiquine", I thought a good marketing slogan might be "a polyglot in one language". I can hear the complaints already: "polyglot, you keep using that word, I do not think it means what you think it means." But it really is all just the same C code, and this is the true genius of Bjarne Stroustrup and the C++ committee members -- it's very difficult to write for the lowest common denominator for C/C++/C++11 and have the code behave differently. A code that is syntactically correct in all three languages will likely behave the same way, any code that behaves differently will likely be using incompatible syntax. Despite the care in making all incompatibilities obvious, there are still some subtle incompatibilities to be exploited. The two differences exploited by Yoshino are: - Character literals are integers in C90 and C99, but are characters in C++ and above, so sizeof('y') returns different values. - Raw string literals are evaluated before macro arguments are expanded, so the following code means different things in C++98 and C++11: #define q(R) R"()" ---------------------------------------------------------------------- 2. Quine After the polyglot bits are done, the next bit is in writing the quine bit. I have done this many times, usually using some XOR logic. This time I used a preprocessor trick so that the quoted and unquoted code is stored only once. Basically it's something like this: #define q(code) #code; code You lose the ability to use commas and comments in the code, but otherwise no big deal. This made Yoshino the most efficiently encoded C quine that I have made so far, almost half the size of Youmu/Yuyuko in terms of non-whitespace characters for three layouts instead of two. I don't know why I have avoided preprocessors for so long. ---------------------------------------------------------------------- 3. Layout The layout bits is using pretty much the same run-length encoding that I have been doing for the past few years: # .. [ = whitespaces ] .. ~ = characters ! = newline Encoding was done using a perl script, which also generated the unformatted generator that would produce all 3 formatted source files. This was a fairly straightforward task of just inserting spaces in the right places until all output looked right. One bit of time saver that I did was to make the encoder pad the encoded templates such that the template size is fixed despite the changing layout. After all three programs have been formatted, I replaced the spare padding bits with some message. Thus the number of readable bytes in the encoded format data is a signal of how conservative I was in choosing the layout. ---------------------------------------------------------------------- 4. Compatibility Yoshino has been tested on multiple compiler and platform combinations. Due to the use of C++11 raw strings, anything older than GCC 4.5 or Clang 3.0 will not work properly. Totally works: - Clang 3.0 on Linux. - GCC 4.6.3 on Linux. - GCC 4.8.2 on Linux. - GCC 4.8.3 on Windows (Cygwin / MingW). Partially works (no Zadkiel): - GCC 4.3.5 on JS/Linux. - GCC 4.4.5 on Linux. Partially doesn't work (doesn't support -std=c++0x): - GCC 4.2.4 on Linux. ---------------------------------------------------------------------- 5. Finally... Yoshino has been a fairly easy program to write, the logic was simple and the functionality was easy to test. Nevertheless, I still get a kick out of making these ASCII art tributes to characters that I like. This is why I make these programs for fun. I had more ambitious plans, which perhaps I will do when I make ASCII art for Tokisaki Kurumi or Tobiichi Origami (my next favorite characters after Yoshino). But ambitious plans would require real engineering a lot more coffee, and I tend to allocate those to my daytime job these days. Maybe some other time...