1: Leaf
Contents
A high-level, general purpose programming language with a strong static type system.
Leaf is a complete language with fixed semantics. The type system, scoping rules, mutability model, and all language constructs are defined by the specification — there is no target-dependent variation. The compiler front-end fully defines what Leaf is; back-end compilation is a separate concern not covered by this specification.
Source files use the .leaf extension. See lang/definitions.md §5 for details.
1.1: Pipeline
Code -> Lex -> Parse -> AST -> Scopes -> Type check
(.leaf)
The compiler front-end is responsible for:
- Lexing and parsing
.leafsource files - Building the AST
- Resolving scopes and imports
- Type checking against the language's built-in type system
The front-end defines:
- One source file type:
.leaf— see definitions.md §5.1 - Built-in primitive types:
bool,byte,int,uint,float,str— see §1.3 - Built-in type-system concepts:
never— see types.md §3.2 - Built-in collection types:
List[T],Map[K, V],Set[T],Buffer,Tuple[T1, T2, ...]— see structs/data-types.md §4.8 - Built-in algebraic types:
Option[T],Result[T, E]— see stdlib/types.md - A prelude of built-in interfaces, functions, and collection APIs — see stdlib/interfaces.md
- Compound types: structs, enums (with associated data), type aliases — see structs/core.md §4
- First-class function types (
fn(T) -> U) — see semantics/functions.md §2.5 - Generator functions (
Generator[Y, R, N]) — see types.md §3.3 - Interfaces with explicit conformance declarations — see structs/core.md §4.5
- Generics with type parameter constraints — see structs/core.md §4.4
- Block scoping — see semantics/core.md §2.1
- Immutable-by-default bindings — see semantics/core.md §2.2
- Private-by-default visibility — see semantics/core.md §2.3
1.2: Assumptions
- Target environment has automatic memory management (GC or reference counting).
- Back-end compilation strategy is not specified — the language definition is front-end focused. A back-end may target bytecode, a scripting language, or any other runtime.
1.3: Built-in Types
Leaf has a fixed set of built-in types that are always available:
Primitive types:
| Type | Description |
|---|---|
bool |
Boolean values (true, false) |
byte |
Unsigned 8-bit integer (0 to 255) |
int |
Signed 64-bit integer |
uint |
Unsigned 64-bit integer |
float |
IEEE 754 double-precision (64-bit) floating-point |
str |
String values |
Type-system types:
| Type | Description |
|---|---|
never |
Bottom type — no value inhabits it; assignable to every type |
Built-in generic types:
| Type | Description |
|---|---|
Option[T] |
Optional value — Some(T) or None |
Result[T, E] |
Success or failure — Ok(T) or Err(E) |
List[T] |
Ordered, growable sequence |
Map[K, V] |
Key-value collection |
Set[T] |
Unordered collection of unique values |
Buffer |
Mutable, growable byte sequence for binary data |
Tuple[T1, T2, ...] |
Fixed-size heterogeneous collection (2–10 elements) |
Generator[Y, R=(), N=never] |
Generator / coroutine type |
Built-in enums:
| Type | Description |
|---|---|
Ordering |
Comparison result — Less, Equal, or Greater |
GeneratorResult[Y, R] |
Generator step — Yielded(Y) or Done(R) |
The ? suffix is sugar for Option: int? means Option[int].