Write a Polyquine

CJam/GolfScript, 34 bytes

{"__X.0#@@?LL
;~"N}__X.0#@@?LL
;~

The byte count contains a trailing linefeed, since the program wouldn't be a quine without it.

While CJam and GolfScript are very similar in some aspects, there are a lot of differences. To make this an "honest" polyquine, I decided to rely on the differences as much as possible. Except for the block and string syntax (which the languages share with oh so many others), no part of the code achieves exactly the same in both languages.

The online GolfScript interpreter has a bug; this program works only with the official interpreter.

Example run

$ cat polyquine
{"__X.0#@@?LL
;~"N}__X.0#@@?LL
;~
$ md5sum polyquine <(cjam polyquine) <(golfscript polyquine)
e2f1f3cd68abbbceec58080513f98d9a  polyquine
e2f1f3cd68abbbceec58080513f98d9a  /dev/fd/63
e2f1f3cd68abbbceec58080513f98d9a  /dev/fd/62

How it works (CJam)

" Push that block.                                                                        ";

{"__X.0#@@?LL
;~"N}

" Push two copies of the block, 1 (computed as 1**0) and rotate the block copies on top.  ";

__X.0#@@

" If 1 is truthy (oh, the uncertainty), execute the first copy; else, execute the second.
  Evaluating the block pushes the string it contains; N pushes a linefeed.                ";

?

" Push two empty arrays.                                                                  ";

LL

" Discard one empty array and dump the second.                                            ";

;~

" (implicit) Print all items on the stack.                                                ";

How it works (GolfScript)

# Push that block.

{"__X.0#@@?LL
;~"N}

# Push a copy of the block; _ and X are noops, # initiates an inline comment.

__X.0#@@?LL

# Discard the 0 and execute the copy of the block.
# Evaluating the block pushes the string it contains; N is a noop.

;~

# (implicit) Print all items on the stack, followed by a linefeed.

CJam/GolfScript, 12 bytes

{"0$~"N}0$~

Cheaty solution that avoids the languages' differences as much as possible.

Try it online:

  • CJam
  • GolfScript

How it works (CJam)

 "0$~"       " Push that string.                                                          ";
      N      " Push a linefeed.                                                           ";
{      }0$~  " Push a copy of the block and execute it.                                   ";
             " (implicit) Print the stack.                                                ";

How it works (GolfScript)

 "0$~"       # Push that string.
      N      # Undefined token (noop).
{      }0$~  # Push a copy of the block and execute it.
             # (implicit) Print the stack, followed by a linefeed.

C#/Java, 746 bytes

I use the property that chars in Java can be written as identical unicode sequences. If we have A instruction for C# compiler and B instruction for Java, we can use the following code fragment:

//\u000A\u002F\u002A
A//\u002A\u002FB

It will be "recognized" by the following way with C#:

//\u000A\u002F\u002A
A//\u002A\u002FB

And by the following way by Java:

//
/*
A//*/B

Because of \u000A is line break, \u002F is / and \u002A is * in Java.

So the final polyglot-quine is:

//\u000A\u002F\u002A
using System;//\u002A\u002F
class Program{public static void//\u000A\u002F\u002A
Main//\u002A\u002Fmain
(String[]z){String s="//@#'^using System;//'#^class Program{public static void//@#'^Main//'#main^(String[]z){String s=!$!,t=s;int[]a=new int[]{33,94,38,64,35,39,36};String[]b=new String[]{!&!!,!&n!,!&&!,!&@!,!&#!,!&'!,s};for(int i=0;i<7;i++)t=t.//@#'^Replace//'#replace^(!!+(char)a[i],b[i]);//@#'^Console.Write//'#System.out.printf^(t);}}",t=s;int[]a=new int[]{33,94,38,64,35,39,36};String[]b=new String[]{"\"","\n","\\","\\u000A","\\u002F","\\u002A",s};for(int i=0;i<7;i++)t=t.//\u000A\u002F\u002A
Replace//\u002A\u002Freplace
(""+(char)a[i],b[i]);//\u000A\u002F\u002A
Console.Write//\u002A\u002FSystem.out.printf
(t);}}

However, the size is too huge because of languages verbosity.

Compilation available on ideone.com: C#, Java.


Python 3 and JavaScript, 134 bytes

Here's my (final?) attempt:

a='eval(a.split(" ")[2%-4]),1//2# q=String.fromCharCode(39);console.log("a="+q+a+q+a.slice(-8)) print(a[-12:]%a) a=%r;eval(a)';eval(a)

It can probably be golfed a bit more, especially if anyone knows a better way to get single quotes in JavaScript.


Boiled down, the program looks like this:

a='a long string';eval(a)

The eval() function will evaluate expressions in both languages. So the long string gets executed:

eval(a.split(" ")[2%-4]),1//2# ... the rest gets commented out

This splits the long string by spaces and evaluates the substring indexed by 2%-4. JavaScript will run the third substring (2 % -4 == 2) and Python the second last (2 % -4 == -2), because their modulo operators behave differently for negatives.

The rest of the string gets ignored in both languages. JavaScript stops at the //, while Python sees it as integer division and stops at the #.

So JavaScript prints the source code to the console here:

q=String.fromCharCode(39);console.log("a="+q+a+q+a.slice(-8))

And Python here:

print(a[-12:]%a)

Both make use of the final part of the string, which is a template of the program:

a=%r;eval(a)