$ show gren-lang/compiler
This repository is the compiler and build toolchain for the Gren programming language, a functional language forked from Elm. It compiles Gren source code to JavaScript, targeting both Node.js and browser environments. The codebase is split into two parts: a Haskell backend that handles parsing, type inference, optimization, and code generation, and a Gren frontend that provides a CLI for building, running, package management, REPL, and documentation generation. The Gren frontend is self-hostingβcompiled by the compiler itself. The architecture is modular, with clear separation of concerns across parsing, canonicalization, type checking, optimization, and JavaScript generation. The compiler features sophisticated error reporting with pedagogical hints, a custom type system with flexible type variables, and an efficient pattern matching compilation algorithm based on decision trees. The project also includes a builder for incremental compilation and dependency resolution, and a package manager that fetches packages from GitHub. While the codebase demonstrates high craftsmanship in many areas, it is a two-language project with self-hosting complexity and some very large modules.
The LLM identified 5 files worth highlighting for reuse:
A generic union-find (disjoint set) data structure implemented with IORef-based points. It provides efficient union and find operations with path compression. This module is self-contained and can be extracted for any unification problem in compilers, type inference, or graph algorithms.
A custom UTF-8 string type with efficient operations for encoding, decoding, indexing, and binary serialization. It avoids the overhead of Haskell's built-in String type and provides low-level byte manipulation. This module is reusable in any project that needs high-performance UTF-8 handling.
Implements decision tree compilation for pattern matching, based on the algorithm from the paper 'When Do Match-Compilation Heuristics Matter?'. It converts pattern match expressions into efficient decision trees, handling exhaustiveness and redundancy. This algorithm is reusable for any language compiler that needs to compile pattern matching.
Generates source maps in the standard JSON format, including VLQ-encoded mappings. It tracks source locations during JavaScript code generation and produces mappings that allow debugging the original Gren source. This module is reusable for any JavaScript backend that needs source map support.
A low-level parser combinator library that operates on byte-level input with continuation-passing style. It provides primitives for parsing characters, strings, numbers, and whitespace, with detailed error reporting. This library is self-contained and can be extracted for building parsers in other Haskell projects.