Skip to content
All projects

Mica

A native programming language with Java-style objects. It compiles to C11, and its compiler is written in Mica.

Role
Sole author
Period
2026-09 → now
Status
active
Stack
Mica, C11, PowerShell, TypeScript
Compiler and runtimeMica plus C, excluding the generated bootstrap~5,000 lines
Self-hostingstages 2 and 3 emit identical C3 stages
Integer benchmark vs handwritten Cone trial-division program on one machine0.51× time

Overview#

Mica is a small programming language I'm building. It reads like Java with less boilerplate. Classes declare their fields in the constructor header, data types get equality and hashing generated for them, and functions take default and named arguments. There's also with for copying a value with changes, defer, and tests written in Mica itself.

The compiler emits C11, and any C compiler turns that into a native executable. Memory is handled by a tracing garbage collector, which also frees reference cycles.

The compiler is written in Mica. A fresh checkout starts from a committed C copy of the compiler, builds it, and then uses it to rebuild itself three times. The repository is private for now. wordfreq is a public example program.

Problem#

I wrote Mica because I wanted a language like Java and C without the boilerplate being awful to write. Java needs a VM to run and makes you write a lot of ceremony for small programs. C leaves memory management and most structure to you. The project goal in the repo puts it as "safe and productive by default, explicit and predictable when asked".

Writing the compiler in Mica was the real test. A compiler that only runs small examples doesn't tell you much about the language. A compiler is a large program with a lot of string handling, data structures and error paths. If Mica can compile its own compiler, it can handle real code, and any bug in the language shows up quickly because the compiler hits it.

Constraints#

  • There is no VM. The output is a native executable with a small runtime.
  • The first build cannot assume a Mica compiler is installed anywhere.
  • New compiler code has to compile with the previous version of the compiler, or the bootstrap breaks.
  • Windows x64 with MSVC is the only tested platform so far. Other C toolchains are experimental.

Architecture#

Source goes through a lexer, a parser, a checker and then a C emitter. core.mica holds the AST, the lexer and the core parser. check.mica handles types, scopes, mutability and initialisation. emit.mica writes the C declarations, statements, constructors and garbage collector roots. The shorter syntax lives in its own syntax/ layer, which parses it and lowers it into the core forms.

The runtime is written in C and covers numbers, strings, arrays and the collector. host.c provides the operating system and toolchain calls the compiler needs. The build embeds the runtime into the compiler binary, so the compiler can always emit a complete program.

Technical decisions#

C11 as the backend#

Every platform already has a good optimising C compiler, and generated C is easy to read when something breaks. The cost is that native builds need a C toolchain installed. Type checking and emit-c work without one.

A three-stage self-hosting check#

The build compiles the compiler with the bootstrap copy, then compiles it again with the result, then once more. If stages 2 and 3 don't produce identical C, the build fails. Only stage 3 gets packaged. Without this check it would be easy to ship a compiler that only works because an older version built it.

One generated file in the repo#

bootstrap/mica.c is C generated from the compiler. It's the only generated source file checked in, and it exists so a fresh checkout can build without an existing Mica binary. The very first compiler was written in Rust. It has been retired, and nothing in the build uses it now.

Trade-offs#

Compiling through C adds a C compiler run to every build, and some optimisation decisions are made by MSVC rather than by me. Self-hosting slows down work on the compiler, because new features have to be written using only what the previous compiler already supports.

The language is still experimental, and the name and syntax may change. The repo keeps design ideas in separate notes from the syntax the compiler accepts today.

Implementation details#

The self-hosting check in scripts/build.ps1 looks like this.

scripts/build.ps1 (excerpt)
& $bootstrapExe build $compilerSource -o $stage1
& $stage1 build $compilerSource -o $stage2
& $stage2 build $compilerSource -o $stage3
# ... hash the C each stage emitted ...
if ($hash2 -ne $hash3) { throw "self-hosting fixed point failed: generated C differs ($hash2 vs $hash3)" }

The garbage collector is precise. The emitter registers roots for local bindings, temporaries, call arguments and nested fields. The test suite runs the examples with the collector forced to run often, so a missing root fails in CI instead of turning into a rare crash later.

There's also a VS Code and Cursor extension. It shows diagnostics for unsaved files and has completion, hover, go to definition, an outline and commands to run, build and test. Its syntax grammar is tested against the example programs.

Outcomes#

The compiler builds itself and passes the three-stage check on every build. The compiler and runtime come to about 5,000 lines of Mica and C. The generated bootstrap file adds another 56,000 lines of C.

I ran one early benchmark, counting primes up to 2,000,000 by trial division. The Mica version took a median of 283.52 ms and a handwritten C version took 552.75 ms, on one machine with MSVC. I haven't looked at the assembly to find out why. The program doesn't use objects, strings, the collector or I/O, so I wouldn't read much into it beyond the generated C being reasonably fast.

The next things on the roadmap are enums with payloads and an exhaustive match, optional types, interfaces, and generic collections with a proper standard library. None of them exist yet.