A polyquine is both quine and polyglot.1 You are to write a quine which is valid in at least two different languages. This is code golf, so the shortest answer (in bytes) wins.
1 I made that up. Or rather, Geobits did. Apparently, he wasn't the first one either, though.
Rules for Quines
Only true quines are accepted. That is, you need to print the entire source code verbatim to STDOUT, without:
- reading your source code, directly or indirectly.
- relying on a REPL environment which just simply evaluates and prints every expression you feed it.
- relying on language features which just print out the source in certain cases.
- using error messages or STDERR to write all or part of the quine. (You may write things to STDERR or produce warnings/non-fatal errors as long as STDOUT is a valid quine and the error messages are not part of it.)
Furthermore, your code must contain a string literal.
Rules for Polyglots
The two languages used must be distinctly different. In particular:
- They must not be different versions of the same language (e.g. Python 2 vs. Python 3).
- They must not be different dialects of the same language (e.g. Pascal vs. Delphi).
- One language may not be a subset of the other one (e.g. C vs. C++).
-
4\$\begingroup\$ "your code must contain a string literal" Even in languages that have no string literals, such as Brainfuck? \$\endgroup\$Peter Olson– Peter Olson2014年09月08日 19:35:55 +00:00Commented Sep 8, 2014 at 19:35
-
\$\begingroup\$ @PeterOlson The purpose of the rule is avoid a few loopholes in the golfing-specific languages (e.g. this one). As I can't anticipate what removing or weakening the rule would lead to (and as I'm not a fan of changing rules unless absolutely necessary), I'm sorry, but BF submissions won't be valid for the purpose of this challenge. If it's any consolation, a BF submission would likely not be competitive anyway. ;) \$\endgroup\$Martin Ender– Martin Ender2014年09月08日 20:31:53 +00:00Commented Sep 8, 2014 at 20:31
-
1\$\begingroup\$ "he wasn't the first one either" links to where? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年05月23日 16:39:20 +00:00Commented May 23, 2017 at 16:39
-
4\$\begingroup\$ C is not really a subset of C++. \$\endgroup\$MD XF– MD XF2017年05月23日 18:35:01 +00:00Commented May 23, 2017 at 18:35
-
\$\begingroup\$ Do two different Brainf**k-based languages count as the same language, such as Brainf**k and MOO? \$\endgroup\$MD XF– MD XF2017年10月31日 01:11:51 +00:00Commented Oct 31, 2017 at 1:11
22 Answers 22
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)
-
\$\begingroup\$ +1 Golfed it to 140 bytes:
a='print(a[78:]%a)1q=String.fromCharCode(39);console.log("a="+q+a+q+a.slice(82))1a=%r;eval(a.split(1)[0|0=="0"])';eval(a.split(1)[0|0=="0"]). Tested in JavaScript but not in python... but it should work. \$\endgroup\$soktinpk– soktinpk2014年09月09日 01:44:49 +00:00Commented Sep 9, 2014 at 1:44 -
\$\begingroup\$ @soktinpk Thanks, but I don't think Python will allow
a.split(1). \$\endgroup\$grc– grc2014年09月09日 03:38:56 +00:00Commented Sep 9, 2014 at 3:38 -
1\$\begingroup\$ -8 bytes:
q=unescape("%27")\$\endgroup\$Patrick Roberts– Patrick Roberts2017年05月23日 20:54:33 +00:00Commented May 23, 2017 at 20:54
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:
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.
-
3\$\begingroup\$ Welcome to the Programming Puzzles & Code Golf community! \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年09月15日 11:38:57 +00:00Commented Sep 15, 2016 at 11:38
-
3\$\begingroup\$ I know it's been almost two years, but you can golf 58 bytes. Try it online Java. and Try it online C# .NET. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年04月13日 11:30:47 +00:00Commented Apr 13, 2018 at 11:30
-
\$\begingroup\$ Did you mean 688 bytes? \$\endgroup\$Ivan Kochurkin– Ivan Kochurkin2018年04月13日 19:42:52 +00:00Commented Apr 13, 2018 at 19:42
Ruby/Perl/PHP, 52
$b='$b=%c%s%c;printf$b,39,$b,39;';printf$b,39,$b,39;
Copied verbatim from Christopher Durr's Perl quine.
This is rules abuse. Ruby and Perl are definitely not the same language, nor is Perl a subset of Ruby (most of the linked Perl quines don't work in Ruby, for example). But Ruby was designed to be able to look a lot like Perl if you want it to, and this happens a lot when golfing.
-
\$\begingroup\$ Could this be made to work (or does it already) in PHP as well? If I'm reading the doc correctly, you can run it on the command line with
-Rand you don't need the script tags. php.net/manual/en/features.commandline.options.php \$\endgroup\$hmatt1– hmatt12014年09月08日 18:08:33 +00:00Commented Sep 8, 2014 at 18:08 -
\$\begingroup\$ I wouldn't call it rule abuse. Finding a quine which works on an intersection of two languages is definitely a valid way to approach this question. However, seeing this is not your own work, I'd prefer if you marked it community wiki. \$\endgroup\$Martin Ender– Martin Ender2014年09月08日 19:22:02 +00:00Commented Sep 8, 2014 at 19:22
-
\$\begingroup\$ @chilemagic indeed it does! \$\endgroup\$histocrat– histocrat2014年09月08日 20:41:23 +00:00Commented Sep 8, 2014 at 20:41
-
\$\begingroup\$ @MartinBüttner done. \$\endgroup\$histocrat– histocrat2014年09月08日 20:42:20 +00:00Commented Sep 8, 2014 at 20:42
Perl 5/Ruby/JavaScript (Node.js)/Bash/Python 2/PHP, 1031 bytes
s=1//2;_=r'''<?#/.__id__;s=+0;#';read -d '' q<<'';s=\';Q='echo s=1//2\;_=r$s$s$s\<\?\#/.__id__\;s=+0\;#$s\;read -d $s$s q\<\<$s$s\;s=\\$s\;Q=$s$Q$s\;eval\ \$Q;echo $q';eval $Q
$_='eval("0"?0?"def strtr(s,f,t);s.tr(f,t) end;class String;def chr(n);self+n.chr end;end":"$u=strtr=(s,f,t)=>[...f].reduce((s,a,i)=>s.replace(RegExp(a,`g`),t[i]),s);printf=console.log;(S=String).prototype.chr=function(n){return this+S.fromCharCode(n)}":[]&&"sub strtr{eval q(q(X)=~y/X/X/r)=~s/X/shift/ger}");printf(strtr("%s<?#/.__id__;s=+0;#j;read -d jj q<<jj;s=zj;Q=jecho s=1//2z;_=rksksksz<z?z#/.__id__z;s=+0z;#ksz;read -d ksks qz<z<ksksz;s=zzksz;Q=kskQksz;evalz zkQ;echo kqj;eval kQwk_=j%sj;eval(k_);//;#jjj;f=jjjs=1//2;_=r%%s%%s%%s;f=%%s%%s%%s;q=_[18]*3;print f%%%%(q,_,q,q,f,q)jjj;q=_[18]*3;print f%%(q,_,q,q,f,q)%s","jkwz","".chr(39).chr(36).chr(10).chr(92).chr(92)),[]&&"s=1//2;_=r".chr(39).chr(39).chr(39),$_,$u?"":"".chr(10));';eval($_);//;#''';f='''s=1//2;_=r%s%s%s;f=%s%s%s;q=_[18]*3;print f%%(q,_,q,q,f,q)''';q=_[18]*3;print f%(q,_,q,q,f,q)
Based on my updates to this answer, I thought I'd try and optimise out the code that prints a different permutation, but ended up adding in Bash, which added a load more bytes anyway. Whilst this is more optimised than my first attempt (saved over 300 bytes) I'm sure it can still be golfed further.
Alternative Perl 5/Ruby/JavaScript (Node.js)/Bash/Python 2/PHP, 1040 bytes
s=1//2;_=r'''<?#/.__id__;s=+0;#';read -d '' q<<'';s=\';Q='echo s=1//2\;_=r$s$s$s\<\?\#/.__id__\;s=+0\;#$s\;read -d $s$s q\<\<$s$s\;s=\\$s\;Q=$s$Q$s\;eval\ \$Q;echo $q';eval $Q
$_='$z=0?"$&".next+92 .chr+10 .chr: 0..a||eval("printf=console.log;unescape`$%27%5C%0Ak`");$q=$z[1]?$z[1]:h^O;printf("%s%s%s%s<?#/.__id__;s=+0;#%s;read -d %s%s q<<%s%s;s=%s%s;Q=%secho s=1//2%s;_=r%ss%ss%ss%s<%s?%s#/.__id__%s;s=+0%s;#%ss%s;read -d %ss%ss q%s<%s<%ss%ss%s;s=%s%s%ss%s;Q=%ss%sQ%ss%s;eval%s %s%sQ;echo %sq%s;eval %sQ%s%s_=%s%s%s;eval(%s_);//;#%s%s%s;f=%s%s%ss=1//2;_=r%%s%%s%%s;f=%%s%%s%%s;q=_[18]*3;print f%%%%(q,_,q,q,f,q)%s%s%s;q=_[18]*3;print f%%(q,_,q,q,f,q)%s",[]&&"s=1//2;_=r",$r=[]&&$q,$r,$r,$q,$q,$q,$q,$q,$b=$z[2]?$z[2]:chr(92),$q,$q,$b,$d=$z[0]?$z[0]:h^L,$d,$d,$b,$b,$b,$b,$b,$d,$b,$d,$d,$b,$b,$d,$d,$b,$b,$b,$d,$b,$d,$d,$d,$b,$b,$b,$d,$d,$q,$d,$n=$z[3]?$z[3]:chr(10),$d,$q,$_,$q,$d,$q,$q,$q,$q,$q,$q,$q,$q,$q,$z[4]?"":$n);';eval($_);//;#''';f='''s=1//2;_=r%s%s%s;f=%s%s%s;q=_[18]*3;print f%%(q,_,q,q,f,q)''';q=_[18]*3;print f%(q,_,q,q,f,q)
A little closer to my original approach, but the repetition of the args for printf is still insane. Using positional arguments instead make this only work in Chrome and is tricky to get working in PHP as well because the $s in %1$s is interpolated, but could save a lot of bytes, perhaps using a combination of the two approaches...
PHP/Perl - 171
#<?PHP$s=1;$t="";
$a='%s<%cPHP$s=1;$t="";%c$a=%c%s%c;$t==$s?$t="#":$s;printf($a,$t,63,10,39,$a,39,10,63);%c#%c>';$t==$s?$t="#":$s;printf($a,$t,63,10,39,$a,39,10,63);
#?>
Run with:
$ php quine.pl
$ perl quine.pl
The php code is actually running (not just printing itself).
Bash/Ruby, (削除) 104 (削除ここまで) 82
"tee`#";puts <<a*2+'a'#`" -<<'a';echo a
"tee`#";puts <<a*2+'a'#`" -<<'a';echo a
a
Older version:
"alias" "puts=f()(tee -;echo a);f"
puts <<a *2+"a"
"alias" "puts=f()(tee -;echo a);f"
puts <<a *2+"a"
a
Bash/Ruby, 128 without undefined behavior
"alias" 'puts=f()(a=`cat`;echo "$a
$a
a");f'
puts <<'a' *2+"a"
"alias" 'puts=f()(a=`cat`;echo "$a
$a
a");f'
puts <<'a' *2+"a"
a
-
\$\begingroup\$ wow, I don't even understand how the Ruby code works :D \$\endgroup\$Martin Ender– Martin Ender2014年09月10日 18:51:45 +00:00Commented Sep 10, 2014 at 18:51
-
\$\begingroup\$ @MartinBüttner
<<ain Ruby works just like Bash, but returns a string. I didn't write a Ruby program before. I just found a random language with this feature. \$\endgroup\$jimmy23013– jimmy230132014年09月10日 19:00:49 +00:00Commented Sep 10, 2014 at 19:00 -
\$\begingroup\$ I don't know how it works in bash either :P \$\endgroup\$Martin Ender– Martin Ender2014年09月10日 19:02:19 +00:00Commented Sep 10, 2014 at 19:02
-
\$\begingroup\$ @MartinBüttner It is called heredoc.
<<wordreturns a string closed by a line with a singleword. \$\endgroup\$jimmy23013– jimmy230132014年09月10日 19:06:45 +00:00Commented Sep 10, 2014 at 19:06
Bash/GolfScript, 73
.~0 ()
{
declare "-f" @* ".~0" " ()
"+@n.;
echo '.~0;'
}
.~0;
There is a trailing space on each of the first 3 lines.
Bash/GolfScript, 78
alias :a~a.='eval "alias :a~a."\
;set [61 39]+a[39]+n"":a;echo ":a~a."'
:a~a.
Ruby/Mathematica, 225 bytes
Here is my own very beatable polyquine (which serves as example and proof-of-concept):
s="s=%p;puts s%%s;#Print[StringReplace[s,{(f=FromCharacterCode)@{37,112}->ToString@InputForm@s,f@{37,37}->f@37}]]&@1";puts s%s;#Print[StringReplace[s,{(f=FromCharacterCode)@{37,112}->ToString@InputForm@s,f@{37,37}->f@37}]]&@1
The first part is based on this Ruby quine and is basically:
s="s=%p;puts s%%s;#MathematicaCode";puts s%s;#MathematicaCode
The string assignment is exactly the same in Mathematica. The puts s%s is interpreted as a product of 4 symbols: puts, the string s, % (the last REPL result or Out[0] if it's the first expression you evaluate) and another s. That's of course completely meaningless, but Mathematica doesn't care and ; suppresses any output, so this is just processed silently. Then # makes the rest of the line a comment for Ruby while Mathematica continues.
As for the Mathematica code, the largest part of it, is to simulate Ruby's format string processing without using any string literals. FromCharacterCode@{37,112} is %p and FromCharacterCode@{37,112} is %%. The former gets replaced with the string itself, (where InputForm adds the quotes) the latter with a single %. The result is Printed. The final catch is how to deal with that # at the front. This is Mathematica's symbol for the first argument of a pure (anonymous) function. So what we do is we make all of that a pure function by appending & and immediately invoke the function with argument 1. Prepending a 1 to a function call "multiplies" the result with 1, which Mathematica again just swallows regardless of what kind of thing is returned by the function.
reticular/befunge-98, 28 bytes [noncompeting]
<@,+1!',k- ';';Oc'43'q@$;!0"
Try reticular! Try befunge 98!
Anything in between ;s in befunge is ignored, and ! skips into the segment between ;s for reticular. Thus, reticular sees:
<@,+1!',k- ';';Oc'43'q@$;!0"
< move left
" capture string
0 push zero
;! skip `;` (end program)
$ drop zero
q@ reverse TOS
'43' push 34 (")
c convert to char
O output all
; end program
Befunge sees:
<@,+1!',k- ';';Oc'43'q@$;!0"
< move left
" capture string
!0 push 1
; ; skip this
- ';' push 27
,k output top 27 chars
+1!' push 34 (")
, output "
@ end program
C/PHP, (削除) 266 (削除ここまで) (削除) 304 (削除ここまで) (削除) 300 (削除ここまで) (削除) 282 (削除ここまで) (削除) 241 (削除ここまで) 203 + 10 bytes
//<?php
function main($a){printf($a="%c//<?php%cfunction main(%ca){printf(%ca=%c%s%c,13,10,36,36,34,%ca,34,36,10,10,10);}%c#if 0%cmain();%c#endif",13,10,36,36,34,$a,34,36,10,10,10);}
#if 0
main();
#endif
+10 bytes because compiling in C requires the GCC compiler flag -Dfunction=.
How it works (in PHP):
- The PHP interpreter simply prints everything before the
<?phpas HTML.//is not a comment in HTML, so it's simply printed. mainis declared as a function with a variablea.printfprints a carriage return (to override the already-printed//) and then the source code, using a standard C/PHP quining method.#if 0is ignored by PHP.main($a)initializes an empty variablea. (Previously usederror_reporting(0)to ignore errors caused by callingmain())#endifis also ignored by PHP.
How it works (in C):
//<?phpis a single-line comment, so it is ignored.- The
functionkeyword is ignored due to the command-line compiler argument-Dfunction=. - GCC and Clang don't care if variables start with or contain
$. (This saved the day.) printfprints a carriage return (useless in this instance) and then the source code, using a standard C/PHP quining method.#if 0ignores everything until theendif, so PHP can callmain.#endifends the "ignore me" block.
Wumpus/><>/Befunge-98 31 bytes
"]#34[~#28&o@,k+deg0 #o#!g00
Try it in Wumpus!, Try it in ><>!, Try it in Befunge-98!
How it Works:
Wumpus Code:
" Start string literal
Bounce off end of line and come back
" End string literal
] Push top of stack to bottom
#34 Push double quote
[~ Get bottom of stack and swap it with the double quote
#31 Push 31
&o@ Print the top 31 items on stack and terminate program
><> Code:
" Start string literal
Wrap when it reaches the end of the line
" End string literal
]# Clear stack and reflect
" Wrapping string literal again, but backwards
+2: Copy the space and add 2 to get "
#o#! Skip into the printing loop
Exit with an error
Befunge-98 Code:
" Wrapping string literal
] Turn right
] Turn right again, going West
" Wrapping string literal going West
!+2: Get double quote and invert it
#o# Skip over the o instruction
+2:$ Get double quote
+ff Push 30
@,k Print 30+1 items from the stack and terminate program.
><> and CJam, 165 bytes
"~~~~~~~~~~~~~~~~~~~~~~~r00gol?!v93*0.Hi
' < .1*5av!?log10oar~~~r
'"`{"`"\"_~e#.21 <.2+4*96;!?log10oa"}_~e#.21 <.2+4*96;!?log10oa
To CJam, the program starts with a multi-line string literal. This is escaped with `, and then it uses the standard quine to print the quine code, as well as a trailing comment.
To><>, the first " starts a string literal that goes through the entire first row, pushing every character to the stack. After that, the trailing spaces (created due to the input being padded) are deleted, and then the stack is reversed. Every character in the stack (i.e. the entire first row) is output, and then it moves down to the second row.
The second row essentially does the same thing, except that it's in the opposite direction, so you don't need to reverse the stack. (I do anyway, because I have to delete the trailing spaces.)
Finally, it moves on to the third line. The only major difference is that you must skip the CJam block, which is done using . The single quote captures the entire line (again, backwards), and then it is output.
C/Lisp, 555 bytes
t(setq /*;*/){}main(){char q='\"',s='\\';char*a=
"~%t(setq /*;*/){}main(){char q='~A';char*a=
~S;char*b=/*
)(setq a ~S)
(setq */ ~S;printf(b,s,q,s,s,q,a,q,q,s,s,s,q,s,s,s,s,q,q,b,q/*
)(format t /* a /* a */);}~%";char*b=/*
)(setq a "\\\"',s='\\\\")
(setq */ "
t(setq /*;*/){}main(){char q='%c%c',s='%c%c';char*a=
%c%s%c;char*b=/*
)(setq a %c%c%c%c%c',s='%c%c%c%c%c)
(setq */ %c%s%c;printf(b,s,q,s,s,q,a,q,q,s,s,s,q,s,s,s,s,q,q,b,q/*
)(format t /* a /* a */);}
";printf(b,s,q,s,s,q,a,q,q,s,s,s,q,s,s,s,s,q,q,b,q/*
)(format t /* a /* a */);}
Intentionally blank first line.
Vyxal D, 70 bytes/ Python 3, 77 bytes
a="`q‛:Ė+34Cp‛a=print('a=%r;exec(a[13:])#\140円:Ė'%a)";exec(a[13:])#`:Ė
Vyxal : Try it Online!
a="`q‛:Ė+34Cp‛a=print('a=%r;exec(a[13:])#\140円:Ė'%a)";exec(a[13:])#`:Ė
Python : Try it online!
The difference beetween the number of bytes is due to the fact that vyxal and python doesn't use the same charset
Explanation :
- Vyxal
a=" # do nothing revelent here
`q‛:Ė+ `:Ė # structure for a quine
34Cp‛a=p # prepend `a="` to the string
rint # do stuff that somehow push 0 to the stack
( # for loop over 0 (do nothing)
# # comment and implicily close the loop
# implicit output
- Python
The quine is a variation of a="print('a=%r;exec(a)'%a)";exec(a)
C/TCL, 337 bytes
#define set char*f=
#define F
#define proc main(){
set F "#define set char*f=
#define F
#define proc main(){
set F %c%s%c;
proc /* {} {}
puts -nonewline %cformat %cF 34 %cF 34 91 36 36]
set a {*/printf(f,34,f,34,91,36,36);}
";
proc /* {} {}
puts -nonewline [format $F 34 $F 34 91 36 36]
set a {*/printf(f,34,f,34,91,36,36);}
C/Vim 4.0, 1636 bytes
Contains control characters.
map () {}/*
map g ;data0df"f"cf"
f"cf"
f"D2kyyP;g6k2dd4x5jA"JxA","JxA","jyyPkJxA"jok;g2kdd4xkJx3jdd
map ;g O"vdldd0i# 0# 1# 2# 3# 4# 5# #0lx2lx2lx2lx2lx2lx2lx:s/##/#/g
o:s//"/gk0y2lj02lp"addk@ao:s//\\/gk0ly2lj02lp"addk@ao:s///gk04ly2lj02lp05l"vp"addk@ao:s///gk05ly2lj02lp05l"vp"vp"addk@ao:s//
/gk06ly2lj02lp05l"vp"vp"vp"addk@ao:s//
/gk02ly2lj02lp05l"vp"addk@a
unmap ()
map ;data o*/ char*g[]={"map () {}/*#2map g ;data0df#0f#0cf#0#5#3f#0cf#0#5#3f#0D2kyyP;g6k2dd4x5jA#0#3JxA#0,#0#3JxA#0,#0#3jyyPkJxA#0#3jo#3k;g2kdd4xkJx3jdd#2map ;g O#4#4#4#4#3#0vdldd0i## 0## 1## 2## 3## 4## 5## ###30lx2lx2lx2lx2lx2lx2lx:s/####/##/g#5o:s//#0/g#3k0y2lj02lp#0addk@ao:s//#1#1/g#3k0ly2lj02lp#0addk@ao:s//#4#4#3/g#3k04ly2lj02lp05l#0vp#0addk@ao:s///g#3k05ly2lj02lp05l#0vp#0vp#0addk@ao:s//#4#4#5/g#3k06ly2lj02lp05l#0vp#0vp#0vp#0addk@ao:s//#4#4#5/g#3k02ly2lj02lp05l#0vp#0addk@a#2unmap ()#2#2map ;data o*/ char*g[]={","#A#0#a#0,#0#b#0,#0#c#0#C#2","}; /*#3#2#2#0*/ print(char*s){char*t=s,c,d;while(c=*t++)if(c==35){c=*t++;if(c==35)putchar(c);else if(c==48)putchar(34);else if(c==49)putchar(92);else if(c==50)printf(#0#1n#0);else if(c==51)putchar(27);else if(c==52)putchar(22);else if(c==53)putchar(13);else if(c>64&&c<91)print(g[c-65]);else printf(g[c-97]);}else putchar(c);} main(){print(g[1]);}"}; /*
"*/ print(char*s){char*t=s,c,d;while(c=*t++)if(c==35){c=*t++;if(c==35)putchar(c);else if(c==48)putchar(34);else if(c==49)putchar(92);else if(c==50)printf("\n");else if(c==51)putchar(27);else if(c==52)putchar(22);else if(c==53)putchar(13);else if(c>64&&c<91)print(g[c-65]);else printf(g[c-97]);}else putchar(c);} main(){print(g[1]);}
Your Vim needs to have the following set:
set noai
set wm=0
set nosi
set tw=0
set nogdefault
Perl/Javascript (SpiderMonkey), 106 bytes
$_='$q=+[]?h^O:unescape("%27");print("$_="+$q+$_+$q+";eval($_)"||(q($_),"=$q$_$q;",q(eval($_))))';eval($_)
Try the Perl online!
Try the JavaScript online!
Explanation
The quine data is stored in $_ in both languages and then evaled, which is pretty much standard procedure in Perl. I chose SpiderMonkey on TIO as it has a print function, but this could easily be ported to browser for + 20 bytes (add eval("print=alert"); to the beginning of $_s definition).
Perl sees the data stored in $_ and evals it as usual. Since +[] is truthy in Perl,' is stored in $q via the stringwise-XOR of h and O. The final trick is in the call to print where the first part for JavaScript uses +, which in Perl treats all items as numbers, and adds up to 0, then we use the || operator to return what we actually want (q($_),"=$q$_$q;",q(eval($_))) which is equivalent to "\$_=$q$_$q;eval(\$_)".
In JavaScript, +[] returns 0, so we call unescape("%27") to store ' in $q (unfortunately, atob doesm't exist in SpirderMonkey...). In the call to print, since + is the concatenation operator in JavaScript, the first block builds the desired output and the second part after the || is ignored.
Thanks to Patrick Roberts' comment for the unescape trick!
Perl/JavaScript (Browser), 108 bytes
$_='eval("q=_=>_+``;printf=console.log");printf(q`$_=%s%s%s;eval($_)`,$q=+[]?h^O:atob("Jw"),$_,$q)';eval($_)
$_='eval("q=_=>_+``;printf=console.log");printf(q`$_=%s%s%s;eval($_)`,$q=+[]?h^O:atob("Jw"),$_,$q)';eval($_)
Explanation
We store the quine data in $_ in both languages and then eval it, which is pretty much standard procedure in Perl.
Perl sees the data stored in $_ and evals it as usual. The eval within $_ is executed and fails to parse, but since it's eval, doesn't error. printf is then called, with a single quoted string q(), with ` as the delimter, as just using ` would result in commands being executed in shell, then for the first use of $q, since +[] is truthy in Perl, ' is stored in $q via stringwise-XOR of h and O.
In JavaScript, the eval block within $_ sets up a function q, that returns its argument as a String and aliases console.log to printf, since console.log formats string like printf in Perl. When printf is called +[] returns 0, so we call atob to decode ' and store in $q.
Perl 5/Ruby/PHP/JavaScript (Browser), 153 bytes
$_='$z=0?"$&".next: 0..a||eval("printf=console.log;atob`JCc`");printf("%s_=%s%s%s;eval(%s_);",$d=$z[0]?$z[0]:h^L,$q=$z[1]?$z[1]:h^O,$_,$q,$d);';eval($_);
Try the Perl online!
Try the Ruby online!
Try the PHP online!
$_='$z=0?"$&".next: 0..a||eval("printf=console.log;atob`JCc`");printf("%s_=%s%s%s;eval(%s_);",$d=$z[0]?$z[0]:h^L,$q=$z[1]?$z[1]:h^O,$_,$q,$d);';eval($_);
05AB1E/2sable, 14 bytes, non-competing
0"D34çý"D34çý
Try it online! (05AB1E)
Try it online! (2sable)
2sable is derived from 05AB1E and is similar, but has major differences.
Trailing newline.
-
4\$\begingroup\$ I don't know what the current state of 2sable and 05AB1E is, but last time I checked I would have considered them to be different dialects of one language. \$\endgroup\$Martin Ender– Martin Ender2016年11月11日 16:06:53 +00:00Commented Nov 11, 2016 at 16:06
C/dc, 152 bytes
z1d//[[z1d//]P91PP93P[dx]Pq
;main(){char*a="z1d//[[z1d//]P91PP93P[dx]Pq%c;main(){char*a=%c%s%c;printf(a,10,34,a,34);}//]dx";printf(a,10,34,a,34);}//]dx
Taking advantage of comments, yeah!
Python 3 / JavaScript (V8), 87 bytes
p="'";f=';print("p="+f[7]+p+f[7]+";f="+p+f+p+f)';print("p="+f[7]+p+f[7]+";f="+p+f+p+f)
Exploiting the fact that Javascript V8 and Python both have the print function to print to STDOUT.