8
\$\begingroup\$

Challenge

  1. Choose any number of languages. Call this set L.
  2. Write a program so that running it in any language in L produces an interpreter for a different language in L. Each language in L must have exactly one interpreter for it. This is to prevent, for example, Nothing or Text being part of L and all interpreters being for that single language.
  3. Your score is the number of bytes of your code divided by the number of languages in L. Lowest score wins.

Rules

  • Any language may have an interpreter for any other language -- it doesn't need to be one large cycle.
  • An interpreter can input program code and input however you see fit.
  • Standard loopholes apply.
  • Different versions of a language count as distinct (e.g. Python 3.4 and 3.5) but not patch versions (e.g. Python 3.4.1 and 3.4.4). Thanks to caird coinheringaahing for this.

Example Submission (clarified due to Kaddath's comment)

# [Julia 1.0](https://julialang.org) and [Brainf***](https://esolangs.org/wiki/brainfuck), B bytes / 2 languages = B/2
```
#=
<julia interpreter written in brainf***>
[-][
=#
<brainf*** interpreter written in julia>
# ]
```
Julia 1.0: [Try it online!](https://tio.run/#julia1x)
Brainf***: [Try it online!](https://tio.run/#brainfuck)
Explanation:
When ran in Brainf***, the Brainf*** code is run. The `[-]` sets the current cell to zero, then we skip over the rest of the code using `[ ... ]` since we know the current cell is zero.
When ran in Julia, the `#= ... =#` marks a comment, letting the Brainf*** code not get executed. After that is the Brainf*** interpreter, then the final line would have an unmatched `]`, so it is commented out.
\$\endgroup\$
12
  • \$\begingroup\$ Welcome to Code Golf and nice first question! For future reference, we recommend using the Sandbox to get feedback on challenge ideas before posting them to main. This seems like a nice challenge on paper, but the practicalities of writing interpreters for languages any more complex than Turing tarpits means that this requires a lot of work to do for any non-trivial languages \$\endgroup\$ Commented Jun 8, 2021 at 11:54
  • \$\begingroup\$ If you're not sure what @cairdcoinheringaahing means by a Turing tarpit, see the Wikipedia entry linked. \$\endgroup\$ Commented Jun 8, 2021 at 11:56
  • \$\begingroup\$ @cairdcoinheringaahing Ah, ok. Yes, most languages are rather complicated. In retrospect, the Sandbox would have been the way to go. Sorry! \$\endgroup\$ Commented Jun 8, 2021 at 11:57
  • \$\begingroup\$ @JeffZeitlin A Turing tarpit is a language in which technically everything is possible but nothing is easy (e.g. brainf***). \$\endgroup\$ Commented Jun 8, 2021 at 11:57
  • 1
    \$\begingroup\$ I followed the consensus that emerged from the 2 existing answers before mine that the answer's code would be the interpreter for L1 when run in L2 (and vice-versa for 2 languages), intead of outputting the other language's interpreter, like it could be understood from your Example Submission.. Seeing you have commented an existing answer approvingly, I thought this is what you meant. But that could be clearer in your example \$\endgroup\$
    – Kaddath
    Commented Jun 9, 2021 at 15:24

3 Answers 3

6
\$\begingroup\$

Jelly u / Python 3 + jelly, 70 bytes = 35

from jelly import*
a=sys.argv
main(a[1],[*map(try_eval,a[2:])],'')
ŒV

Try it online! (Jelly)

Try it online! (Python)

Both error after executing the other language.

This is a bit of a cheat tbh. Jelly has an "execute as Python" builtin, ŒV, and, as of April 9, 2018, Jelly is a pip-installable package, meaning that we can just import it and run it.

How they work

Jelly ignores every line except for the last, and just runs that:

ŒV - Main link. Takes a string on the left
ŒV - Execute the string as Python code

For some reason, I think because of the [...] in the above lines, there's some parsing error thrown afterwards.

For Python, we import the contents of the jelly package, read and evaluate the command-line arguments, then run the program in the first command-line argument with the values in the second and third arguments

\$\endgroup\$
3
  • \$\begingroup\$ Wow! How does the Jelly solution work? I'm surprised! \$\endgroup\$ Commented Jun 8, 2021 at 12:05
  • \$\begingroup\$ @KinuTheDragon I've added an explanation - ŒV is Jelly's "evaluate as Python" builtin \$\endgroup\$ Commented Jun 8, 2021 at 12:12
  • \$\begingroup\$ I've seen Jelly used to parse HTML, but this is surprising! :) +1 \$\endgroup\$ Commented Jun 8, 2021 at 12:14
2
\$\begingroup\$

Windows Batch/Powershell 7, 38 bytes, 19 score

ls >$null&&cmd /c "$args"
pwsh -c "%*"

Assumes that there is no file named ls in current directory plus Powershell 7 executable pwsh.exe is in PATH.

It needs Powershell 7 because && and || syntactic sugar was introduced in that version.

It first checks if any command is ls, >$null suppresses the output. If it succeeded we assume the interpreter is Powershell, so we execute Batch code and vice versa.

It generates some extra errors besides the STDOUT, but I think that's fine because @caird's answer also errors.

\$\endgroup\$
1
\$\begingroup\$

Windows Batch / PHP < 8, 25 bytes, Score: 12.5

php -r %1;echo`$argv[1]`;

(cannot really) Try it online for PHP! (it runs, but external scripts seem to be disabled, for security reasons I guess)

Cannot test Windows Batch in TIO and haven't found another site for it (makes sense actually) -> you can place the code in a test.bat file and run something like test.bat "echo 42;" to pass the PHP code as parameter

  • Assumes that PHP executable is in PATH under the php command.
  • in PHP below version 8, php and r are assumed string constants, and - % are operators, this part tries to calculate the result but does nothing with it. The rest of the code executes the argument as command-line code and outputs the result.
  • in Windows Batch, executes the first argument received as PHP code and outputs the result. The rest of the code is considered as a second argument and is ignored.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.