Interpreted Interpreter

3

Duplicate? I knew somebody must've done this before.


Programming a programming language in itself is something that has interested me since I knew what a programming language was. So I thought I'd create a Code Golf challenge for you.

Challenge

Create an interpreter for a given programming language, programmed entirely in itself. The interpreter should be able to run any valid program in that language, including itself.

Inputs:

The program can take it's input as a file passed as a parameter (e.g. python myfile.py) or as lines from stdin (cat myfile.py | python).

Outputs:

Whatever an interpreter would normally output, on the correct streams (stdout, stderr etc.)

Specifications

  • The code needs to be written in an interpretable language, and serve as an interpreter for its own language.
  • It can't use an eval() function to interpret code.
  • It must run any valid program in that language. If the language allows external modules (or includes), it must be able to read those modules.
  • This task is code golf. The submission with the least characters that is correct wins. In case of a tie, the solution that was submitted first wins.

Loophole remover:

  • Yes, if you can interpret Java, Bytecode or HQ9++ in themselves, go ahead!
  • In JavaScript / ES, using HTML tags such as <script> is not allowed.

wizzwizz4

Posted 2015-08-29T14:05:51.730

Reputation: 1 895

Question was closed 2015-08-29T16:28:21.697

Our default scoring method is bytes, not characters, because scoring in character will likely get you answers like this. You are free to choose characters over bytes if you really want to, but I thought I'd let you know about the consequences. ;)

– Martin Ender – 2015-08-29T14:19:17.010

1Seems like this might be a better popularity contest than code golf- there are many extremely degenerate non-general esolangs which have simple solutions to this problem and more interesting languages have no chance of competing. – JohnE – 2015-08-29T14:34:01.663

2Oh well, this contest is over. Loopholes should be closed not opened. – Dennis – 2015-08-29T14:40:44.023

@MartinBüttner I meant bytes! But there's no point editing now, even though the duplicate refers to an old and inactive challenge. – wizzwizz4 – 2015-08-30T07:30:14.470

is tcl interp create valid? – sergiol – 2017-07-06T20:46:34.043

Answers

10

Nothing, 0 chars

The source code is an empty file. It does nothing, like any Nothing program, therefore it is the correct execution of every such program.

aditsu quit because SE is EVIL

Posted 2015-08-29T14:05:51.730

Reputation: 22 326

2

Nothing doesn't satisfy our definition of programming language.

– Dennis – 2015-08-29T14:36:32.153

@Dennis the OP seems to explicitly accept such languages (e.g. HQ9++) – aditsu quit because SE is EVIL – 2015-08-29T14:37:41.997

11In that case, nothing beats Nothing. – Dennis – 2015-08-29T14:38:41.163

@aditsu That was a joke, as there is no way to create an interpreter of HQ9++ in itself. But this is accepted. – wizzwizz4 – 2015-08-29T16:32:24.287

3

JavaScript ES6, 44 bytes/chars

s=>document.write(`<${t='script>'}${s}</`+t)

Defines a function, taking the code string as a parameter.

How it works:

This function simply takes the code, wraps it in <script> tags, and writes it into the page. It doesn't use the eval function at all - the browser simply runs the code when the new <script> element is inserted into the page.

Tested in the FireFox console on the about:blank page as follows:

(s=>document.write(`<${t='script>'}${s}</`+t))("alert('hi')")


Yes, this is probably a loophole and as such I don't really expect to win. (It's impossible to beat the Nothing answer, anyway...)

If anything, hopefully it shows how it is possible to evaluate code in javascript without the use of the eval function.

jrich

Posted 2015-08-29T14:05:51.730

Reputation: 3 898

1This is a loophole. I'll add it to the Loophole remover. – wizzwizz4 – 2015-08-29T16:33:16.360