MLIR, Nanopass, and Green-Red Trees: Compiler Ideas That Make Code Generators Better
Most code generators are monolithic. A single class reads some input, makes a bunch of decisions, and spits out source files. It works until it doesn't — until you need to support another target la...

Source: DEV Community
Most code generators are monolithic. A single class reads some input, makes a bunch of decisions, and spits out source files. It works until it doesn't — until you need to support another target language, until you need incremental builds, until a 2000-line generator method becomes everyone's least favorite file to touch. I hit this wall while building Proto Wrapper Plugin, a tool that generates version-agnostic Java wrappers from multiple protobuf schemas. The generator worked fine, but every new feature meant wrestling with a monolith. Adding Kotlin support would have meant duplicating the entire thing. So I went looking for better ideas. I found three — from compiler design, of all places — and they changed how I think about code generation entirely. Update: These ideas have since been implemented as IRCraft — a standalone Scala 3 library for multi-language code generation with MLIR-inspired dialects, nanopass pipelines, and immutable green trees. The Three Concepts Concept Origin W