Skip to content

Repository files navigation

cx_logo

The Cx Programming Language

Cx is a pragmatic systems programming language designed to write C code with modern ergonomics, zero-cost abstractions, and complete transparency.

Instead of compiling to opaque machine code or complex IR, Cx transpiles to clean, highly readable C99. You get the safety and expressiveness of modern languages, while retaining the universal portability, tooling, and performance of the C ecosystem.

Core Philosophy

  1. Zero-Cost Abstractions: Generics are resolved via monomorphization. No void*, no runtime type erasure, no hidden allocations.
  2. Explicit over Implicit:== compares pointers (pure C semantics). === compares string content. You are always in control.
  3. Transparent Output: The generated C code is meant to be read, debugged, and manually inspected if necessary.
  4. Seamless C Interop: Use any C library natively. Cross-compile to any platform supported by a C compiler (e.g., CC=x86_64-w64-mingw32-gcc cx main.cx).

Key Features

  • Generics & Monomorphization: Write containers and algorithms once, instantiate for any type with zero runtime overhead.
  • Error Unions (T!E): Typed success/error returns that force explicit handling, eliminating silent failures.
  • Struct Methods & Overloading: Group behavior with data. The compiler automatically resolves . vs -> and handles method overloading.
  • defer Statements: Guaranteed, LIFO-ordered cleanup at every exit point of a function, preventing resource leaks.
  • Native Modules: A simple import system. No manual .h/.c splits, no include guards, no duplicated prototypes.
  • Compile-time Reflection: Inspect types at compile-time with built-ins like __is(T, U) and __type(T).
  • Low-Level Control: Full support for volatile, restrict, _Atomic, and inline assembly (__raw).

A Quick Look

See how Cx eliminates boilerplate while generating standard, optimized C99:

include<stdlib.h>// import std.lib; // from Cx Stdlibinclude<stdio.h>// import std.io; // from Cx StdlibenumMathError { DivByZero }
structCalculator {
// Error Union: forces the caller to handle the errorint!MathErrordivide(inta, intb) {
ifb==0returnMathError.DivByZero;
returna / b;
}
}
intmain() {
Calculatorcalc;
deferprintf("Cleanup done.\n"); // Guaranteed to run at the endint!MathErrorresult=calc.divide(10, 2);
if !result.valid {
printf("Error: %d\n", result.error);
return1;
}
printf("Result: %d\n", result.ok);
return0;
}

Tooling & Ecosystem

  • Fast Compilation: Powered by the D programming language frontend, compiling to C in milliseconds.
  • Built-in Updater: Keep the compiler up to date with a simple cx update.
  • Rich Standard Library: Includes std.array, std.stack, std.io, and more, all built with Cx generics.

Cx vs C: The Boilerplate Reduction

FeatureTraditional CCx
Generic ContainerManual copy-paste or unsafe void*struct Box<T> (Monomorphized)
Error HandlingManual struct with valid flag + unionsint!Error (Compiler-generated)
Resource CleanupRepeated free() or goto cleanup blocksdefer free(ptr); (LIFO injection)
String Equalitystrcmp(a, b) == 0a === b
Module Sharing.h + .c + Include guards + PrototypesSingle .cx file + import module

Status

Cx is in active, early-stage development. The compiler is stable for Linux environments and successfully passes a comprehensive test suite (70+ examples including VMs, HashMaps, and LLVM wrappers).

Next Milestone: Full self-hosting (rewriting the Cx compiler in Cx).


For more detailed examples, explore the examples/ directory or read the Installation Guide.

About

The Cx Programming Language

Resources

Stars

15 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages