$ show melange-re/melange
Melange is an OCaml-to-JavaScript compiler that compiles a large subset of OCaml (including the standard library and the Belt data structures library) to readable JavaScript. It is a fork of ReScript (formerly BuckleScript) with a focus on OCaml ecosystem compatibility, integration with Dune build system, and support for OCaml's module system (including first-class modules and virtual libraries). The compiler features a multi-pass lambda IR transformation pipeline, a PPX rewriter for Melange-specific attributes (e.g., [@mel.module], [@mel.send]), and extensive runtime support for JavaScript interop, including bindings for Node.js, DOM APIs, and ES module systems. The codebase is structured as a monorepo with several components: the compiler core (jscomp/core/), the PPX preprocessor (ppx/), the standard library fork (jscomp/stdlib/), the Belt library (belt/), runtime libraries (jscomp/runtime/), and a test suite with both unit tests (via the Mt framework) and blackbox tests (.t files). The build system uses Dune and opam, with an optional Nix flake for reproducible builds. The repository demonstrates strong craftsmanship in its compiler passes (alpha conversion, dead code elimination, arity analysis, constant folding) and in the design of JavaScript runtime primitives. However, there is some technical debt: several FIXME and TODO comments, conditional compilation for OCaml version compatibility, a few vendored components (Flow parser) that may drift from upstream, and some generated files that require regeneration when source definitions change.
The LLM identified 3 files worth highlighting for reuse:
Implements a polymorphic mutable set using bucket arrays with dynamic resizing. Provides add, remove, check, forEach, size, clear, and iteration. The implementation is self-contained and can be extracted as a standalone data structure for any OCaml project needing a mutable set with good average-case performance, independent of Melange's compiler internals.
Implements Tarjan's strongly connected components algorithm in a generic way using a module signature for graph representation. Computes SCCs and returns a topological ordering. This module is self-contained (depends only on standard OCaml data structures) and can be reused in any project requiring graph decomposition, such as dependency resolution or module ordering.
Provides efficient array sorting via merge sort, along with set operations (union, intersect, diff) on sorted arrays, and binary search. The implementation is generic over a comparator and uses in-place mutation for performance. It can be extracted as a standalone sorting and set utility library for OCaml projects targeting JavaScript or native execution.