1
\$\begingroup\$

Many polyglots fill in the blanks between languages by either making code that is ignored in one language or loading code dynamically.

This time, let's write a polyglot that works without that. This polyglot will print the name of the language in each of the (at least 2) languages running it, for example "Python" or "JavaScript". The languages must be different rather than different versions of the same language.

Rules

  1. All code must be parsed at compile time (e.g. no comments, but dead code like unused string literals is allowed)
  2. No code must be parsed at runtime (no eval, exec etc.)
  3. No errors/exceptions, code must exit with status 0. Warnings are fine.

The hope here is to showcase some parser differences of the same code in two different languages: the cooler the parser differential, the better.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ This question tagged as popularity-contest but it missing things required of a pop-con. The validity criterion is not objective. "No comments" is a requirement that comes up from time to time, and while it can look objective there are so many edge cases that reasonable people differ on. It is also missing a clear goal that must be achieved. \$\endgroup\$ Commented Aug 18 at 21:05
  • \$\begingroup\$ That makes sense, thanks. I'm not sure what you mean by clear goal? I thought parser differentials made sense. Also, when I said no comments I meant in the bytecode sense, I specifically mentioned the code must be parsed somehow. Thanks for the feedback \$\endgroup\$ Commented Aug 18 at 23:27
  • \$\begingroup\$ The tag description is pretty detailed and it is what I'm comparing with. I think "clear goal" is a little confusing, but it's the phrasing used there. I would describe it more as a subjective criterion that users judge. The tag description calls out: Questions like "do (this) the most creative way" as not having a clear goal. I interpret this question as that. \$\endgroup\$ Commented Aug 19 at 13:05
  • \$\begingroup\$ Ah, thanks, I didn't know tags had an internal wiki. \$\endgroup\$ Commented Aug 19 at 16:08

2 Answers 2

5
\$\begingroup\$

Python/JavaScript

{TypeError:[print]}[TypeError][0]("Python")
1>1==console.log("JavaScript") 

Needs window.print to be defined in JavaScript (so a browser environment).

In JavaScript, the {TypeError:[print]} is interpreted as a C-style block with a label of TypeError, so it does nothing and instead it constructs a TypeError("Python") which is unused. In Python, it is interpreted as a dictionary literal instead, leading to print("Python")

On line 2, chained comparators in Python are used to stop the evaluation of console.log, whereas in JavaScript there are no chained comparisons.

\$\endgroup\$
2
  • \$\begingroup\$ Well, Python isn't compiled but is interpreted, so all code is "parsed at runtime". \$\endgroup\$ Commented Aug 19 at 3:23
  • \$\begingroup\$ @Lucenaposition In CPython, the code is compiled into bytecode before running. If there's a syntax error, the parser will reject the entire program rather than a subset, and CPython implements certain things at compile time you wouldn't see in a "pure" interpreter, like constant folding. At runtime, you have code execution, not parsing (in the ast.parse vs exec sense). \$\endgroup\$ Commented Aug 19 at 16:10
4
\$\begingroup\$

Jelly / 05AB1E

“05AB1E““¢³ƒ»Ṫḷ“r

Try in Jelly online!

Try in 05AB1E online!

How?

Employs unused string literals, as allowed under rule (1).
Everything is parsed, but very differently:

Jelly
“05AB1E““¢³ƒ»Ṫḷ“r
“      ““   »     - list of three compressed strings
 05AB1E  ¢³ƒ              “05AB1E»                       “»  “¢³ƒ»
                      -> [" shinestenographist Aaliyah", "", "Jelly"]
             Ṫ    - tail of {that} -> X = "Jelly"
               “r - literal character -> Y = 'r'
              ḷ   - {X} left argument {Y} -> "Jelly"
                  - implicit print
05AB1E
“05AB1E““¢³ƒ»Ṫḷ“r
“05AB1E“          - push compressed string "05AB1E"
                    (none of the characters are in the subset of the code page
                     usable for compressed parts of strings, so are taken as is.)
        “¢³ƒ»Ṫḷ“  - push compressed string "slightly withinṪḷ"
                    (the `Ṫḷ` unicode is taken as is, as these characters are not
                     in the code-page at all.)
                r - reverse the stack
                  - implicitly print the top of the stack
\$\endgroup\$
1
  • 3
    \$\begingroup\$ What an inspiring message: "Shine, stenographist Aaliyah!" \$\endgroup\$ Commented Aug 18 at 20:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.