Polyglot self-interpreter time

8

1

Your task is to make a program which interprets the language it is run in, but also works in many languages.

Let's say you make a Brainfuck-Underload self-interpreglot, as it will be called. Run in Brainfuck, this program should be a Brainfuck interpreter; run in Underload, it should be a Underload interpreter.

Your program should work like the hypothetical given above: given a list of languages L1, L2, L3, L4... up to Ln, it should be an L1 interpreter in L1, an L2 interpreter in L2, an L3 interpreter in L3, up to being an Ln interpreter in Ln.

Your program can handle errors in any way. However, it must never output to STDERR (or closest equivalent) if the input given for interpretation has no errors.

It must output to STDOUT (or closest equivalent) and take input from STDIN (or again, closest equivalent).

You can choose any interpretation of a given language, e.g. if your polyglot works in Brainfuck, it can interpret Brainfuck with finite or unbounded tape. You must specify what interpretations you choose to use for each language.

Programs must have at least 2 characters for this challenge to prevent empty programs from competing.

Functions such as exec and eval are allowed.

Only a Turing-complete subset of a language has to be interpreted.

Different languages have to be interpreted, not different language versions.

Your score is C/(L^3), where C is code length, computed in bytes with UTF-8 encoding, and L is the amount of languages. The lowest score wins: if two winning answers have the same score the one posted earlier wins.

Have fun.

Andrew

Posted 2019-12-28T16:44:51.713

Reputation: 2 067

1Are functions such as exec and eval allowed? Does the entire language spec have to be interpreted or just a turing complete subset? – FlipTack – 2019-12-28T17:27:27.010

Yes and yes. Mostly for non-esoteric languages. – Andrew – 2019-12-28T18:47:27.493

4@FlipTack's second question was an "either/or" not a "yes/no"! :D – Shaggy – 2019-12-28T19:35:09.973

"In languages without any method of input (e.g. ///) programs may get input through an insertion into the source code" does this make the empty program a valid submission in some languages? https://codegolf.meta.stackexchange.com/a/10553/89930

– 79037662 – 2019-12-29T18:47:13.603

Answers

6

GolfScript/MathGolf/CJam, 2 bytes, score = 2/27 ≈ 0.074075

The most boring (yet probably the optimal) answer. (CJam's addition made the program longer by 1 byte; but this still contributes to the ending score...)

Going forward to see what is the most common character choice for an evaluation operator...

l~

TIO for MathGolf

TIO for CJam

TIO for GolfScript

What other languages have ~ as an evaluation built-in?

Not adding those languages to the polyglot. Because I'm too lazy.

Explanation (MathGolf and CJam)

l  Read an input
 ~ Evaluate the input

Explanation (GolfScript)

l  Do nothing
 ~ Evaluate the (implicit) input

user85052

Posted 2019-12-28T16:44:51.713

Reputation:

5

Jelly, 05AB1E, GolfScript 8 bytes, score = \$\frac 8 {27} \approx 0.296\$

.Vq;~
ɠV

Try it online Jelly!

Try it online 05AB1E!

Try it online GolfScript!

In all cases, takes a program in the relevant language from STDIN and executes it, with any output going to STDOUT.

Nick Kennedy

Posted 2019-12-28T16:44:51.713

Reputation: 11 829

The scoring has been updated, so this is now 23/27 ~ 0.851 – Lyxal – 2019-12-29T01:43:20.610

Sorry about that. The challenge keeps changing :-/ – Luis Mendo – 2019-12-29T09:59:59.110

3

Keg and Python 3, 19 characters, score = \$ \frac{19}{8} \$

exec(input())
"ø¿ß"

Try it online! (Keg)

Try it online! (Python 3)

Uses the same approach as Nick first did, but with Keg instead.

I'll have to figure out how to add another language to get competitive again.

Lyxal

Posted 2019-12-28T16:44:51.713

Reputation: 5 253

3

GolfScript/Keg, 6 bytes, score = 3/4 = 0.75

Do we have to use at least one practical language? It doesn't seem to say that in the spec.

ß.~\;

Golfscript

ß     # Odd undefined ops
 .    # Copy the input
  ~   # Evaluate it
   \; # Discard the unevaluated copy

Keg

ß     # Take input & evaluate the input
 .    # Output the result as a number
  ~   # Push a random number
   \; # Escape the ; character

user85052

Posted 2019-12-28T16:44:51.713

Reputation: