After yesterday’s piece on the 128 language Quine Relay, I had to follow up with this polyglot program on Stack Overflow’s Code Golf forum, which at the time of writing, works in 191 languages and is still actively updated.

The nth program to be submitted must run in n different languages; specifically, all the languages added in previous programs to be submitted, plus one more. The program must output 1 when run in the first language used in answers to this question, 2 when run in the second language, and so on.

A polyglot program is closely related to a quine relay; where a quine relay is run in one language to produce a program in another, a ployglot program is a single program which can be interpreted as-is in more than one language – often to produce different results.

It begins with a Python 3 program, that prints 1:

print(1)

To which a program in V (vim) was added, which prints 2, respecting the Python 3′s program:

print(1)#i2

This works because everything up to the # is ignored by V, and the i following the # is read as insert, inserting the value 2, while # marks a comment in Python 3.

From there, a string of 2D languages are added, which adds alternate program flows to the program, useful in that it’s easier to avoid problematic characters intended for other languages, but adding another complication to keep track of as the program evolves.

Many entries take advantage of symbols that denote comments in one language and not another, like with the Python example above. Others use differences in whether a value is treated as true or false:

In Python 2, ½ is 0, which is a falsy value, which makes Python print 5. In Python 3, ½ is 0.5, which is a truthy value, which makes Python print 1

The great feature of this thread is the detail given by many of the participants in their explanations of how and why their entry works. It is a great intro in how to write a polyglot. Also, it’s still an open contest, meaning there’s time to take part in expanding this monstrosity of code. It officially closes when there are 14 days of inactivity, at which point the second-to-last entry wins (to make sure people don’t intentionally post changes that make their entry imposible to follow).

As of the time of writing, it’s at 191 languages (over 9k of text!), the latest adding PHP, and it looks like this (good luck making sense of it):

#16  "?63(o+?50;+'51;'  # #@@ " /*"r"{\D-v e-'[fa5.q]PkPPX)\( 9 '#CO"14"^ 92                                       7 222222222222222222222222                                                             ##*/
#/*1&7//```"`    [>.>.]       )[-'][(7  >77*,68*,@@'_         7 )(22)S / \iiipsddpsdoh#####(#######??   #### ##  ######     ####         ###### # ####  #######     ####         ###### # ####  #######  a5# \7aa*+42@@n; 7 999993 1 7 3 1 8 1 1 55 EEEEEδΘΔΔΔΘΔΘλa k zzzzkf kf k zzzzzd kf k zzzzza kf bfz coding=utf8 p''53'S^'                                                                                                          ! 1>?7ДOq#t#>2/Wr#t#t#q#68#r#t#t#68#q#63#r#t#t#6v#>#</Wr#6}#y/===Wr#7ЯOq#>J7Д/Wr#y<Wr#>5/Wr#t#t#6y#>-=/Wr#6|#>6/Wr122! 1退
#>x#z#111#y#y#y#_#0111118&1&  111/"78"oo@@        xxxxxxxxxxxx        /112\     ##### #######   # #    ##### h#115#  o#    ##### ####  ###   #### #  # #####  #    ##### ####  ###   #### #  # #####  #    #
# 36!@@`D  e ++++++::@@                 L               R.----._      x-----x ########8=,_## ### ###### ######## #### ##### ####### ##### ###    # # #### ### ##### ####### ##### ###    # # #### ### ##### #
#comment -[af] xxxxxxxxxxxx\#184O@@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # ##########  ### ## #####  ## #### ## # #####  ## ##### ####   ##### ## # ##  ## ####  ## ##### ####   ##### ## # ##  ## ####
#~==RtRtRtMbMbMbPSPSPS                                                       # ????!?!??!??!!!!???!?!??!!?!?!!!!!?!!!!?????!????????????????????!                                                           
#[#[]]QhQhQhQrQrQrHnHnHnbqbqbqLzLzLzQtQtQtTcTcTcRQRQRQ                       #
#<<<#++R++  ++++++++++++++++++++++++++++++++++++++++++U+++.._+++++++._       #
############################################################################## 4O6O@@
 #-]+-}}[.^x+;;+;;+;;+<>;;+;;+;;+;;;;;;+;;+;;.._]}--<^>++[+++++[>+++++++<-]>._ ++++._+++._^<]+-+<[<<._>>>-]^>[<+++++[>++++++++++<-]>@@@@+.---@@._+>][[
#{  
#=  
#*  
#cs  
#2""/*  
#9999 9 9
#9 999 99 9999 9
#9  
# 9 9999
#`<`(+?+?0l0v01k1kMoOMoOMoOMoOMOOx0l0ix0jor0h0h1d111 0eU0y0yx0moO1d0y0e0e00m1d0i0fx0g0n0n11yxMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOmOotMOo0moo0n0tx0t0moO0f0t0gOOM0g0f0h0j0j0i000x1k1x0vx0v0l11110000011100^_)\
[ "`e```.1'.0'.6''i]56pq\{}26q",'_\['];#/s\\/;print 24; exit}}__END__/
###<$+@@+-@@@@@@@@=>+<@@@@@@=>+<?#>;?\:-._++._++++._#/<?\>3-++._6+---2._#</++++++++++++++++++++++++++++++++++++++++++++++++._++._++++++.>!\
'wWWWwWWWWwvwWWwWWWwvwWWWwWWW\WWWWWwWWWWwWWWW/WW\WwWWWWWWWWwwwwvwW/WWwWWWWwvwWWwWWWwvwWWwWWWwvwWWwWWW ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho ho dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO dO MU ([])  ({}<(((((()()())){}{})){}{})>)(({})){}{(<(<()>)({})({}<{}>({}){})>){({}[()])}}({}){}({}()<()()()>)  (<><()>){({}[()])}{  #((((((\';  a=$(printf \\x00);b=$(echo -n $a|wc -c);case $b[1] in 1*)echo 54;;4*)echo 78;;8*)echo 166;;*1*)echo 50;;*)echo 58;;esac;exit;#)';  print (0and eval('(defined?pp)&&190||(defined?__dir__)&&13||\'_\'[0]==95&&108||110')or(None and 9or 1/2and 1or 5<<64>>64or 68));"[${ print(g+91) }$/]es";exit; "$'#{print(187);exit}'$/"; #< ?>wWWssss {}#(prin 45)(bye) 46(8+9+9+9+9+=!) </>* * * * *[[3+4*7*@@]]xxxxxxxxxx
###;{a=1}={a:null};console.log a&&39||180;\
__DATA__=""""  
#p  \
__END__
set pr'[puts 59][exit]\'':;@@echo 185 #';set pr'-';pr 89;exit#ss
ifdef x 
#  
#  
#:1*23!/5x%6E0  !|*****[[[828+*+@@+*99]]]*****|!
#  
[mxf]-main=-[165]- ###jxf*#p 173#*
p now 70 dollar off!
p has been selling out worldwide!
#PLACET,2<- #2FAC,2SUB#1<- #52FAC,2SUB#2<- #32FACREADOUT,2PLEASEGIVEUPFACs>>>
seeeemPaeue_ewuuweeee_eeeeeeCisajjapp_ppppxf⠆⠄⡒⡆⡘😆😨😒😨💬95💬👥➡😻😹😸🙀🙀😹😼😿🙀🙀😼😼😸🙀🙀🙀🙀  
👋🔢🌚🌝🌝🌚🌚🌚🌚🌚▲▲▲²²²²▲¡▼¡▲▲¡→  
밠밠따빠빠맣박다맣받다맣희맣희うんたんたんたんたんうんうんうんうんうんたんうんうんうんたんうんたんたんうんたんたんうんたんたんうんたんたんうんたんたんたんたんたんうんうんうんうんたんたんうんたんたんたんうんうんうんたんうんうんたんうんうんたんうんうんたんうんたんうんうんうんたんたんうんたんたんうんたんたんうんたんたんうんたんたんたんうんうん  
😊♈💖  
😇♈♈  
😊♉♈  
😇♈♈  +------+
😇♈♉  |/0110@@| 
😇♈💜 |/0011 | 
😊♉♈  |/0010 | 
😊📢♈ |/0011 | 
😈♈💜 |/0001 | 
😊📢♈ |/$0011| 
😇♉💞 +------+ 
😊📢♉⠀⢃⠛⠋  
#-49,A,-1                              #
#-5,A,-1                               #
#6,A,-1                                #
1<<<< ! !
2<<<!<   !
1+``  +!!
1=  *
53  +
1>  *
51  +
#
1+  *     x+x   x  x
49 x+  xx+xxx+  x+x + x B+ ===+
x= xx x  x===  x x >8 xxx +++= +
x+  +x + x,_8=>+ xB  x+x
x==+R _*=x==  x +x==+> x x    +
 x +R"12"+8=Dx B=xD +  + xxx+
`+ x`=>8=x>  x+~ x  x +x+B+
x  +====+x>~+B+= x x + x
xD~+x 8=++>8x +=   x+    +,_   +
15 + x   x  + +  xx   +  ?   ?@@  %"18"_     !
1  + x   + x ++  xx   + ?@@    ?
1 \+* x    +  +  x    +   +   *
1   ! x  +x   +       + ?  !  ?
1    ! x xx --... ...--  ? @@ ?
x +   ++ xx  ++ ++     +
 +xxxxx  +     +
  
#  +
+ *   +
#  + *
  ***************************************<>***********+
  +  +$$$
  <>"3"O._+ rk:start | print: "69" rk:end e$P+++++*D*+++1++1E!s
   +   + *
$'main' *****+
 -+3o4o# +
   ++       +
 * *
  +*****
#       +
 (printout t 164 )
(exit )  
#cepp  
MsgBox (0,"",169     )
#cs  
Yo::=~147
::=  
You can see an x here.<<<<
  
>{-<<<<<  
> 176
>> Output 1
>SET x TO 120. [0]{472454523665721469465830106052219449897}   @@,-1,:*b5<>␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␌␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␌␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋␋
>X x. PPQ-}
>x--/2  
> =157;y=146--/2 
>main=print y{-ss s
  
\begin{code}  
{-x   ␉␉␉␉ 
␉
 ␉  



-}
open import IO;main = run(putStr"159" )
\end{code}
ppppppppppppp
out &49 &1
out &56 &1
out &50 &1

Take Northern Line to Tooting Bec
Take Northern Line to Charing Cross
Take Northern Line to Charing Cross
Take Northern Line to Bank
Take District Line to Hammersmith
Take District Line to Upminster
Take District Line to Hammersmith
Take District Line to Upminster
Take District Line to Embankment
Take Bakerloo Line to Embankment
7 UP
Take Northern Line to Mornington Crescent
7 RIGHT
7 RIGHT
7 TEACH
6 BOND
6 BOND
6 BOND
5 RIGHT
5 LEFT
5 RIGHT
7 BOND
7 TEACH
5 TEACH
6 YELL
5 TEACH
6 YELL
6 YELL
set ! 57,,...,,.,,..,,,,,,..,,,.$^
set ! 51.                         #e.0,1,_ _ye{--}besizeString xflos1''gem x=4721en nd ogola=1$0C0 cod/|puts_e25y $"3"91/2 <5>6#"5"8=6028=+ codefn;#6or [9]=x 3 8[]p#s4;93
More 91 of this
How much is it
red down one blue up red down one blue up red up one red right two blue up sss
baa baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bleeeeeeeeeeeeet bleeeeeeeeeeeeet bleeeeeeeeeet baaaa bleet bleeeeeeeeeet bleeet bleeeeeeeeeet
wwWWWwWWWWWwWWWWWWW wWWWWWWWWpppppp
When this program starts:
There is a scribe called x
x is to write 179

*/
#if 0
.int 2298589328,898451655,12,178790,1018168591,84934449,12597
#endif//*
#1""//*
#include<stdio.h>
#define  x(a) #a
#define u8 "38\0 "//"
char*x="24 10 31 1"
"a c #FFC0FF""B c #0000C0""d c #58007B""e c #0C8302"
"h c #E60001""i c #CAFFFF""j c #280000""k c #CA0000""l c #CA007F""n c #330001 ""q c #E60000"
"o c #FF8000""t c #FF00BC""u c #008080"
"A c #0040C0""E c #808000""F c #00C040""G c #008000 ""R c #800000"
"H c #0000AA""I c #00AA00""J c #55FFFF""K c #AAAAAA"
"r c red""g c green""b c blue""c c cyan""m c magenta""y c #FFFF00""x c black""_ c #FFFFFF"
"HHHahtdegggggggyrggggggc"
"IHHaixuEFbGGbggbryAEGRgc"
"JJHajyurbgbgggggggb____o"
"IJHakmyyyyyyyyyyyyyyyyye"
"I__almyyyyyyyyyyyyyyyyye"
"K__anmyyyyyyyyyyyyyy_y_e"
"HH_aqggyyyyyyyyg____m_Je"
"JH_axxxxxxxxxxxxxxxxxxxx"
"K__aaaam___bbbbbBm_bbBab"
"K__________bbbbb___bbxbb";//"
int f(char*a,char*b ){puts(a?"124":sizeof(0,u8)-5?u8"67":*u8""?"37":x(0'0  "'\"")[9]?"75":'??-'&1? "79":"77");}main(){f(x,x=0);}//<*/
#1""/*/
 
>import Text.Heredoc--WWWWWWWWWWWWWW<<W
>instance Num B where fromInteger _=B 170;negate _=B$x#x
>data B=B{u::Integer};g=[here|here<-"W>W"] --WWWWWWWWWW570rt Unc27<<[|]
>x=1;y#a=128;x#a = 174;(!  )=seq
>main|let p!_=0=print$sum[0!61,{-_-}last$172:[u$[-1]!!0|g<"Z>"]] --}  
 
 
 console.log 178;
 
#1""/*/
#if 0
#fi`le. :-write(186). end_of_file. `
#endif//*
/*/
p=sizeof(" (\");print'(''72'')';end!");main(){puts('??-'&1?"101":"92" );return 0;}
#if 0
#endif//*
print 61
#}
disp 49
#{
}{}<>        K yya+-        &  g+$
 /._++.._#<!._++.-.>@@ A=AgRA;       AC
#   /
 \?     -/!+++++++++++++++++++++++++++++++++++++++++++++++++<>^+++++<\>+R++.-.
endif
#<<<#/<+>/>>>\
"""#";  print(85)
#<   ~#class C{function:Main(a:String[])~Nil{83->Print();}}#+</.---.>/
#endcOmment
#nocOmment outtext("155")
#ce pS9^7^8^MUOUOF @@0:8:8      \    @@,,1'1'<> @@125iRE
#p|o51~nJ;#:p'34'3             \=# print(size([[1] [3]][1,:])[1]==2?158+4:17)#>say 27#>>say 170-3#]#print(47 )#]#echo 21#>/#print(171)#s-#print 175#s
#8M`|  <esc>dggi2<esc>//       $}<}}<}>}[<< }<<<<<}<<}<<<<}</<<}}}<}}<}}<}    }<}}<}}<}}}<}}<<<<<<<<<<<}}}<}}<}}<}}<}}<}}<}}}<<<<<<<<<<}]~-<~-<~-<<<~-<<COprint("65")#`=>ass^_^_#
#9'>+/' 25       xpppppppeeee*n*n*n*es*s*s*^ee*n*n*n*e*sss*e*n*n*n*ee*s<* 5>1  e*///\)Q222999686#