• Stars
    star
    158
  • Rank 237,131 (Top 5 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 8 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimal self-hosted JavaScript compiler in 1k lines of code

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.

Overview:

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)

Usage:

node build

Features:

Pass by reference:
// variables with native types get passed by value in js -
// with 'inout' we tell the compiler to transform passed in variables into referenceable objects
function swap(inout a, inout b) {
  let tmp = a;
  a = b;
  b = tmp;
};
let test1 = 5;
let test2 = 10;
console.log(test1, test2); // 5, 10
swap(test1, test2); // swap both variables
console.log(test1, test2); // 10, 5

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); // here we point to the variable's value
swap(test1, test2); // here we pass the variable's reference
console.log(test1.$iov, test2.$iov); // much hax
Enums:
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.

Stub usage:

let compiler = require("./bin/stub");
compiler.compile("const a = 10;", {
  console: console, // uses log and error method
  error: function(msg) {
    console.error("Error: " + msg); // wat happens on a error
  }
});

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.

More Repositories

1

poxi

A flat pixel art editor
JavaScript
2,479
star
2

Iroh

Dynamic code analysis tool - Exploit, record and analyze running JavaScript
JavaScript
921
star
3

nvk

Vulkan API for JavaScript/TypeScript
C++
915
star
4

PokeMMO

๐ŸŽฎ Pokemon MMO engine with realtime editor
JavaScript
728
star
5

POGOserver

Pokemon GO server emulator
JavaScript
460
star
6

azula

Lightweight GPU accelerated HTML GUI for native JavaScript
C
320
star
7

dawn-ray-tracing

Hardware Ray tracing extension for Chromium WebGPU
C++
263
star
8

wasm-particles

WASM accelerated particles
JavaScript
253
star
9

mini-c

C to WebAssembly compiler
JavaScript
250
star
10

webgpu

WebGPU for Node [Deprecated, Unmaintained]
C
243
star
11

rokon

3D engine using WebGL2, WebAssembly
JavaScript
233
star
12

glmw

WebAssembly based Matrix and Vector library
C
200
star
13

WebGPU-Path-Tracer

Toy path tracer using WebGPU RT
JavaScript
135
star
14

tiny-rtx

VK_NV_ray_tracing based Ray tracer
JavaScript
100
star
15

TXT2PNG

๐Ÿ—ป Store ASCII inside PNGs
JavaScript
73
star
16

momo

A Vulkan RTX Path Tracer
JavaScript
69
star
17

hevia-parser

A recursive descent Swift parser written in ES6
JavaScript
63
star
18

nvk-examples

Examples and demos for nvk
JavaScript
58
star
19

chromium-ray-tracing

Chromium build with hardware accelerated ray tracing
57
star
20

pokemon-emerald-rom-hacking

ROM hacking kit for Pokemon Emerald
JavaScript
50
star
21

VK_KHR_ray_tracing

VK_KHR_ray_tracing example
C++
42
star
22

YUE

My personal WebGPU based 3D renderer
TypeScript
38
star
23

html-canvas

Rendering HTML into canvas
JavaScript
26
star
24

canvas-live-stream

Live streaming canvas over websockets
JavaScript
26
star
25

webgpu-examples

Examples for node-webgpu
JavaScript
24
star
26

hevia-compiler

Strongly typed language that compiles to JavaScript
JavaScript
17
star
27

toy-compiler

A tiny self-hosted compiler
JavaScript
16
star
28

emerald-engine

JavaScript
16
star
29

POGO-asset-downloader

๐Ÿš€ Download Pokemon GO 3D models
JavaScript
16
star
30

Lamella

โšช Flow based Visual Programming
JavaScript
12
star
31

tolw

WebGL .obj loader - WebAssembly port of tinyobjloader
JavaScript
11
star
32

src2game

Turn Javascript source code into a Game
JavaScript
10
star
33

nvk-optix-denoiser

Real-time Denoiser for node-vulkan
JavaScript
10
star
34

3d-code-vizard

3D runtime code visualization
JavaScript
9
star
35

object-reflector

Simple & powerful Object reflections
JavaScript
8
star
36

htmlparse

Minimal blazing fast HTML parser
JavaScript
7
star
37

space-shooter

A simple space shooter
JavaScript
7
star
38

banotils

Utilities for the banano cryptocurrency
TypeScript
6
star
39

bitpackr

An efficient low-level packet serializer, down to bit-level
TypeScript
6
star
40

bnsh-decoder

A decoder for Nintendo Switch BNSH shaders
C++
5
star
41

Watcher

Track all changes inside your DOM
JavaScript
5
star
42

webgpu-compute-rasterizer

A simple compute based rasterizer
JavaScript
5
star
43

PokeName

Fiddle with Pokรฉmon names in different languages
JavaScript
4
star
44

Calc

Browserbased Excel
JavaScript
4
star
45

xr-engine

WebXR engine
JavaScript
3
star
46

POGOdecode

Pokemon GO Request<->Response decoder
JavaScript
3
star
47

nvk-essentials

Essential development tools for nvk
GLSL
3
star
48

Interpreter

A small interpreter with asynchronous step based execution support
JavaScript
2
star
49

wast-parser

WebAssembly WAST parser
JavaScript
2
star
50

neural-ocr

Neural network OCR
JavaScript
2
star
51

mc-spawner-upgrade

Lets you upgrade your mob spawners in minecraft
Java
2
star
52

pawtils

Utilities for the paw cryptocurrency
TypeScript
2
star
53

nanpow

TypeScript
1
star
54

Music-Box

Programmable wave music box
JavaScript
1
star
55

Expanding-spiral-animation

Canvas based expanding spiral experiment written in ES6
JavaScript
1
star