Methods
The two most commonly used techniques for constructing a polyglot program are to make liberal use of languages which use different characters for comments and to redefine various tokens as others in different languages. Often good use is made with syntax quirks. These are demonstrated in this public domain polyglot written in ANSI C, PHP and bash:
#define a /* # /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 */ #includeNote 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 is used to eliminate undesirable outputs.
- Even on commented out lines, the "" and "?>" PHP indicators still have effect.
- The statement "function main" is valid in both PHP and bash; C #defines are used to convert it into "int main(void)" at compile time.
- Comment indicators can be combined to perform various operations.
- "if (($x))" is a valid statement in both bash and PHP.
- printf is a bash shell builtin which is identical to the 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 main function. In PHP the main function is defined but not called and in C there is no need to explicitly call the main function.
Some less-common languages also offer possibilities to create Polyglot code. Here is a small sample, written simultaneously in SNOBOL4, Win32Forth, PureBasicv4.x, and REBOL:
*BUFFER : A.A ; .( Hello, world !) @ To Including? Macro SkipThis; OUTPUT = Char(10) "Hello, World !" ;OneKeyInput Input('Char', 1, '') ; 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 Input set-modes system/ports/input NOP:: EndMacro ; Wishing to refine it with new language ? Go on !The term is sometimes applied to programs that are valid in more than one language, but do not strictly perform the same function in each. One use for this form is a file that runs as a DOS batch file, then re-runs itself in Perl:
@rem = ' --PERL-- @echo off perl "%~dpnx0" %* goto endofperl @rem '; #!perl print "Hello, world!\n"; __END__ :endofperlThis allows creating Perl scripts that can be run on DOS systems with minimal effort.
Read more about this topic: Polyglot (computing)
Famous quotes containing the word methods:
“The comparison between Coleridge and Johnson is obvious in so far as each held sway chiefly by the power of his tongue. The difference between their methods is so marked that it is tempting, but also unnecessary, to judge one to be inferior to the other. Johnson was robust, combative, and concrete; Coleridge was the opposite. The contrast was perhaps in his mind when he said of Johnson: his bow-wow manner must have had a good deal to do with the effect produced.”
—Virginia Woolf (18821941)
“I think it is a wise course for laborers to unite to defend their interests.... I think the employer who declines to deal with organized labor and to recognize it as a proper element in the settlement of wage controversies is behind the times.... Of course, when organized labor permits itself to sympathize with violent methods or undue duress, it is not entitled to our sympathy.”
—William Howard Taft (18571930)
“The reading public is intellectually adolescent at best, and it is obvious that what is called significant literature will only be sold to this public by exactly the same methods as are used to sell it toothpaste, cathartics and automobiles.”
—Raymond Chandler (18881959)