The x2c Book
x2c extends C with dynamic values, collections, and compile-time code generation. Use the features you need; keep ordinary C where it already works. C headers, libraries, data layout, expressions, and native tools remain available. x2c compiles to C, and its compiler and runtime are written in x2c.
This book explains how to use x2c and specifies its language and library. Install a native compiler explains setup.
What x2c adds
x2c adds these features to C:
Varholds values and pointers with runtime type information, alongside ordinary static types.Stringprovides immutable text with interpolation. ImmutableListvalues share structure and support mapping, filtering, and folding; mutableArrayandMapcontainers grow automatically and storeVars.Symbolprovides compact named values without declaring an enum;SymbolSetgives those names an explicit order and indexing.Itercomposes traversal operations, andforeachvisits collections andFilecontents.protocoldefines interfaces that concrete types explicitly adopt. Namespaced methods, inherited typedef methods, delegation, and static or dynamic dispatch let types share behavior while retaining C layouts.Funcholds callable functions and capturing lambdas. Expression-bodied functions, dot methods, and call chains are also available.ScopeandContextmanage allocation lifetimes and isolated runtime state without garbage collection. Pools give immutable values nested lifetimes.matchselects the first matching case for structuredListdata and exposes named captures as local variables.Threadruns work on native threads and returns results throughjoin;Mutexcoordinates access to shared mutable data.Errorcarries structured failures.try,catch, andraisehandle them;finallyanddeferrun cleanup when control leaves a block.macroexpands to parsed source at compile time, and names a template declares stay private to each expansion.Typeholes specialize code for concrete types, including typed collections.Decoratormacros transform the expression or source item that follows. The optional autodiff package uses them to generate dual-number types from aTypehole and forward- and reverse-mode derivative functions with decorators.importbrings packages into a local namespace, including wrapped C libraries and their x2c and Lisp bindings.Lispevaluates code inside a running program; compile-time Lisp uses the same interpreter to compute values and generate syntax during translation.
A program can use one feature and keep the rest in C.
Reading the book
If you know C, most source will look familiar. The guide covers the features that replace hand-written tagged unions, adapters, repeated declarations, collection allocation, traversal, cleanup, and pattern matching.
- The language guide shows how to use x2c and when its features help.
- The language reference states exact syntax and semantics.
- The standard library describes the available modules and links to their API reference, generated from the source.
- Compiler architecture explains the self-hosting implementation for readers who want to study or contribute to it.
The book describes implemented behavior. The optional make doc-examples
translates its x2c samples and compiles them to objects. make doc-outputs
also links and runs samples with expected output. The optional make examples
runs the showcase programs and checks their output.