Zed is a systems programming language that compiles directly to x86-64 assembly. It focuses on simplicity, low-level control, and minimal runtime overhead while providing a comfortable syntax for systems programming.
- Direct compilation to x86-64 assembly
- Zero runtime overhead
- First-class inline assembly support
- C-like syntax with modern conveniences
- Minimal but powerful standard library
- Integrated build system
- VSCode integration with syntax highlighting
- Built-in documentation generator
- Code formatting tool
// Single-line comments
/* Multi-line
comments */
// Include standard library
@include <std/io.zed>;
// Include local file
@include "mylib.zed";
// Function declaration
fn add(a, b) {
return a + b;
}
// Variables
x = 42;
str = "Hello, Zed!";
The standard library is organized into modules.
puts(str)
: Write raw string to stdoutputchar(c)
: Write single characterprint_number(n)
: Print numeric value (base 10)print_number_base(n, base)
: Print number in specified base (2-16)print_binary(n)
: Print number in base 2print_hex(n)
: Print number in base 16println(x)
: Print string with newlinevprintln(x)
: Print numeric value with newlinegetchar()
: Read single character from stdinreadline(buffer, max_len)
: Read line from stdin into buffer
abs(x)
: Absolute valuemin(a, b)
: Minimum of two numbersmax(a, b)
: Maximum of two numberspow(x, n)
: Calculate x raised to power nsqrt(x)
: Calculate square root using Newton's methodgcd(a, b)
: Greatest common divisorlcm(a, b)
: Least common multiplefactorial(n)
: Calculate factorial
strlen(str)
: Get string lengthstrcpy(dest, src)
: Copy string with null terminationstrncpy(dest, src, n)
: Copy at most n charactersstrcat(dest, src)
: Concatenate stringsstrcmp(s1, s2)
: Compare stringsstrchr(str, c)
: Find first occurrence of characterstrrchr(str, c)
: Find last occurrence of characterstrstr(haystack, needle)
: Find substring, returns position or 0 - 1strupper(str)
: Convert string to uppercase in-placestrlower(str)
: Convert string to lowercase in-place
exit(code)
: Exit program with status codesleep(seconds)
: Sleep for specified secondsgetpid()
: Get process IDgetppid()
: Get parent process IDgetuid()
: Get user IDgetgid()
: Get group IDtime()
: Get system timechdir(path)
: Change working directorygetcwd(buf, size)
: Get current working directorymkdir(path, mode)
: Create directoryrmdir(path)
: Remove directoryopen(path, flags, mode)
: Open fileclose(fd)
: Close file descriptorread(fd, buf, count)
: Read from filewrite(fd, buf, count)
: Write to filelseek(fd, offset, whence)
: Seek in filefork()
: Create new processexecve(path, argv, envp)
: Execute programwait(status)
: Wait for child process
memcpy(dest, src, n)
: Copy n bytes of memorymemset(ptr, value, n)
: Set n bytes to valuememmove(dest, src, n)
: Move memory handling overlapsmemcmp(s1, s2, n)
: Compare memory regionsmemchr(s, c, n)
: Find byte in memory blockmalloc(size)
: Allocate memoryaligned_alloc(alignment, size)
: Allocate aligned memoryrealloc(ptr, old_size, new_size)
: Resize allocationfree(ptr, size)
: Free allocated memorysecure_zero(ptr, n)
: Securely zero memory block
// If statement
if (condition) {
// code
} else {
// code
}
// While loop
while (condition) {
// code
}
// Function declaration with implementation
fn add(a, b) {
return a + b;
}
// Function predeclaration
fn complex_function();
// Later implementation
fn complex_function() {
// Implementation
}
Note that numeric literals in Zed cannot be directly negative - use 0 - number
syntax (e.g., 0 - 1
for -1).
Zed provides comprehensive inline assembly support with full constraint specifications:
fn example() {
asm "movq %rdi, %rax # Move input to rax
addq $1, %rax # Add 1
ret" # Return value in rax
: "=r"[result] # Output constraints
: "r"[input] # Input constraints
: "rax"; # Clobber list
}
Supported constraint types:
r
: Register constraint=r
: Output register constraint- Memory clobbers:
"memory"
- Condition codes:
"cc"
// Basic array operations
buffer[0] = 65; // Store byte
value = buffer[0]; // Load byte
// Dynamic memory allocation
ptr = malloc(1024); // Allocate 1024 bytes
memset(ptr, 0, 1024); // Zero memory
free(ptr, 1024); // Free memory
The Zed build system (zed
) provides the following commands:
# Create new project
zed new project-name
# Build project
zed build
zed build --release # With optimizations
# Run project
zed run
zed run --release # Run optimized build
# Clean build artifacts
zed clean
# Install/update standard library
zed install-std
The zed-docgen
tool generates beautiful HTML documentation from Zed source files:
# Generate docs for a single file
zed-docgen input.zed -o docs/
# Generate docs for an entire project
zed-docgen src/ -o docs/ --title "My Project"
# Include private functions
zed-docgen src/ -o docs/ --private
Features:
- Markdown support in documentation comments
- Syntax highlighted code blocks
- Search functionality
- Public/private function visibility
- Function grouping and navigation
- Responsive design
- Print-friendly styling
The zed-fmt
tool formats Zed code according to consistent style rules:
# Format a single file
zed-fmt file.zed
# Format and write changes
zed-fmt --write file.zed
# Check formatting only (useful for CI)
zed-fmt --check src/
# Format with custom settings
zed-fmt --indent 2 --max-width 80 src/
Formatting rules:
- Consistent indentation
- Operator spacing
- Line length limits
- Comment preservation
- Special handling for inline assembly
- Empty line management
Zed provides a robust package management system through zed-pkg
, allowing easy installation, publishing, and management of packages.
The Zed package registry hosts community-created packages, enabling simple sharing and reuse of code.
# Install a package
zed-pkg install package_name
# Install a specific version
zed-pkg install package_name --version 1.0.0
Packages are automatically installed into src/pkg/package_name.zed
.
# List installed packages
zed-pkg list
# Remove a package
zed-pkg remove package_name
To publish a package:
- Ensure your project has a
zed.json
with package metadata - Run
zed-pkg publish
- Packages are stored in
src/pkg/
- Each package has a
.zed
file for code - Metadata is stored in a companion
.json
file
- Use semantic versioning
- Include clear documentation
- Keep packages focused and modular
A typical Zed project has the following structure:
project/
├── src/
│ └── main.zed # Entry point
├── examples/ # Example code
├── docs/ # Generated documentation
├── target/ # Build outputs
│ ├── debug/
│ └── release/
├── zed.json # Project configuration
└── .gitignore
The zed.json
file contains project metadata:
{
"name": "project-name",
"version": "0.1.0",
"target": "main"
}
Zed provides fine-grained control over memory alignment through the @align
directive. This is crucial for:
- SIMD operations requiring aligned memory access
- Cache-line optimization
- Hardware requirements
- DMA and device interactions
// Align variable allocation
@align(16)
buffer = malloc(1024);
// Align function stack frame
@align(16)
fn process_vectors(data) {
// Function body...
}
- Alignment must be a positive power of 2 (2, 4, 8, 16, 32, etc.)
- Multiple align directives are not allowed on the same declaration
- Alignment applies to the immediately following declaration or definition
- SIMD Operations
@align(16)
fn vector_add(a, b) {
asm "vmovdqu %rdi, %xmm0
vmovdqu %rsi, %xmm1
vpaddd %xmm0, %xmm1, %xmm0";
}
- Cache Line Optimization
@align(64) // Common cache line size
buffer = malloc(256);
- Hardware Requirements
@align(4096) // Page size alignment
page = malloc(4096);
The assembler ensures proper alignment by padding as needed. The stack is automatically realigned when entering aligned functions to maintain the specified alignment requirements.
- Rust toolchain (2021 edition or later)
- GNU Assembler (as)
- GNU Linker (ld)
git clone https://github.com/zed-coding/zed-lang.git
cd zed-lang
# Build everything
make
The standard library is automatically installed to ~/.zed-lang/std/version/1.0.0/
when creating a new project. Manual installation:
zed install-std
The Zed VS Code extension provides:
- Keywords and control flow
- Functions and variables
- Strings and numbers
- Comments (single-line and block)
- Inline assembly with register highlighting
- Include directives
- Bracket matching and auto-closing
- Comment toggling (Ctrl+/)
- Scope awareness
- Custom theme optimized for Zed
The extension includes:
- Language configuration for proper editing behavior
- Dark theme optimized for Zed syntax
- Full TextMate grammar for accurate highlighting
- Lexical analysis (lexer.rs)
- Parsing and AST generation (parser.rs)
- Code generation to x86-64 (codegen.rs)
- Assembly and linking via GNU tools
- Detailed error messages with source location
- Syntax and semantic error detection
- Color-coded error output
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests
- Submit a pull request
- Install Rust and required tools
- Clone repository
- Build compiler and tools
- Install VS Code extension (optional)
This project is licensed under the Apache License 2.0.
Voltaged (VoltagedDebunked)
Email: rusindanilo@gmail.com