Based on a comment by George Edison to this question, write the smallest self-interpreting interpreter.

  • You may use the language of your choosing.
  • Empty languages do not count. Your program must be at least two characters long.
  • The program does not need to interpret the entire language, just a Turing-complete subset of language features (that contains the interpreter).
  • Quines don't count.
  • Do not use your language's built-in eval function or equivalent. Same goes for apply, etc.
  • 1
    \$\begingroup\$ (Hmm.. I should do something with /usr/bin/cat) what about Turing-completeness? \$\endgroup\$ – Ming-Tang Feb 6 '11 at 2:36
  • \$\begingroup\$ @ SHiNKiROU: Thanks, I didn't think of that as a test. Updated. \$\endgroup\$ – Hoa Long Tam Feb 6 '11 at 2:57
  • \$\begingroup\$ Related: Language w/ the smallest interpreter written in itself on Stack Overflow, though are are few (only one?) answers that actually abide by the rules given here. \$\endgroup\$ – dmckee --- ex-moderator kitten Feb 6 '11 at 3:56
  • 1
    \$\begingroup\$ Do we have to rewrite that Scheme sexp parser, or can we consider the host language's ok? \$\endgroup\$ – J B Feb 6 '11 at 15:40
  • \$\begingroup\$ @J B: The language's string processing utilities are fine, including the sexp parser. \$\endgroup\$ – Hoa Long Tam Feb 6 '11 at 20:22

CI - 260


320 → 260: Push simple character-to-instruction mappings, then fold over them. This halves the code size per case (there are 18 cases), but costs 30 characters to do the folding.

This is another one of my constructed languages, (base interpreter hosted on Gist). It's unique in that the language reifies code fragments. That is, strings of instructions in this stack-based language are used to the same effect as data structures or closures in other languages:

1^      # Push a 1, and "lift" it to be a code literal.
(5 +)   # Define a code literal.
&       # Join the two code literals, forming (1 5 +)
$       # Execute the code literal.

The interpreter builds a code fragment of the entire program before running it, so it can also be considered a compiler. Because of this, stacking the interpreter does not result in exponential run-time overhead.

The interpreter derives all of its operators directly from the host interpreter. However, it does the parsing by itself, so the majority of the code is just sequences that translate characters into their respective code literals. This isn't the same as using eval, but it reveals how dependent any programming language implementation is on the semantics of its host language/architecture.

Language reference:

Get the interpreter here


  • ( ... )

    Create a "block", which is effectively a list of instructions with no context. Internally, it could even be machine code.

  • block $

    Call a block. The callee is handed the global stack, which includes the block being called.

  • value ^

    Lift a value. Namely, turn it into a block that pushes that value.


       1 ^
    == (1)
  • block1 block2 &

    Join two blocks, forming one that runs both in sequence.


       (1) (2) &
    == (1 2)

Stack manipulation

  • n c

    Copy the nth value of the stack.


       5 4 3 2 1 0 3c
    == 5 4 3 2 1 0 3
  • n p

    Pluck the nth value of the stack (remove it, and bring it to front).


       5 4 3 2 1 0 3p
    == 5 4 2 1 0 3
  • n d

    Drop n values from the stack. 0d is a no-op.


       5 4 3 2 1 0 3d
    == 5 4 3

Relational operators

  • a b (on_true) (on_false) =

    Test if a equals b. Consume all but the first argument, and call on_true or on_false. If one argument is zero and the other is any other type, the result will be false. Otherwise, a and b must be integers.


       3 3 ('0+ .) (1d) =
    == 3 '0+ .
  • a b (on_true) (on_false) <

    Test if a is less than b. a and b must be integers.


       3 5 (1d 5) () <
    == 3 1d 5
    == 5
  • a b (on_true) (on_false) >

    Test if a is greater than b. a and b must be integers.


       3 5 (1d 5) () >
    == 3
  • a lo hi (on_true) (on_false) ~

    Test if lo <= a <= hi. a, lo, and hi must be integers.


       3 0 10 ('0+ .) (1d) ~
    == 3 '0+ .


  • c .

    Put the character c (consuming it from the stack).

  • ,

    Get a character and push it to the stack. If the end of file has been reached, -1 is pushed.

  • c !

    Unget a character. Just like ungetc in C, only one pushback is allowed.

Integer literals

  • 'c

    Push the character c.

  • [0-9]+

    Push a decimal integer.


  • a b +
  • a b -
  • a b *

    Add/subtract/multiply two numbers.


       3 5 + 7 3 + *
    == 80
  • a b /

  • a b %

    Division and modulus. Unlike in C, these round toward negative infinity.


  • code # comment

    The # character comments out everything to the end of the line.

  • )

    Used to end blocks. Can be used to end the whole program, too.

  • All other characters are ignored.


Binary Lambda Calculus, 232 bits (29 bytes)


See http://en.wikipedia.org/wiki/Binary_lambda_calculus#Lambda_encoding for details

  • 2
    \$\begingroup\$ Why isn't this the accepted answer D: BLC is amazing! \$\endgroup\$ – cat Mar 18 '16 at 22:06
  • \$\begingroup\$ Can you explain it at all? \$\endgroup\$ – PyRulez Jun 13 '17 at 0:48
  • 1
    \$\begingroup\$ Unfortunately, Wikipedia removed the Binary Lambda Calculus page. My tromp.github.io/cl/cl.html page links to a preserved copy and to a paper I wrote explaining the workings of the interpreter. \$\endgroup\$ – John Tromp Jun 14 '17 at 7:49

I can't take credit for this one, but I thought I would share this amazing one:

Brainf*** (423)


BlockScript - 535


BlockScript is a trivial spaghetti stack-based language I created specifically for this challenge. The base interpreter is blockscript.c .

Sample program (prints the first 15 Fibonacci numbers):

0 1 15d!

The interpreter reads both source code and program input from standard input, in that order. This means that to run an interpreter within an interpreter within an interpreter, simply copy and paste:

# Level 1

# Level 2

# Level 3
0 1 15d!

Like the movie Inception, you pretty much can't go any deeper than three levels. It's not a matter of time, but space. BlockScript leaks memory profusely, and this has to do with how the language itself is designed.

Language reference:

Get the interpreter here

In BlockScript, the "stack" is not an array that is overwritten by subsequent operations like you may be used to. It is actually implemented as an immutable linked list, and a stack persists for the duration of the program. Also, no operator (except @) removes values from the stack. However, stack modifications only affect the block in which they occur.

Value selection

  • a through z

    Fetch the 0-25th item from the stack, and push it to the stack. a refers to the head, or most recently pushed item, of the stack.

  • A through Z

    Fetch the 0-25th item of the current frame, and push it to the stack.

  • [

    Open a "frame" to select items from the stack reference (see below) on the head of the stack. [ doesn't require a matching ], but frames are lexically scoped. In BlockScript, "scope" is determined by braces ({ ... }) that form blocks. Thus, opening a frame inside of a block will have no effect on code outside of the block.

  • ]

    Close the current frame, returning to the previous frame (if any).


  • { ... }

    Create a "block", and push it to the stack. Inside a block, the stack will start at what it was before the block, except the stack of the caller will be pushed on top. Stacks are persistent and immutable in BlockScript, so blocks are closures. The idiom {[ means open a block, then open a frame to start selecting arguments (using A through Z). The return value of a block is the head of the stack when } is reached.


    '3 '2 '1 {[ b. d. f. B. C. D. A! } 'D 'C 'B d!;

    This prints 123BCD123DCB123BCD123DCB… . The lowercase letters refer to stack values, while the uppercase letters refer to arguments (because the frame is set to the stack of the caller). A! takes the head of the caller (which is guaranteed to be the block being called) and calls it. If you're wondering why it reverses BCD every other time, it's because B. C. D. pushes those arguments in reverse order right before the block calls itself.

  • !

    Call a block. Push the return value to the stack.

Stack references

  • &

    Create a stack reference, and push it to the stack. Think of this as "super-cons", as it effectively takes every item on the stack and forms a "tuple" out of it. The idiom &[ means that whatever a, b, c referred to before can now be accessed with A, B, C (for the remainder of the block or until ] is encountered).

    In part because & captures more values than it usually needs, BlockScript leaks memory by design.

  • @

    Switch to the stack pointed to by the stack reference a. This operator is rather weird, but the BlockScript self-interpreter uses it a couple times to avoid having to push the same arguments twice. The effects of @ (or any stack operation, for that matter) are confined to the block in which it is invoked. Also, the frame is unaffected by @, so the frame can be used to grab values you need after switching stacks.

Conditional expression

  • ? <on true> : <on false>

    Conditional expression, just like the ternary operator in C. That is, if a is "true" (that is, not equal to the integer zero), then do <on true>, otherwise do <on false>.


Note: Input and output are done in UTF-8. A "character" is an integer corresponding to a Unicode index.

  • ,

    Get the next character of input, and push it to the stack. If the end of input is reached, push -1 instead.

  • .

    Output the character on the head of the stack.

Integer/character literals

Note: Integers and characters are the same thing in BlockScript.

  • 'c

    Push the character c.

  • [0-9]+

    Push a decimal integer.


These operators only work on integer values.

  • + Compute b + a (pushing the result, but not discarding either value).
  • - Compute b - a.
  • * Compute b * a.
  • / Compute b / a (integer division; rounds toward negative infinity).
  • % Compute b % a (integer modulus; rounds toward negative infinity).

Relational operators

These operators only work on integer values.

  • < If b is less than a, push 1, else push 0.
  • >
  • =


  • # Comment to end of line
  • The program must end with ;
  • All other characters are ignored.
  • 2
    \$\begingroup\$ Christ. Care to share the spec like you did for CI? \$\endgroup\$ – Casey Apr 28 '11 at 1:19
  • \$\begingroup\$ @Casey: Added a reference. \$\endgroup\$ – Joey Adams Apr 28 '11 at 4:03
  • 1
    \$\begingroup\$ Would you be interested in knowing that you're dreaming? ...At level 4. \$\endgroup\$ – Mateen Ulhaq Apr 30 '11 at 5:49

Zozotez LISP: 414

linefeeds added to get a nice block is not needed and not counted.

((\(E V A L)(E(r)'(())))(\(x e)(?(s x)(V x e)((\(b j)(?(= b ")(a j)(?(= b \)x
(?(= b ?)(?(E(a j)e)(E(a(d j))e)(E(a(d(d j)))e))(?(s b)(A b(E(a j)e)(E(a(d j)
)e))(E(a(d(d b)))(L(a(d b))j e)))))))(E(a x)e)(d x))))(\(x g)(? g(?(= x(a(a
g)))(d(a g))(V x(d g)))x))(\(f h v)(?(= f r)(r)(?(= f p)(p h)(?(= f s)(s h)(?
(= f a)(a h)(?(= f d)(d h)(?(= f =)(= h v)(c h v))))))))(\(k v i)(? k(L(d k)(
d v)(c(c(a k)(E(a v)i))i))i)))

In theory it should be able to run itself, but since the original interpreter is a BrainFuck binary and itself an interpreter I have only been able to test each part. When given itself and a simple expression (p p) I think it needs more time than the 40 minutes I have waited so far and I'm using my fast jitbf to run it which (mis)uses Perl Inline-C to run C code on the fly.

It's impossible to implement the whole Zozotez in Zozotez since it does not have means of mutating cons and : (setq/define) need that to update bindings. I also didn't implement explicit begin/progn or &rest argument, macros and special printing arguments since I didn't use it in the interpreter. I did include p (print) even though I don't use it so programs need to explicit print their calculations just like the original interpreter.

The same ungolfed:

;; A stand alone Zozotez script need to be
;; contained in one expression, here with
;; all functions provided as arguments to
;; get them bound in the dynamic environment
((\ (E V A L)
 ;; E (EVAL)
 (\ (x e)
   (? (s x)
      (V x e)
      ((\ (b j)
         (? (= b ") (a j)
         (? (= b \) x
         (? (= b ?) (? (E(a j)e) (E(a(d j))e) (E(a(d(d j)))e))
         (? (s b)
            (A b (E(a j)e) (E (a(d j))e))
            (E (a(d(d b))) (L(a(d b))j e)))))))
       (E (a x) e)(d x))))
 ;; V (VALUE / symbol->value)
 (\ (x g)
   (? g
      (? (= x (a (a g)))
         (d (a g))
         (V x (d g)))
 ;; A (APPLY) but just for primitives
 (\ (f h v)
   (? (= f r) (r)
   (? (= f p) (p h)
   (? (= f s) (s h)
   (? (= f a) (a h)
   (? (= f d) (d h)
   (? (= f =)
      (= h v)
      (c h v))))))))
 ;; L ( joint evLis / extend-env)
 (\ (k v i)
   (? k
      (L (d k) 
         (d v)
     (c (c (a k) 
           (E (a v) i)) 

CHIQRSX9+ (probably non-competing), 2 bytes


There's no way to write a self-interpreting interpreter in this HQ9+-based language without using I, which runs a built-in interpreter that processes STDIN.

  • 1
    \$\begingroup\$ Nowhere in the rules does it say that built-in self interpreters are disallowed. It says for eval, which is for expressions, not programs. \$\endgroup\$ – Erik the Outgolfer Jun 7 '16 at 16:45
  • \$\begingroup\$ How does one compute primes in this language? \$\endgroup\$ – pppery Jun 13 '16 at 13:49
  • \$\begingroup\$ @ppperry X is supposed to make the language Turing-complete (thus able to compute primes) in an implementation-dependent manner. \$\endgroup\$ – user8397947 Jun 13 '16 at 19:50
  • \$\begingroup\$ Acording to the Esolang page, the Perl interpreter's X command randomly perturbs the program and the executes it, meaning that one cannot use than command to deterministically compute primes. Can you give me of an example interpreter that lets you use X in the way you specified? \$\endgroup\$ – pppery Jun 13 '16 at 20:03
  • \$\begingroup\$ @ppperry Apparently the interpreter written in Perl is the only interpreter, so no. Also, what if there's a program that computes primes when "randomized" with a specific value? \$\endgroup\$ – user8397947 Jun 13 '16 at 21:28

Concurrent Filesystem Befunge 98 - 53\18 bytes (almost certainly cheating)

Full 53 byte interpreter with no restrictions (although I haven't tested complicated timing interactions involving IP splitting and wrapping):

v ;;;;;;;;

Reads input from a file named a and executes it. It isn't specifed in the rules that we can't use self-modifying code.

18 byte interpreter that does not allow wrapping (the IP moving of one edge of the code and starting at the opposite edge):


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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