A robust, type-safe systems programming language transpiled to C.
Features • Installation • Usage • Roadmap
Zag is an experimental statically-typed programming language written in Zig. It combines modern language features like optionals, error unions, and type inference with the portability of C.
The compiler works by transpiling Zag source code into C11, which is then compiled by your system's C compiler (e.g., gcc, clang, or cc). This ensures that Zag binaries are highly optimized and portable to any platform that supports C.
- 🚀 Zero Runtime Overhead: Transpiles to raw C code.
- 🛡️ Type Safety: Strong static typing with type inference.
- 🧩 Modern Constructs:
- Optionals (
?T): Null safety built-in. - Error Unions (
!T): Ergonomic error handling without exceptions. - Structs & Enums: Data organization made simple.
- Optionals (
- 🔌 C Interop: Native binding support via
bind fn. - 🌳 Tree-sitter Grammar: First-class support for syntax highlighting and tooling.
- Zig: To build the project yourself, you need Zig 0.15.x Zig compiler.
- C Compiler: A standard C compiler (
cc,gcc, orclang) must be available in your system path.
-
Clone the repository:
git clone https://github.com/dtasada/zag.git cd zag -
Build the project:
zig build install --prefix ~/.localThis will copy the zag compiler into
~/.local/bin/zag. If~/.local/bin/is in your$PATH, runningzagwill work. If not, add~/.local/binto your$PATH. You could also choose any other install prefix. -
Run the compiler:
zag build
Zag uses a syntax familiar to users of Zig, Rust, or Swift.
// Define a struct with methods
struct Vector2 {
x: i32,
y: i32,
// Static constructor method
fn new(x: i32, y: i32) Vector2 {
return Vector2 {
x: x,
y: y,
};
}
// Instance method
fn add(lhs: Vector2, rhs: Vector2) Vector2 {
return Vector2 {
x: lhs.x + rhs.x,
y: lhs.y + rhs.y,
};
}
}
fn main() i32 {
// Type inference in action
let vec = Vector2.new(1, 2);
let lhs = Vector2 {
x: 10,
y: 20
};
// Method call syntax
let result = lhs.add(vec);
return result.x;
}Zag makes it easy to bind to existing C libraries:
// Bind to the standard C printf function
bind fn printf(fmt: &c_char, args...) c_int;
fn main() i32 {
printf("Hello from %s!\n", "Zag");
return 0;
}The project is currently in the alpha stage. The following features are planned or in progress:
- Configurable Paths: Remove hardcoded compiler/input paths (CLI Argument parsing).
- String Literals: Support for escape sequences (
\n,\t,\"). - Multi-line Comments: Support for
/* ... */blocks. - Safety: Improved integer overflow checks and safe casting.
- Comptime: Expanded compile-time expression evaluation.
Distributed under the MIT License. See LICENSE for more information.