This is a tiny interpreter (with a bytecode compiler and a stack-based virtual machine) for a lilliputian subset of JavaScript, written in C and... JavaScript itself. It fits in less than 2500 lines of code.
The parser and the bytecode generator (in compile.js
) are written in
Toy-compatible JavaScript. It was just too painful to write those in
C. Since you can run them with Node.js or Toy itself, they
self-compile (into compiler_code.c
) and are magically bundled inside
the toy
executable.
- Closures
- Compiles itself (see above)
- Little mark-and-sweep garbage collector
- ES201{5,6,7}
- Functions with many arguments (use lists, dictionnaries or curryfication instead)
- Prototypes
- Exceptions
undefined
(seriously, who needsundefined
?null
is sufficient)- Optional semicolons
- Booleans (use 0 and 1, or anything else truthy or falsy)
for
,switch
,do
else
(just writeif
statements. Thousands ofif
statements.)- Regexps
You need Node.js, a C compiler and GNU make. Just run make
and try
the examples.
First of all, because I hate naming things à la JavaScript:
- an object, a hash map or a hash table is dictionnary
- an array is a list
For the sake of simplicity, the whole thing is really slow.
The dictionnary implementation is a shame.
Lists are implemented with those dictionnaries. This is really straightforward since we need dictionnaries and JavaScript lists behaves mostly the same. It’s obviously slow.
Since the garbage collector does not visit the stack, each object has
a reference counter which prevents it from being collected if that
counter is nonzero. Moreover, the GC must not run at any time, but
only between two instructions (see the call to
request_garbage_collection();
in vm.c
).
Variables are compiled as-is. The VM interprets scoping rules and catches variable definition errors at run-time. The compiler is really straightforward.
I just hope you are not crazy enough to use this hack in production.
It was fun. Enjoy 😉