Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Polyglot (computing)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== ===C, PHP, and Bash=== Two commonly used techniques for constructing a polyglot program are to make use of languages that use different [[Character (computing)|characters]] for [[comment (computer programming)|comments]], and to redefine various tokens as others in different languages. These are demonstrated in this [[public domain]] polyglot written in [[ANSI C]], [[PHP]] and [[Bash (Unix shell)|bash]]: {{columns-start|num=3}} '''Highlighted for Bash''' <syntaxhighlight lang="bash"> #define a /* #<?php echo "\010Hello, world!\n";// 2> /dev/null > /dev/null \ ; // 2> /dev/null; x=a; $x=5; // 2> /dev/null \ ; if (($x)) // 2> /dev/null; then return 0; // 2> /dev/null; fi #define e ?> #define b */ #include <stdio.h> #define main() int main(void) #define printf printf( #define true ) #define function function main() { printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/; return 0; } #define c /* main #*/ </syntaxhighlight> {{column}} '''Highlighted for PHP''' <syntaxhighlight lang="PHP"> #define a /* #<?php echo "\010Hello, world!\n";// 2> /dev/null > /dev/null \ ; // 2> /dev/null; x=a; $x=5; // 2> /dev/null \ ; if (($x)) // 2> /dev/null; then return 0; // 2> /dev/null; fi #define e ?> #define b */ #include <stdio.h> #define main() int main(void) #define printf printf( #define true ) #define function function main() { printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/; return 0; } #define c /* main #*/ </syntaxhighlight> {{column}} '''Highlighted for C''' <syntaxhighlight lang="c"> #define a /* #<?php echo "\010Hello, world!\n";// 2> /dev/null > /dev/null \ ; // 2> /dev/null; x=a; $x=5; // 2> /dev/null \ ; if (($x)) // 2> /dev/null; then return 0; // 2> /dev/null; fi #define e ?> #define b */ #include <stdio.h> #define main() int main(void) #define printf printf( #define true ) #define function function main() { printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/; return 0; } #define c /* main #*/ </syntaxhighlight> {{columns-end}} Note the following: *A hash sign marks a [[preprocessor]] statement in C, but is a comment in both bash and PHP. *"//" is a comment in both PHP and C and the [[root directory]] in bash. *Shell [[Redirection (computing)|redirection]] is used to eliminate undesirable outputs. *Even on commented out lines, the "<code><?php</code>" and "<code>?></code>" PHP indicators still have effect. *The statement "<code>function main()</code>" is valid in both PHP and bash; C #defines are used to convert it into "<code>int main(void)</code>" at compile time. *Comment indicators can be combined to perform various operations. *"<code>if (($x))</code>" is a valid statement in both bash and PHP. *<code>[[printf (Unix)|printf]]</code> is a bash [[shell builtin]] which is identical to the [[printf format string|C printf]] except for its omission of brackets (which the [[C preprocessor]] adds if this is compiled with a [[C compiler]]). *The final three lines are only used by bash, to call the <code>main</code> function. In PHP the <code>main</code> function is defined but not called and in C there is no need to explicitly call the <code>main</code> function. ===SNOBOL4, Win32Forth, PureBasicv4.x, and REBOL=== The following is written simultaneously in [[SNOBOL]]4, [[Forth (programming language)|Win32Forth]], [[PureBasic]]v4.x, and [[REBOL]]: {{columns-start|num=4}} '''Highlighted for SNOBOL''' <syntaxhighlight lang="snobol"> *BUFFER : A.A ; .( Hello, world !) @ To Including? Macro SkipThis; OUTPUT = Char(10) "Hello, World !" ;OneKeyInput Input('Char', 1, '[-f2-q1]') ; Char End; SNOBOL4 + PureBASIC + Win32Forth + REBOL = <3 EndMacro: OpenConsole() : PrintN("Hello, world !") Repeat : Until Inkey() : Macro SomeDummyMacroHere REBOL [ Title: "'Hello, World !' in 4 languages" CopyLeft: "Developed in 2010 by Society" ] Print "Hello, world !" EndMacro: func [][] set-modes system/ports/input [binary: true] Input set-modes system/ports/input [binary: false] NOP:: EndMacro ; Wishing to refine it with new language ? Go on ! </syntaxhighlight> {{column}} '''Highlighted for Forth''' <syntaxhighlight lang="forth"> *BUFFER : A.A ; .( Hello, world !) @ To Including? Macro SkipThis; OUTPUT = Char(10) "Hello, World !" ;OneKeyInput Input('Char', 1, '[-f2-q1]') ; Char End; SNOBOL4 + PureBASIC + Win32Forth + REBOL = <3 EndMacro: OpenConsole() : PrintN("Hello, world !") Repeat : Until Inkey() : Macro SomeDummyMacroHere REBOL [ Title: "'Hello, World !' in 4 languages" CopyLeft: "Developed in 2010 by Society" ] Print "Hello, world !" EndMacro: func [][] set-modes system/ports/input [binary: true] Input set-modes system/ports/input [binary: false] NOP:: EndMacro ; Wishing to refine it with new language ? Go on ! </syntaxhighlight> {{column}} '''Highlighted for BASIC''' <syntaxhighlight lang="basic"> *BUFFER : A.A ; .( Hello, world !) @ To Including? Macro SkipThis; OUTPUT = Char(10) "Hello, World !" ;OneKeyInput Input('Char', 1, '[-f2-q1]') ; Char End; SNOBOL4 + PureBASIC + Win32Forth + REBOL = <3 EndMacro: OpenConsole() : PrintN("Hello, world !") Repeat : Until Inkey() : Macro SomeDummyMacroHere REBOL [ Title: "'Hello, World !' in 4 languages" CopyLeft: "Developed in 2010 by Society" ] Print "Hello, world !" EndMacro: func [][] set-modes system/ports/input [binary: true] Input set-modes system/ports/input [binary: false] NOP:: EndMacro ; Wishing to refine it with new language ? Go on ! </syntaxhighlight> {{column}} '''Highlighted for REBOL''' <syntaxhighlight lang="rebol"> *BUFFER : A.A ; .( Hello, world !) @ To Including? Macro SkipThis; OUTPUT = Char(10) "Hello, World !" ;OneKeyInput Input('Char', 1, '[-f2-q1]') ; Char End; SNOBOL4 + PureBASIC + Win32Forth + REBOL = <3 EndMacro: OpenConsole() : PrintN("Hello, world !") Repeat : Until Inkey() : Macro SomeDummyMacroHere REBOL [ Title: "'Hello, World !' in 4 languages" CopyLeft: "Developed in 2010 by Society" ] Print "Hello, world !" EndMacro: func [][] set-modes system/ports/input [binary: true] Input set-modes system/ports/input [binary: false] NOP:: EndMacro ; Wishing to refine it with new language ? Go on ! </syntaxhighlight> {{columns-end}} ===DOS batch file and Perl=== The following file runs as a [[DOS]] batch file, then re-runs itself in [[Perl]]: {{columns-start}} '''Highlighted for DOS batch''' <syntaxhighlight lang="dosbatch"> @rem = ' --PERL-- @echo off perl "%~dpnx0" %* goto endofperl @rem '; #!perl print "Hello, world!\n"; __END__ :endofperl </syntaxhighlight> {{column}} '''Highlighted for Perl''' <syntaxhighlight lang="perl"> @rem = ' --PERL-- @echo off perl "%~dpnx0" %* goto endofperl @rem '; #!perl print "Hello, world!\n"; __END__ :endofperl </syntaxhighlight> {{columns-end}} This allows creating Perl scripts that can be run on DOS systems with minimal effort. Note that there is no requirement for a file to perform exactly the same function in the different interpreters.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)