r/Compilers • • Sep 10 '26

An experimental C-like language with a different approach to memory safety (no GC, no borrow checker)

Post image
0 Upvotes

I wanted something that feels as simple as C while making memory bugs fail predictably instead of turning into undefined behavior.

That's why I started building hc2.

hc2 is a small experimental programming language with runtime-checked pointers. Every pointer carries its bounds, mutability, and allocation it belongs to. Out-of-bounds accesses, use-after-free trap immediately instead of silently corrupting memory. The cost is a few extra instructions per memory access.

There is no GC, no borrow checker, and no lifetime annotations. Rather than preventing these bugs at compile time, hc2 detects them when they occur and turns undefined behavior into runtime failures.

I'd like to hear your thoughts and feedback.

GitHub: https://github.com/hc2lang/hc2


r/Compilers • • Aug 17 '26

Can Sanskrit work as a natural programming language?

5 Upvotes

I’ve been experimenting with this idea by building a Sanskrit compiler based on Pāṇinian grammar.

Instead of treating Sanskrit only as text to interpret, the compiler parses grammatically structured Sanskrit and turns the instructions into executable operations.

The interesting part for me is whether Pāṇini’s formal grammatical system can provide enough structure to bridge natural language and programming languages deterministically.

I now have a working implementation and would be interested in hearing what others think about this approach.

Website: https://panini.cc/
GitHub: https://github.com/kaushalbx/paninivm

Article: https://medium.com/@kaushalbx/p%C4%81%E1%B9%87inivm-building-a-natural-programming-language-with-sanskrit-grammar-aa82b855074c
Article: https://medium.com/@kaushalbx/building-p%C4%81%E1%B9%87inivm-compiling-2-500-year-old-paninian-grammar-into-an-executable-kotlin-engine-5a6bb8de20fb


r/Compilers • • Aug 13 '25

Is "Written in Rust" actually a feature?

Thumbnail reddit.com
1 Upvotes

r/Compilers • • May 31 '26

Is My lang readable?

0 Upvotes

You know how you can look at a language like Python and still understand what’s going on even if you don’t really know it?The more I look at my lang, the more I feel like the syntax is kind of horrendous. Take a look at some of the .pile files https://github.com/NoTimeDev/pile let me know what you think, it’s stack based so it doesn’t really help the syntax look any better, lol.


r/Compilers • • Feb 19 '26

The Claude C Compiler: What It Reveals About the Future of Software - Chris Lattner

Thumbnail
modular.com
0 Upvotes

r/Compilers • • Dec 16 '25

Stop building compilers from scratch: A new framework for custom typed languages

0 Upvotes

Hey everyone,

After two years of development, I’m excited to share Tapl, a frontend framework for modern compiler systems. It is designed specifically to lower the friction of building and experimenting with strongly-typed programming languages.

The Vision

Building a typed language from scratch is often a massive undertaking. Tapl lowers that barrier, allowing you to focus on experimenting with unique syntax and type-checking rules without the usual boilerplate overhead.

A Unique Compilation Model

TAPL operates on a model that separates logic from safety by generating two distinct executables:

  • The Runtime Logic: Handles the actual execution of the program.

  • The Type-Checker: A standalone executable containing the language's type rules.

To guarantee safety, you run the type-checker first; if it passes, the code is proven sound. This explicit separation of concerns makes it much easier to implement and test advanced features like dependent and substructural types.

Practical Example: Extending a Language

To see the framework in action, the documentation includes a walkthrough in the documentation on extending a Python-like language with a Pipe operator (|>). This serves as a practical introduction to customizing syntax and implementing new type-checking behavior within the framework.

👉View the Tutorial & Documentation

Explore the Project

TAPL is currently in its early experimental stages, and I welcome your feedback, critiques, and contributions.

I look forward to hearing your thoughts on this architecture!


r/Compilers • • May 26 '26

IRON: A DSL that can convert source code into IR extremely quickly.

1 Upvotes

IRON a.k.a. Intermediate Representation Object Notation is a interpreter/database, that takes source code, matches it to a database and outputs the IR.

It gives you full control of the database and what IR is outputted.

In 55 micro seconds it was able to parse this source code:

turn 128bit double float xmm1 into replicate of xmm2
turn 128bit double float xmm1 into xmm2
turn 128bit float xmm1 into even replicate xmm2
turn 128bit xmm1 into xmm2
turn 128bit aligned xmm1 into xmm2
turn 16bit xmm1 into xmm2
turn 64bit xmm1 into xmm2

and output this assembly:

movddup xmm1, xmm2
movupd xmm1, xmm2
movsldup xmm1, xmm2
movdqu xmm1, xmm2
movdqa xmm1, xmm2
mov word xmm1, xmm2
mov qword xmm1, xmm2

It's made 100% in assembly, with no external libraries.

This is the Repo: https://github.com/dogmaticdev/IRON

This is the example in full: https://github.com/dogmaticdev/IRON/blob/main/examples/example.md


r/Compilers • • Jul 31 '25

How will AI/LLM affect this field?

0 Upvotes

Sorry if this has been asked multiple times before. Im currently working through crafting interpreters, and Im really enjoying it. I would like to work with compilers in the future. Dont really like the web development/mobile app stuff.

But with the current AI craze, will it be difficult for juniors to get roles? Do you think LLM in 5 years can generate good quality code in this area?

I plan on studying this for the next 3 years before applying for a job. Reading stroustrup's C++ book on the side(PPP3), crafting interpreters, maybe try to implement nora sandler's WCC book, college courses on automata theory and compiler design. Then plan on getting my hands dirty with llvm and hopefully making some oss contributions before applying for a job. How feasible is this idea?

All my classmates are working on AI/ML projects as well. Feels like im missing out if I dont do the same. Tried learning some ML stuff watching the andrew ng course but I am just not feeling that interested( i think MLIR requires some kind of ML knowledge but I havent looked into it)


r/Compilers • • Oct 06 '24

Build a compiler with python?

2 Upvotes

Is it possible that I can build a compiler from scratch in python? And if so, can anyone tell me how can I make it because I have an assignment in university 😭


r/Compilers • • Apr 24 '23

Command Line Compilers

0 Upvotes

This is about how bare compilers are typically invoked from the command line.

While most invocations might be via a IDE or hidden within a build process, when you need to use them from within a console or terminal, it should be as simple as possible.

(This comes up for me when compiling dozens of tiny, ad hoc test programs across languages and across compilers. A formal build process wouldn't work.)

I got a thrashing in the C group yesterday when I suggested that gcc was backward. I highlighted two issues: one was that it needs the -lm option when an application uses maths functions, something you see nowhere else, and only on Linux. (This doesn't affect me much, but it keeps coming up on forums because some program won't link.)

The other was that it has a penchant for naming every executable a.out or a.exe unless overridden, also at odds with other language implementations and also with other C compilers. It means an annoying exception when working with multiple compilers, and complicates any build instructions I provide with C distributables.

There was one more I didn't mention, because it is so widespread, which is requiring the file extension of the source files to be typed.

I've heard every argument for why using a.out is a good idea, even when the program consists of one module. And also for why specifying file extensions is essential.

So gcc hello won't work; you need gcc hello.c. And if you want the output to be called hello.exe instead of a.exe, you must type gcc hello.c -ohello.exe. But then, somebody mentioned make; apparently if you do this:

make hello

it compiles hello.c to hello.exe, without using an extension, and without needing to specify the output; magic! Suddenly all those arguments go out the window. (Sadly it doesn't fix -lm, and it doesn't really work on Windows (I get an error). You don't really know what compiler will be used either.)

So, out of interest, how does your stuff work? Do you need the equivalent of that outdated -lm when a program calls pow()? How does it work out the name of the executable when the input consists of multiple modules?

(I assume it will require file extensions so I won't labour that point. I think Linux needs them more because there, prog and prog. are distinct files so there is an ambiguity.)

My own tools of course require the absolute minimum to build anything, and that's usually 2 pieces of information to type at the console: the name of the compiler, and the name of the lead module, for example:

mm prog                # Submit prog.m to compiler; produce prog.exe

No file extension is needed; this is a whole program compiler that only takes one input file with extension expected to me .m.

(Extra info such as external libraries tends to be specified inside source files. Tools that turn multiple inputs into one output name it after first input file. Anything different, then you supply overrides or extra info.)


r/Compilers • • Aug 09 '26

Overkilled Ray Tracing Compiler

0 Upvotes

Hello, I wanna share something I've been working on for the past 8 months, out of curiosity. I shared this in r/rust communityso I'm skipping details about the other stuff and just compiling here. The project name is Razz. Frankly, I thought it sounded cool, then I searched for its potential meaning, which means playful, matching perfectly with my project's intention.

My proudest achievement yet is a compiler. I learned about compilers in class, but thought it didn't go as deep as I wanted to, so I took matters into my own hands. The Razz Compiler is a fully handwritten (mostly, I'll talk about it in a bit) pipeline, from Lexer -> Parser (LL(1.5), could have been LL(1) but I'm too lazy to refactor a part that's LL(2) to LL(1)) -> Type Checker -> SSA IR Lowering -> HIR Structurizer -> Rust Codegen. So the input is my custom language .rz, and the output is Rust, with HTTP-like verbs for manipulating the scene of the object, likePOST /hittable sphere;, or foo = GET /camera; For example, it has many, many challenges along the way.

The most annoying part was actually studying the SSA IR lowering, which I kinda forgot a bit because it's been a while, but I followed Braun et al algorithm, which works pretty well. Also, I get pretty annoyed at Rust here because of the borrow checker.

I'm past that. HIR structurizer also poses a big challenge as well, using a lot of graph traversal (BFS/DFS) to move SSA IR to a higher-level language like Rust. A technical note for HIR is that I performed DFS on conditional goto, and see if there's a cycle, if it is, then a loop, otherwise it's an if statement. Codegen has some footprints, a small preprocessing and assigns which variable is a declaration, mutable, or reassignment by looking at the loop.

Finally, I wrote some basic optimizations, such as constant folding, constant propagation, and dead code elimination. Although it does not have dead branch elimination, because I'm lazy. Another code optimization I was going to implement soon enough, if I'm still motivated, is removing unnecessary string allocations via string interner, and using Arena for constructing the AST, will see if I still wanna work on it. If the compiler is a bit buggy, I may or may not fix it. Most likely will fix it, but I enjoyed every bit of the process building this buggy, overkilled compiler.

I have not tested the actual ray tracer output because I'm lazy, will do that next week. And probably have an LLM to help me test it.

LLM Usage: In the pre-LLM era, this project would probably have taken up to years to finish, but with LLM, I was able to research things well. Most of the time I'm coding, the style I go for is pair programming with LLM, as I said, think and implement. And if there's something I'm not sure, research like, for example, the Braun et al, or help me write test cases so I stay in line, LLM is a great tool to use here.

LLMs helped generate test cases, which I reviewed and validated. I don't understand why LLM wants to write a test that passes even though the case should fail. I thought that's testing in general.

TLDR: Cool ray tracing in one weekend with a network protocol and an overkill compiler that no one will use, all in Rust except the Arduino part is written in C++.

Repo: https://github.com/sudo-JP/Razz

under razz-compiler/


r/Compilers • • Oct 09 '26

Can a borrow checker be auto-vectorized?

1 Upvotes

A borrow checker sounds like the last job for a CPU's vector unit. It follows references and asks whether a borrow outlives the value it points to. That looks like pointer chasing and branching, not the thing an autovectorizer is looking for.

But one step of it is plain arithmetic on small integers. Vx gives every block a region, its nesting depth, so a "smaller valued region lives longer".

A reference also records which way its lifetime may change when it is assigned (covariance, contra-variance etc).

Vx packs up to four of these lifetimes into one 64-bit word, 16 bits each. Checking whether one lifetime may stand in for another means comparing two words slot by slot.

Because this borrow checker loop is written without early exits, LLVM's auto-vectorizer does its job well.

More on that here: https://vxlang.org/blog/can-the-borrow-checker-be-auto-vectorized.html


r/Compilers • • Jan 26 '26

Can a self-taught dev build a production-grade language using Go?

1 Upvotes

Hi everyone,

I’ve been programming since I was 15. I don’t have a CS degree and I’m entirely self-taught. I also have ADHD, which means while formal education materials can be boring for me, I get into a state of extreme hyperfocus when I’m actually coding.

Since day one, my dream has been to build my own programming language. I don’t want to do this for a job; I want it to be a serious, long-term hobby project. However, I don’t want it to just remain a "toy language." I want to go deep and eventually create something production-ready that I can maintain and update for years.

I have a few specific questions:

  1. The "No Degree" Factor: Is it truly possible to build a production-grade compiler without a CS degree? I’m not talking about hacking something together in a weekend. If I assume a scenario where I work on this consistently for 5+ years, can I reach the technical depth required to build a real, usable compiler without the academic background?

  2. Using Go: I know and love Go. I know it lacks some syntax sugar often used in compiler writing (like advanced pattern matching found in Rust or functional languages). Is this a major roadblock, or is Go still a viable choice for a serious compiler project?

  3. Resources: Are there any books or resources on compiler design that explain deep technical concepts in "ELI5" (Explain Like I'm 5) terms?

Thanks in advance!


r/Compilers • • 9d ago

Compilers add or remove perfomace?

1 Upvotes

isnt it that compilers change the code, as in not only transferring it into machine language, but before that changing the structure of the code. so how would one attempt to make performant code if the compiler changes sed code that i tried to make performant?


r/Compilers • • Jan 19 '26

Building a Statically-Typed Functional Language Translator to C++20 with LLM Agents

Thumbnail github.com
5 Upvotes

Context

I spent the last year experimenting with LLM-assisted development (Claude, Codex, Cursor) to understand how far these tools can go on non-trivial projects. As an exercise in "deep" work with AI agents, I built a compiler for a statically-typed functional language that translates to C++20.

This is explicitly a prototype and learning exercise, not a production compiler. The goal was to see if LLM agents can handle the full compiler pipeline with proper TDD discipline. About 85% of the code was AI-generated.

Result: 714 files, 1524 tests passing, 4014 assertions green. The compiler works for its feature set, but has significant limitations I'll detail below.

GitHub: https://github.com/jenya239/mlc

The Language: Cherry-Picking Features That Map Well to C++20

The core idea was to select language features from established functional languages that have natural mappings to modern C++. This isn't a novel language design - it's a deliberate selection of features based on target compilation feasibility.

Sum Types: OCaml/Rust → std::variant

Source: OCaml's variants, Rust's enums

type Result<T, E> = Ok(T) | Err(E)
type Option<T> = Some(T) | None

Why this works in C++20:

template<typename T, typename E>
struct Ok { T value; };
template<typename E>
struct Err { E error; };

template<typename T, typename E>
using Result = std::variant<Ok<T, E>, Err<E>>;

C++17's std::variant is literally designed for tagged unions. The mapping is almost 1:1. Each variant becomes a struct, the sum type becomes std::variant<...>, and type safety is preserved at compile time with no runtime overhead beyond what variant already has.

What's hard: Rust's enum can have shared fields across variants. C++ variant can't do that - each alternative is independent. So I couldn't implement:

// Can't do this - all variants would need 'timestamp'
type Event = Click(i32, i32) | KeyPress(char) with timestamp: i64

Pattern Matching: OCaml/Haskell → std::visit

Source: OCaml's match ... with, Haskell's case ... of

fn unwrap(r: Result<i32, str>) -> i32 =
  match r
    | Ok(value) => value
    | Err(_) => 0

Why this works:

int unwrap(Result<int, string> r) {
  return std::visit(overloaded{
    [](const Ok<int, string>& ok) { return ok.value; },
    [](const Err<string>& err) { return 0; }
  }, r);
}

The lambda overload pattern is elegant. C++ compiler checks exhaustiveness (all variant alternatives covered). The semantic is identical to functional languages.

What's hard: Nested patterns like OCaml's | Some(Ok(x)) => become nested std::visit calls in C++, which is both verbose and slow. OCaml's guards (when clauses) don't map naturally to lambdas. Haskell's view patterns have no C++ equivalent. I implemented only basic patterns because anything more complex would require generating decision trees, which is substantially harder.

Product Types: OCaml records → structs

Source: OCaml/SML records

type Point = { x: f32, y: f32 }

Maps directly:

struct Point { float x; float y; };

This is trivial. Records are just structs. Even field access syntax is the same (point.x).

What's hard: Nothing, really. This is the easiest feature. The only limitation: no structural typing - C++ requires nominal types.

Parametric Polymorphism: Haskell/ML → C++ templates

Source: Haskell's type parameters, ML's 'a

type Option<T> = Some(T) | None

fn map<A, B>(opt: Option<A>, f: A -> B) -> Option<B> = ...

Why this works:

template<typename T>
using Option = std::variant<Some<T>, None>;

template<typename A, typename B>
Option<B> map(Option<A> opt, std::function<B(A)> f) { ... }

C++ templates are powerful enough for this. Type parameters translate directly. Instantiation happens at compile time, just like in ML.

What's hard: Higher-kinded types like Haskell's Functor and Monad don't work because C++ templates aren't first-class types. Type inference is limited because C++ can't infer template parameters from function bodies, forcing me to require explicit type signatures for all functions. Polymorphic recursion, where Haskell allows f :: [a] -> [a]; f (x:xs) = f [x], is impossible with C++ templates.

Type Inference: Hindley-Milner (limited)

Source: ML/Haskell inference

let x = 5        // Inferred as i32
let y = x + 1    // Inferred as i32

Why limited: Full Hindley-Milner requires analyzing the entire program. For C++ target, I'd need to generate C++ with explicit types everywhere. So I only infer let-bindings locally, require explicit function signatures.

This is a compromise for compilation target, not a language limitation. If I targeted LLVM, full inference would be easier.

Function Syntax: ML-style

Source: OCaml/SML

fn divide(a: i32, b: i32) -> Result<i32, str> =
  if b == 0 then Err("division by zero")
  else Ok(a / b)

Why this, not Haskell-style: Haskell uses guards and where-clauses heavily:

divide a b
  | b == 0    = Err "division by zero"
  | otherwise = Ok (a / b)

OCaml style translates more directly to C++ control flow:

Result<int, string> divide(int a, int b) {
  if (b == 0) 
    return Err{"division by zero"};
  else 
    return Ok{a / b};
}

Guards would require more complex control flow transformation.

What I Deliberately Excluded

Mutation and references like Rust's &mut were excluded because while C++ has pointers and references, their semantics differ fundamentally from Rust's borrow checker. The trade-off is that everything uses value semantics, which is expensive for large types but simple to implement correctly.

Type classes from Haskell and traits from Rust were excluded because they would require C++ concepts or template specialization, both complex to implement. Without them, there's no ad-hoc polymorphism - you can't write a generic print function that works for all types.

Effects and monads like Haskell's IO or Rust's async have no good C++ equivalent and would need a runtime system. The trade-off is that side effects are unrestricted with no compile-time effect tracking.

Mutable data structures like OCaml's ref were excluded because they make code generation harder - you need to track mutability through the type system. This means you can't write efficient in-place algorithms.

The module system from OCaml and Haskell is partially implemented but incomplete. C++ modules and namespaces don't map well to ML modules, which are more powerful (first-class, parameterized). The trade-off is that everything lives in a single namespace with no separate compilation.

The Selection Principle

Included if:

  1. Clean mapping to C++20 features exists

  2. Semantic is preserved without runtime system

  3. Type safety is maintained at C++ compile time

Excluded if:

  1. Requires complex runtime (GC, async runtime)

  2. No direct C++ equivalent (type classes, HKT)

  3. Would require sophisticated code transformation (optimization passes)

This is why MLC feels like "OCaml-lite targeting C++" rather than a full functional language. The features that made it in are those where the C++ compiler does the heavy lifting (variant checking, template instantiation), not ones requiring complex compilation strategies.

Example: Why Pattern Matching Works So Well

The key insight is that C++17 variant + C++20 concepts give you almost exactly what ML variants provide:

ML variants (OCaml) provide multiple alternatives, exhaustiveness checking at compile time, type safety preventing access to wrong alternatives, and no runtime overhead for tag checking. C++ provides exactly the same properties: std::variant has multiple alternatives, std::visit with overloaded lambdas enforces exhaustiveness, the variant holds type information ensuring safety, and tag checking has minimal overhead.

This is not a coincidence. The std::variant facility was explicitly designed for this use case. The same alignment doesn't exist for other features like type classes mapping to concepts, which is why I didn't implement them.

Compilation Strategy

Sum types → std::variant:

// MLC: type Result = Ok(i32) | Err
struct Ok { int field0; };
struct Err {};
using Result = std::variant<Ok, Err>;

Pattern matching → std::visit with overloaded lambdas:

// MLC: match res | Ok(v) => v | Err => 0
int unwrap(Result res) {
  return std::visit(overloaded{
    [](const Ok& ok) { return ok.field0; },
    [](const Err& err) { return 0; }
  }, res);
}

The overloaded helper is from the runtime library:

template<class... Ts> struct overloaded : Ts... { 
  using Ts::operator()...; 
};
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

Type parameters → C++ templates:

// MLC: type Option<T> = Some(T) | None
template<typename T>
struct Some { T field0; };
struct None {};
template<typename T>
using Option = std::variant<Some<T>, None>;

This approach ensures type safety and exhaustiveness checking at the C++ level.

Compiler Architecture

Classic multi-pass pipeline implemented in Ruby:

1. Lexer (lib/mlc/source/lexer)

Manual lexer that tokenizes source code. It handles keywords (fn, type, match, if, then, else, let), identifiers with ML-style naming conventions, literals (integers, floats, strings, booleans), operators (=>, |, ->, arithmetic, comparison), and structural tokens ((), {}, [], ,, ;). Nothing fancy - just straightforward character-by-character scanning with lookahead for multi-character operators.

2. Parser (lib/mlc/source/parser)

Recursive descent parser that builds the AST. Expression parsing uses precedence climbing for operators. Type expression parsing handles complex types including generics, sum types, and records. Pattern parsing deals with match expressions, including nested patterns and variable binding. Declaration parsing covers functions and type definitions.

AST structure (lib/mlc/common/ast):

Program
  ├── TypeDecl (name, type_params, variants/fields)
  ├── FunctionDecl (name, params, return_type, body)
  └── ...

Expr (expression nodes)
  ├── MatchExpr (scrutinee, branches)
  ├── IfExpr (condition, then_branch, else_branch)
  ├── LetExpr (bindings, body)
  ├── CallExpr, LiteralExpr, VarExpr, ...
  └── ...

3. Semantic Analysis (lib/mlc/representations/semantic_ir)

This is where it gets interesting. The semantic pass performs type checking with Hindley-Milner style inference for let-bindings, though function signatures must be explicit. Generic instantiation uses constraint solving, and pattern matching gets exhaustiveness checking.

Name resolution handles scope management including nested scopes and shadowing. Symbol tables track both types and values, and type parameters are bound correctly in generic contexts.

The output is a typed IR where every expression has a resolved type, all names are resolved to their declarations, and all patterns are validated for exhaustiveness.

Example transformation:

// Source
fn id(x: i32) -> i32 = x

// Semantic IR (conceptual)
FunctionDecl {
  name: "id",
  params: [(x, Type::I32)],
  return_type: Type::I32,
  body: VarExpr { name: "x", type: Type::I32 }
}

4. Code Generation (lib/mlc/backends/cpp)

Two-stage process:

Stage 1: SemanticIR → C++ AST

This uses a Ruby DSL I built for programmatic C++ code generation (lib/cpp_ast). The DSL can represent classes, structs, and inheritance hierarchies. It handles templates including variadic and template-template parameters. Functions can have all qualifiers (const, noexcept, constexpr). C++20 features like concepts, modules, and coroutines are supported. The DSL can build full expression trees.

Example DSL code:

struct_def("Ok") do |s|
  s.member("int", "field0")
end

using_decl("Result", 
  template_inst("std::variant", ["Ok", "Err"]))

Stage 2: C++ AST → Source Code

Pretty-printer that generates formatted C++20 code. It handles proper indentation, respects operator precedence, orders include directives correctly, and manages namespaces. The DSL has roundtrip capability - it can parse C++ back into DSL structures for analysis or transformation. This wasn't needed for the compiler itself but proved useful for testing.

5. Runtime Library (runtime/include/mlc/)

Modular C++20 headers provide the runtime support. The core module includes match.hpp for overloaded pattern and variant helpers, and types.hpp for type aliases and utilities. The text module has string.hpp for UTF-8 string handling. The io module provides file.hpp for file operations. The math module includes vector.hpp for vector math operations. All of this uses modern C++20 features - concepts for constraints, ranges for sequences, constexpr for compile-time evaluation, and noexcept specifications for optimization.

What Works

The language implementation covers the core functional programming features I targeted. Sum types work with full pattern matching and exhaustiveness checking. Product types (records with named fields) compile cleanly to C++ structs. Parametric polymorphism handles generic types and functions through C++ templates. Let-bindings have type inference, if-then-else expressions work as expected, and function calls including higher-order functions all function correctly. The basic standard library provides I/O, string handling, and math operations.

The compilation pipeline works end-to-end. The compiler generates valid C++20 code that system compilers (g++ or clang++) can compile to working binaries. The runtime library integrates cleanly with generated code without requiring special linking steps or runtime dependencies.

Testing coverage is comprehensive with 1524 test runs and 4014 assertions all passing. Unit tests cover each compiler phase independently. Integration tests compile and execute complete MLC programs to verify correctness. The C++ AST DSL has roundtrip tests ensuring that generated C++ can be parsed back into the DSL structure.

Example working program:

type Result = Ok(i32) | Err(str)

fn divide(a: i32, b: i32) -> Result =
  if b == 0 then Err("division by zero")
  else Ok(a / b)

fn main() -> i32 =
  match divide(10, 2)
    | Ok(value) => value
    | Err(_) => 0

Compiles to working binary that returns exit code 5.

What Doesn't Work (Limitations)

Being honest about what's missing. There are no optimizations. The compiler generates naive code where every operation becomes a separate statement. There's no constant folding, dead code elimination, or inlining. There's no tail call optimization despite the functional nature of the language. Pattern matching compiles to nested std::visit rather than decision trees.

Error messages are terrible. You get "Error at line 42: Type mismatch" with no context, no suggestions, no highlighting. Just line numbers.

There's no incremental compilation. The compiler recompiles everything on every change, with no module boundaries for separate compilation and no dependency tracking.

Type inference is limited to let-bindings. Function signatures must be explicit. There's no bidirectional type checking that would allow inference to flow both ways through function applications.

The module system is incomplete. Basic import and export syntax is parsed but not fully implemented. There are no separate compilation units - everything lives in one namespace.

The standard library is minimal. It covers basic I/O, strings, and math, but there are no collections beyond what you can implement yourself with the List type. No concurrency primitives, no effects system.

Pattern matching has limitations. There are no guards (when clauses), no @-patterns for binding and matching simultaneously, and no view patterns.

There are no mutable references. Everything uses value semantics, so you can't modify values in place. There's no borrowing or ownership system - the code relies on C++ semantics with smart pointers.

Memory management is entirely through C++ smart pointers (shared_ptr and unique_ptr). There's no control over allocation strategy and no arena allocation for performance.

These aren't bugs - they're scope limitations. The goal was proving LLMs can build a working compiler with proper architecture, not competing with production compilers.

Interesting Technical Decisions

Why C++20 as Target?

C++20 provides several features that map perfectly to functional language constructs. std::variant fits sum types exactly. The template system handles parametric polymorphism naturally. Lambda overloading makes pattern matching elegant. Concepts allow constraining generic code. And there's no runtime required - everything compiles to native code.

I considered LLVM IR as an alternative but rejected it. LLVM is more complex to get working correctly. Debugging generated LLVM bitcode is harder than debugging C++. And targeting LLVM creates expectations of optimization, which was out of scope for this project.

Why Ruby for Implementation?

Ruby enables rapid prototyping for compiler development. Dynamic typing proves useful when manipulating AST structures that have many variant types. Metaprogramming capabilities help with DSL construction, particularly for the C++ AST builder. String handling is excellent for code generation. And frankly, I know Ruby well from previous projects.

The downsides are real. Performance is poor compared to compiled languages, but this is a prototype where development speed mattered more. Type safety is missing, though comprehensive tests mitigate this risk.

Pattern Matching Translation

The naive std::visit approach has significant benefits. Correctness is guaranteed because the C++ compiler checks exhaustiveness - if you miss a variant case, compilation fails. The generated code is simple to understand and debug, which matters during development. Type safety is complete since the compiler catches type errors at compile time.

The downsides are real though. Performance suffers with nested patterns because each level requires a separate std::visit call. Code size increases because each pattern branch generates a separate lambda, and the compiler instantiates all these lambdas even if some are never used at runtime.

A real compiler would build decision trees. This generates:

// For: match x | A(B(_)) => 1 | A(C(_)) => 2 | D => 3
std::visit(overloaded{
  [](const A& a) {
    return std::visit(overloaded{
      [](const B& b) { return 1; },
      [](const C& c) { return 2; }
    }, a.field0);
  },
  [](const D& d) { return 3; }
}, x)

Correct but inefficient. Production compilers would analyze patterns and generate a decision tree with minimal checks.

Exhaustiveness Checking

Done at semantic analysis phase, not relying on C++ compiler:

def check_exhaustiveness(scrutinee_type, patterns)
  # Get all variants of the sum type
  required = get_variants(scrutinee_type)
  
  # Extract matched variants from patterns
  covered = patterns.map { |p| extract_variant(p) }
  
  # Check coverage
  missing = required - covered
  raise "Non-exhaustive patterns: missing #{missing}" unless missing.empty?
end

This catches errors before code generation:

// Error: non-exhaustive patterns, missing: Err
match result
  | Ok(x) => x

Type Inference

Using constraint-based approach:

  1. Generate constraints from expressions

  2. Solve constraints with unification

  3. Substitute type variables with concrete types

Example:

let x = 5;  // Constraint: x ~ i32
let y = x + 1;  // Constraint: y ~ i32 (from + : i32 -> i32 -> i32)

This is limited compared to full Hindley-Milner. There's no polymorphic recursion, no higher-rank types, and function signatures must be explicit. But it's sufficient for the language features I implemented.

LLM-Assisted Development Methodology

This is the core experiment: can LLMs build a compiler with proper guidance?

Approach:

  1. I designed the architecture (pipeline stages, IR structure, type system semantics)

  2. I wrote test specifications (input code, expected AST/IR/C++)

  3. AI implemented the code to make tests pass

  4. I reviewed and refactored when needed

Workflow example:

# I write this test:
def test_pattern_matching_sum_types
  code = <<~MLC
    type Result = Ok(i32) | Err
    fn unwrap(r: Result) -> i32 = 
      match r | Ok(v) => v | Err => 0
  MLC
  
  ast = parse(code)
  ir = analyze(ast)
  cpp = generate(ir)
  
  assert_compiles(cpp)
  assert_includes cpp, "std::variant<Ok, Err>"
  assert_includes cpp, "std::visit"
  
  binary = compile_and_link(cpp)
  assert_equal 42, execute(binary, "Ok(42)")
  assert_equal 0, execute(binary, "Err")
end

Then prompt AI:

Implement the code generation for pattern matching over sum types. The semantic IR contains a MatchExpr node with scrutinee (the expression being matched) and branches (list of Pattern, Expr pairs). Generate C++ code using std::visit with lambda overloads. Each pattern should destructure the variant and bind variables. See test test_pattern_matching_sum_types for expected behavior.

AI generates the implementation. Test passes → move to next feature. Test fails → refine prompt or fix manually.

What worked well was incremental development - implementing one feature at a time, each with comprehensive tests before moving forward. Clear specifications were critical: tests defined exact expected behavior, leaving no ambiguity for the AI. Architectural boundaries helped enormously because each compiler phase was isolated, meaning the AI didn't need full context of the entire system. Pattern recognition was a strength - the AI excelled at "transform AST pattern X to C++ pattern Y" tasks where the mapping was well-defined.

What didn't work was having the AI write its own tests. Generated tests were consistently shallow and missed edge cases that a human would catch. Vague specifications produced garbage - telling the AI to "implement pattern matching" without concrete examples and expected output was useless. Large refactorings across many files caused the AI to lose track of what had changed where. Novel algorithms were beyond reach - the AI generated standard textbook solutions but couldn't optimize or find clever approaches.

The statistics tell the story. Roughly 85% of the code is AI-generated - implementation details and boilerplate. The remaining 15% is human-written - architecture, complex logic, and test specifications. This took hundreds of AI interactions over approximately 8 months of part-time work, with countless failed attempts thrown away.

The key insight is that AI is a powerful implementation tool but needs strong architectural guidance and comprehensive tests. Without TDD discipline, AI-generated code degrades rapidly as changes accumulate.

Interesting Bugs and Solutions

Bug 1: Nested pattern matching generates invalid C++

Problem: Patterns like A(B(x)) generated:

std::visit([](const A& a) {
  std::visit([](const B& b) {
    return b.field0;  // 'b' out of scope
  });
}, ...)

Solution: Proper variable scoping in code generator. Each lambda needs to capture correctly:

std::visit([](const A& a) {
  return std::visit([](const B& b) {
    return b.field0;
  }, a.field0);
}, ...)

AI initially missed the return statements. Fixed by adding explicit test case showing the expected structure.

Bug 2: Generic instantiation infinite loop

Problem: Type List<T> = Cons(T, List<T>) | Nil caused infinite recursion during code generation.

Solution: Track types being generated, emit forward declarations:

template<typename T> struct Cons;
template<typename T> struct Nil;
template<typename T> using List = std::variant<Cons<T>, Nil<T>>;

template<typename T> struct Cons { 
  T field0; 
  List<T> field1; 
};

AI struggled with this. I had to implement the logic manually, then had AI generalize it to other recursive types.

Bug 3: Type inference fails with polymorphic functions

Problem:

fn id(x: a) -> a = x
let y = id(5)  // Should infer y : i32

Type checker couldn't instantiate a with i32.

Solution: Two-pass type checking:

  1. Collect function signatures with polymorphic types

  2. Instantiate when called with concrete types

AI generated the first pass correctly but missed instantiation. Added after seeing tests fail.

Bug 4: Pattern match order matters for C++ compilation

Problem: Generated code:

struct Ok { int field0; };
using Result = std::variant<Ok, Err>;  // Error: 'Err' undefined
struct Err {};

Solution: Topological sort of type definitions before code generation. All types must be declared before use in variant.

AI-generated topological sort worked first try after I specified the algorithm in the prompt.

Performance Characteristics

Not optimized, but measured out of curiosity:

Compilation speed (measured on laptop, Core i5):

  • Small program (50 lines): ~200ms (150ms Ruby, 50ms C++)

  • Medium program (500 lines): ~800ms (600ms Ruby, 200ms C++)

  • Large program (2000 lines): ~3s (2.5s Ruby, 500ms C++)

Generated code performance:

  • Simple arithmetic: comparable to hand-written C++

  • Pattern matching: 2-3x slower (nested std::visit overhead)

  • Generic code: similar to C++ templates after instantiation

Binary size:

  • Minimal program: ~40KB (mostly runtime library headers)

  • With pattern matching: ~80KB (template instantiations)

  • Full feature use: ~200KB (many instantiations)

None of this is optimized. A real compiler would do much better.

Testing Strategy

Comprehensive test suite was critical for LLM-assisted development:

Unit tests (1000+ tests):

  • Lexer: tokenization correctness

  • Parser: AST structure for each language construct

  • Semantic analysis: type checking, name resolution

  • Code generation: C++ AST structure

  • C++ DSL: roundtrip parsing

Integration tests (400+ tests):

  • Full pipeline: MLC → C++ → binary → execution

  • Each language feature tested end-to-end

  • Edge cases: empty programs, deeply nested expressions, large types

Regression tests (100+ tests):

  • Captured bugs that occurred during development

  • Ensures bugs don't reappear after refactoring

Property-based tests (small number):

  • Random expression generation

  • Ensures type safety preserved

Test organization:

test/
├── mlc/
│   ├── source/
│   │   ├── test_lexer.rb          # Lexer tests
│   │   └── test_parser.rb         # Parser tests
│   ├── representations/
│   │   └── test_semantic_ir.rb    # Type checking tests
│   └── backends/
│       └── test_cpp_backend.rb    # Code gen tests
├── cpp_ast/
│   ├── test_nodes.rb              # DSL structure tests
│   ├── test_generator.rb          # C++ generation tests
│   └── test_parser.rb             # C++ parsing tests
└── integration/
    ├── test_basic_compilation.rb   # End-to-end tests
    ├── test_pattern_matching.rb
    ├── test_generics.rb
    └── ...

Test-driven workflow was essential:

  1. Write failing test for new feature

  2. Prompt AI to implement feature

  3. Run tests until they pass

  4. Refactor if needed (tests prevent regressions)

  5. Commit

Without tests, AI would introduce regressions constantly. With tests, AI could confidently refactor because tests caught errors immediately.

Related Work and Comparisons

This isn't novel compiler research - it's an exercise in LLM-assisted development. Similar projects:

Other hobby compilers:

  • Most don't publish full source with AI-assistance disclosure

  • Many target VMs (easier than native code)

  • Few implement full parametric polymorphism

Professional compilers:

  • Rust, OCaml, Haskell (what I drew inspiration from)

  • Orders of magnitude more sophisticated

  • Years of development by expert teams

  • MLC is a toy compared to these

AI-assisted programming studies:

  • Published studies show ~40-50% code from AI tools

  • My experience: ~85% with careful prompting and TDD

  • Difference likely due to clear architecture and comprehensive tests

Functional → C++ compilers:

  • Some compile Haskell/ML to C (older projects)

  • Modern ones target LLVM

  • C++20 std::variant approach is relatively uncommon

Resources and Links

Code:

Try it:

git clone https://github.com/jenya239/mlc
cd mlc
bundle install
rake test  # Run all tests (should pass)
bin/mlc examples/hello.mlc  # Compile and run example
bin/mlc --emit-cpp program.mlc  # See generated C++

Other projects (similar methodology):

  • MC2: x86-64 machine code generator (Ruby → ELF)

  • OpenGL GUI Pipeline: GPU-accelerated UI (C++20)

  • Context Engine: RAG system for code search

Contact:

Conclusion

Can LLMs build a compiler? Yes, with caveats:

Required from human:

  • Compiler architecture knowledge

  • Ability to write precise test specifications

  • Willingness to throw away AI output when it's wrong

  • Discipline to maintain TDD workflow

What AI provided:

  • Fast implementation of standard algorithms

  • Boilerplate reduction (especially for AST structures)

  • Pattern-based code generation

  • Refactoring with test safety net

What surprised me:

  • AI handled lexer/parser better than expected

  • AI struggled with novel solutions (optimizations, error recovery)

  • Tests were absolutely critical - without them, quality collapsed

  • Prompting quality mattered more than tool choice

MLC proves that AI can handle complex, interconnected systems like compilers. But it's not autonomous - it needs strong guidance, clear specifications, and comprehensive tests. The human provides expertise and judgment; the AI provides implementation speed.

This was a valuable learning exercise in both compiler development and effective AI collaboration. Would I use this approach for a real compiler project? Probably, but with realistic expectations about where AI helps and where human expertise is irreplaceable.

Open to questions, criticism, and discussions about compiler implementation or LLM-assisted development. If you try the code and find bugs (there are definitely bugs), please let me know.


Transparency note: This post and the compiler README explicitly state the AI-assistance methodology. I believe transparency is important for understanding what current AI tools can and cannot do in compiler development. The project is MIT licensed - use it, learn from it, or critique it as you see fit.


r/Compilers • • Jan 26 '26

Making Memory Safety Easier in C by Borrowing Rust's Borrow Checker

Thumbnail
youtu.be
0 Upvotes

Don't judge by the clickbaity title here, this is just a vid on the tool built to make a smaller version of a Borrow Checker but instead built for C.


r/Compilers • • Jul 05 '26

Searching for a Python 'complier' if it exist

2 Upvotes

Hello,

I'm searching for a compiler for my Python game so It can run faster and can be package in a standalone file (without need of Python installed on the host)


r/Compilers • • Nov 04 '24

Easier Parser Generator for Building Your Programming Languages

0 Upvotes

The Algodal™ Parser Generator Tool can generate a parser in the C programming language.  The code of the parser generated is C99 and can be used in any C, C++ projects as well as with any language that can be bind to C, such as Python and Java.  

This parser generator is simple and fast.  It will speed up your workflow when you need to write a custom parser quickly.  The parser code is compatible on both Linux and Windows.  The generated parser can also read unicode (UTF-8) strings.

Algodal™ Parser Generator is like no other parser generator - it is literally the greatest parser generator under the sun.  You can write a complete JSON parser in literally 35 lines of code in just 30 minutes!  Want your JSON parser to now support comments ? One change and 5 minutes later, it does!

Check it out here: https://algodal.itch.io/algodal-parser-generator-tool


r/Compilers • • May 23 '22

LISP Interpreter in Rust

Thumbnail amazon.com
0 Upvotes

r/Compilers • • Jun 03 '25

What is the base salary of a LLVM complier engineer?

3 Upvotes

r/Compilers • • Jul 27 '22

A bit confuse

2 Upvotes

I would like to build a compiler but I feel confused. Writing a compiler is general for every cpu architecture or assembly language?

For example, I want to build a compiler for Arm, will it work for x64? Do I need to know every assembly language?

Thank you


r/Compilers • • Aug 01 '25

Convo-Lang

Post image
0 Upvotes

I create a new scripting language call Convo-Lang. It's a cross between a LLM prompt templating system and a procedural programming language. It's extremely useful for building AI agents and other agentic applications.

I wrote the parser and runtime in TypeScript and now I'm considering other options. One of the main requirements for the language is ease of integration into web-apps. The language is not intended for heavy compute and acts more of a router between an LLMs and users.

Does anybody have any suggestions?

You can checkout a live demo here - https://learn.convo-lang.ai


r/Compilers • • 4d ago

Audit of semi-colons

1 Upvotes

Is there a set of defined and specific reasons why semi colons are necessary in some languages? I am not against semi colons but from the standpoint of if one would want to create a language why would they use or mandate semi colons.


r/Compilers • • Jul 23 '25

Faster Hash Tables

Thumbnail
medium.com
1 Upvotes

In Jan 2025, Andrew Krapivin published a research that shattered a 40 yr old conjuncture about hash tables. This resulted into discovering fundamentally faster hash tables. Read more about it in my blog!