# Write a third order quine

This challenge is an extension of 'Golf a mutual quine'.

Using three languages of your choice, create a third order Ouroboros program.

That is, in language A, write a program pA which outputs program pB in language B. Program pB should output program pC in language C, which in turn outputs the original program pA in language A.

No two languages in A, B, and C can be the same or subsets or supersets of each other. None of pA, pB or pC may be identical.

For example, a Haskell program outputs a Perl program which outputs a java program which outputs the original Haskell program would be valid.

On the other hand, a C# 4 program which outputs a C# 3 program which outputs a C# 2 program is invalid. Even a Perl -> Ruby -> Bash combination would be invalid if, say, the Perl program and the Ruby program were identical.

This is code golf, so the shortest program pA wins.

## Python -> Perl -> Ruby, 48 characters

s='print q<puts %%q{s=%r;print s%%s}>';print s%s


with Python generates this Perl snippet

print q<puts %q{s='print q<puts %%q{s=%r;print s%%s}>';print s%s}>


which generates the following Ruby code

puts %q{s='print q<puts %%q{s=%r;print s%%s}>';print s%s}


which then prints the original Python snippet:

diff -s <(ruby <(perl <(python thirdorderquine.py))) thirdorderquine.py
Files /dev/fd/63 and thirdorderquine.py are identical


## Perl -> PHP -> HTML + JavaScript, 105 chars

I wanted to make the chain of languages somehow meaningful, so I figured I'd write a PHP script that generates a HTML page containing JavaScript. For the third language I chose Perl, just because I like Perl. :)

Some might consider this four languages, but I don't really count HTML as separate from JavaScript here — it's a markup language, not a programming language. Anyway, here are the three versions:

Perl (105 chars):

printf+(q(<script>alert(unescape("<?=urlencode(<<<E%sprintf+(q(%s),$/)x2,$/%sE%s)?>"))</script>),$/)x2,$/


PHP (165 chars):

<script>alert(unescape("<?=urlencode(<<<E
printf+(q(<script>alert(unescape("<?=urlencode(<<<E%sprintf+(q(%s),$/)x2,$/%sE%s)?>"))</script>),$/)x2,$/
E
)?>"))</script>


HTML + JavaScript (235 chars):

<script>alert(unescape("printf%2B%28q%28%3Cscript%3Ealert%28unescape%28%22%3C%3F%3Durlencode%28%3C%3C%3CE%25sprintf%2B%28q%28%25s%29%2C%24%2F%29x2%2C%24%2F%25sE%25s%29%3F%3E%22%29%29%3C%2Fscript%3E%29%2C%24%2F%29x2%2C%24%2F"))</script>


(Ps. Yes, I know I could've made the PHP step an almost-noop, e.g. just generating HTML + JS code in Perl and appending <?php to it, but that felt too much like cheating. In this solution, the code is actually processed in PHP instead of just being copied verbatim.)

# Underload → sed → Perl, 23 bytes

Can probably get this down lower with better choices of languages. Arguably noncompeting because the rule "sed programs can take an empty line as argument" postdates the contest.

((iprint+q)Sa(:^)*aS):^


generates the sed program:

iprint+q(((iprint+q)Sa(:^)*aS):^)


which generates the Perl program:

print+q(((print+q)Sa(:^)*aS):^)



(note: there are two trailing newlines here), which generates the original Underload program again:

((iprint+q)Sa(:^)*aS):^


The main aim here is to find languages in which strings are nestable (i.e. you can just quote a string by enclosing it in delimiters, rather than having to escape it); Underload has (), Perl has q(), and in sed a string continues until whitespace (which is implicitly nestable if there's no whitespace in the program). There's probably an esolang or golfing language out there with a "print the rest of the current line, not followed by newlines" instruction, which would be ideal here, but I haven't spent all that much time looking for one; you could save 8 bytes minus the length of the instruction in that case. (Jelly almost works but its “ instruction doesn't quote itself. Besides, it postdates the challenge.)

You can reduce this to 22 bytes like this:

((csay+q)Sa(:^)*aS):^



(with one trailing newline, like a regular text file, rather than the zero you normally get in golf). However, this requires an Underload interpreter that's OK with treating newline as a no-op. Try It Online!'s does, but I think it postdates the challenge.