3
\$\begingroup\$

This question already has an answer here:

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.
\$\endgroup\$

marked as duplicate by Downgoat, Digital Trauma code-golf Aug 29 '15 at 16:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ 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. ;) \$\endgroup\$ – Martin Ender Aug 29 '15 at 14:19
  • 1
    \$\begingroup\$ Seems 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. \$\endgroup\$ – JohnE Aug 29 '15 at 14:34
  • 2
    \$\begingroup\$ Oh well, this contest is over. Loopholes should be closed not opened. \$\endgroup\$ – Dennis Aug 29 '15 at 14:40
  • \$\begingroup\$ @MartinBüttner I meant bytes! But there's no point editing now, even though the duplicate refers to an old and inactive challenge. \$\endgroup\$ – wizzwizz4 Aug 30 '15 at 7:30
  • \$\begingroup\$ is tcl interp create valid? \$\endgroup\$ – sergiol Jul 6 '17 at 20:46
10
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ Nothing doesn't satisfy our definition of programming language. \$\endgroup\$ – Dennis Aug 29 '15 at 14:36
  • \$\begingroup\$ @Dennis the OP seems to explicitly accept such languages (e.g. HQ9++) \$\endgroup\$ – aditsu Aug 29 '15 at 14:37
  • 11
    \$\begingroup\$ In that case, nothing beats Nothing. \$\endgroup\$ – Dennis Aug 29 '15 at 14:38
  • \$\begingroup\$ @aditsu That was a joke, as there is no way to create an interpreter of HQ9++ in itself. But this is accepted. \$\endgroup\$ – wizzwizz4 Aug 29 '15 at 16:32
3
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ This is a loophole. I'll add it to the Loophole remover. \$\endgroup\$ – wizzwizz4 Aug 29 '15 at 16:33

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