This is an experimental source-to-source compiler that understands basic JavaScript and offers two extra language features on top. The compiler is bootstrapped (able to compile itself) and written in only 1k lines of code.

I created this project to give a basic, not too much

time-consuming overview, how compilers like Babel work behind the scenes.

bin/stub contains the latest generated version (the “compiler.exe”)

src/index contains the compiler source code we can edit (gets compiled by our stub)

node build

Pass by reference:

function swap ( inout a, inout b ) { let tmp = a; a = b; b = tmp; }; let test1 = 5 ; let test2 = 10 ; console .log(test1, test2); swap(test1, test2); console .log(test1, test2);

Compiles into:

function swap ( a, b ) { let tmp = a.$iov; a.$iov = b.$iov; b.$iov = tmp; }; let test1 = { $iov: 5 }; let test2 = { $iov: 10 }; console .log(test1.$iov, test2.$iov); swap(test1, test2); console .log(test1.$iov, test2.$iov);

enum Direction { Up = 0 , Down, Left, Right } let dir = .Up || Direction.Right;

Compiles into:

var Direction; ( function ( Direction ) { Direction[Direction[ 'Up' ] = 0 ] = 'Up' ; Direction[Direction[ 'Down' ] = 1 ] = 'Down' ; Direction[Direction[ 'Left' ] = 2 ] = 'Left' ; Direction[Direction[ 'Right' ] = 3 ] = 'Right' ; })(Direction || (Direction = {})); let dir = 0 || 3 ;

How it works:

The first version of the compiler was written in very simple javascript, using only explicitly necessary language features. The compiler got extended more and more until it was able to parse the required subset of javascript language features, which are needed to parse and transform strings. In the next step the code generator got added which spits out plain javascript without any formatting. This code then turned into the stub file as well as remains our source file. Now it was easy to add new features to the source file. To add a new feature, we have to blindly extend the compiler without using the new feature in the compiling process, but we can test the new feature at the end of the source file. As soon as no errors gets thrown in the bootstrapping process, we can make use of our new added language feature directly inside our compiler source.

Stub usage:

let compiler = require ( "./bin/stub" ); compiler.compile( "const a = 10;" , { console : console , error: function ( msg ) { console .error( "Error: " + msg); } });

See toy-compiler for a more extended version offering classes and a simple preprocessor.

Another compile-to-js project is this one, which offers a Swift-like language with type inference, custom operators, pass-by-reference etc.