This document provides a deep dive into the Zenith syntax, language semantics, and advanced compiler features.
Zenith supports file-level directives to control the behavior of the Quantum Shield analyzer.
The #![strict] directive converts semantic warnings into fatal compilation errors.
- Undefined Symbols: Any variable, function, or struct access not present in the symbol table will stop the build.
- Strict Typing: Type mismatches in assignments or function returns become fatal.
#![strict]
$x: int = 10;
$y = 20;
$res = $x + $y;
The Zenith transpiler automatically evaluates constant expressions during the code generation phase.
let $x: int = 10 + 20 * 2; // Transpiles directly to $x = 50;
let $s = "Hello " . "World"; // Transpiles to $s = "Hello World";
The pipe operator chains expressions by injecting the left-hand value as the first argument of the right-hand call.
$data |> process() |> format("json") |> println();
Short closure syntax provides concise lambda definitions with implicit scope capture.
$multiplier = ($n) => $n * 10;
$typed_closure = ($x: int): int => $x ** 2;
Functions can be automatically cached using the @memoize attribute. This wraps the function body in a closure and utilizes an internal static $memo_cache.
@memoize
expensive_calc($n) {
// Computed only once for each distinct $n
return $n * 3.1415;
}
SQL is a first-class citizen in Zenith. Instead of strings, use query blocks that are analyzed for security and syntax.
// Global or local DB connection
db->connect("mysql:host=localhost;dbname=prod");
$results = query {
SELECT u.id, u.email
FROM users u
WHERE u.status == 'active'
LIMIT 10
}; // Checked against schema at compile time
Standardize security by using the sanitization operator before output.
let $input = $_GET["html_content"];
println($input !> "html"); // Escapes XSS automatically
Zenith supports generic metadata on definitions. These are captured in the AST and can be used by the compiler or external tools.
@Table("products")
@Serializable
struct Product {
id: int,
price: float
}
Zenith abstracts PHP 8.4+ Fibers into high-level spawn blocks, ideal for parallel I/O tasks like fetching multiple APIs during a single web request.
$api1 = spawn { fetch("https://api.v1.com") };
$api2 = spawn { fetch("https://api.v2.com") };
// Wait and collect results in parallel
$results = [$api1->resume(), $api2->resume()];
Zenith uses a nominal type system with support for composition and inheritance.
struct Entity {
id: int,
created_at: string
}
struct User : Entity {
username: string
}
Access native PHP functions or classes using the double-backslash \\ prefix.
$pdo = \\PDO { "sqlite::memory:" };
$time = \\time();
Zenith turns simple structs into powerful database models using the @Table attribute. This provides a zero-overhead, type-safe ORM experience.
@Table("users")
struct User {
id: int,
name: string,
email: string,
active: bool
}
// Static retrieval
$user = User::find(1);
// Static where-clause (transpiles to optimized SQL)
$active_users = User::where("active", true)->get();
// Method-based persistence
$user->name = "New Name";
$user->save();
Zenith handles the mapping between your struct fields and database columns automatically at compile-time when using query blocks or ORM methods.