Exploit comment symbols
A simple way to create a two-language polyglot is to have the code divided in two parts as follows:
- The first part does the actual work in language A, is harmless in language B (no errors), and ends in a language-A comment symbol, which hides the second part to language A.
- The second part does the actual work in language B.
Thus
- Language A sees the first part, which does the job, and then a comment.
- Language B sees a useless first part and then the second part, which does the job.
The only difficult part here is finding a set of statements (first part) that do the job in language A while not giving errors in language B. Some suggestions for this:
- Most stack-based languages allow displaying only the top of the stack at the end of the program (sometimes this is even default, as in 05AB1E).
- Some languages ignore undefined statements (for example Golfscript).
A simple example that uses these guidelines can be found herehere. Languages A and B are MATL and 05AB1E respectively.