Repository navigation
Language Syntax
This document locks the MVP surface syntax.
Design goals:
- Unambiguous and tooling-friendly (no indentation parsing).
- Highly reliable for LLM generation (explicit delimiters).
- Error recovery-friendly (statement terminators + block delimiters).
- Contracts/refinements are first-class and always either verified (within scope) or rejected (no silent weakening).
Principles are in wiki/Syntax-Principles.md.
A Curlee source file is a sequence of items.
- Import declarations:
import ...; - Struct declarations:
struct ... { ... } - Enum declarations:
enum ... { ... } - Function declarations:
fn ... { ... } - Top-level
letdeclarations (optional in MVP)
Notes:
-
importdeclarations must appear before anyfndeclarations.
Top-level imports only:
import foo;
import foo.bar;
import foo.bar as baz;
Rules:
-
importmust appear at the top level. - The module path is a dotted identifier sequence.
- Optional aliasing is supported via
as <alias>. - A trailing
;is required.
Module resolution rules are specified in Modules and Imports.
Blocks use braces:
{
/* statements */
}
Statements must end with ; where applicable.
This is a deliberate choice to:
- improve parse recovery
- reduce ambiguity for LLMs
Examples:
let x: Int = 1;
print(x);
return;
return x;
x = expr; reassigns an existing binding. It is not a declaration — the
target must already be in scope as a local let binding (a new binding is
still let):
let i: Int = 0;
i = i + 1; // reassigns i
Rules:
- The target is a plain identifier (member/scoped targets are not in the MVP fragment).
- The target's type is unchanged; the RHS must have the same type.
- Parameters and
ghost letsnapshots are read-only and cannot be assigned. - Assignments are how loop-carried state is mutated, e.g. a bounded sum:
fn sum_to(n: Int) -> Int {
let total: Int = 0;
let i: Int = 0;
while (i < n)
[ invariant 0 <= i && i <= n && total >= 0;
decreases n - i; ]
{
i = i + 1;
total = total + i;
}
return total;
}
Verifier semantics: each assignment rebinds the name to a fresh solver
symbol (constrained to the RHS value), so loop decreases variants and
invariants are checked against the post-mutation state; see
Verification Scope §6.
fn name(param1: Type, param2: Type) -> ReturnType { ... }
Return type is recommended for MVP.
Contracts attach to a function header via a bracketed block.
fn divide(numerator: Int, denominator: Int) -> Int
[ requires denominator != 0;
ensures result * denominator == numerator; ]
{
return numerator / denominator;
}
Rules:
- Contract block is optional.
- Inside
[...], each clause ends with;. - Supported clauses:
requires <pred>;ensures <pred>;
-
resultis reserved insideensuresand refers to the returned value.
If a predicate uses constructs outside wiki/Verification-Scope.md, compilation fails with a clear diagnostic.
where refines types at bindings and parameters.
Variable refinement:
let x: Int where x > 0 = 5;
Parameter refinement:
fn takes_pos(x: Int where x > 0) -> Int {
return x;
}
Expressions include:
- literals: integers, strings, booleans
- identifiers
- member access:
value.field - unary:
!,-,~ - binary arithmetic:
+,-,*,/,% - bitwise:
&,|,^,<<,>> - comparisons:
==,!=,<,<=,>,>= - boolean connectives:
&&,|| - calls:
callee(args...)
Struct declarations and literals:
struct Point {
x: Int;
y: Int;
}
let p: Point = Point{ x: 1, y: 2 };
Field access uses .:
let sum: Int = p.x + p.y;
Rules:
- Struct field declarations end with
;. - Struct literals require named field initializers (
name: expr). - Duplicate field names are rejected with span-precise diagnostics.
- Unknown fields are rejected with span-precise diagnostics.
Calls are intentionally restricted in MVP:
name(args...)- module-qualified:
alias.name(args...)orfoo.bar.name(args...)
Note: verification predicates are parsed from expression syntax but are restricted by wiki/Verification-Scope.md.
Enums support constructor calls via scoped variant names:
enum Maybe {
Some(Int);
None;
}
let x: Maybe = Maybe::Some(1);
let y: Maybe = Maybe::None;
MVP deconstruction uses match with variant-only patterns:
match (x) {
Maybe::Some(n) => {
return n;
}
Maybe::None => {
return 0;
}
}
Pattern rules:
- Arm pattern must be
Enum::VariantorEnum::Variant(binding). - Payload-bearing variants require a binding.
- Variants without payload must not bind a payload.
- Duplicate variant arms are rejected.
- Matches must be exhaustive over declared enum variants.
The match arm body is always a block.
program = { item } ;
item = import_decl | fn_decl | let_decl ";" ;
import_decl = "import" module_path [ "as" IDENT ] ";" ;
module_path = IDENT { "." IDENT } ;
fn_decl = "fn" IDENT "(" [ params ] ")" [ "->" type ]
[ contract_block ] block ;
contract_block = "[" { contract_clause ";" } "]" ;
contract_clause = "requires" pred | "ensures" pred ;
params = param { "," param } ;
param = IDENT ":" type [ "where" pred ] ;
let_decl = "let" IDENT ":" type [ "where" pred ] "=" expr ;
block = "{" { stmt } "}" ;
stmt = let_decl ";"
| expr ";"
| return_stmt ";"
| if_stmt
| while_stmt
| match_stmt
| block ;
return_stmt = "return" [ expr ] ;
match_stmt = "match" "(" expr ")" "{" { match_arm } "}" ;
match_arm = match_pattern "=>" block ;
match_pattern = IDENT "::" IDENT [ "(" IDENT ")" ] ;
pred = expr ;
type = core_type | cap_type | nominal_type | phys_type | extern_fn ;
core_type = "Int" | "Bool" | "String" | "Unit" | "U8" | "U16" | "U32" | "U64" ;
cap_type = "cap" capability_name ;
capability_name = IDENT { "." IDENT } ;
nominal_type = IDENT ;
phys_type = "Phys" "<" element_kind ">" ;
element_kind = "U8" | "U16" | "U32" | "U64" ;
extern_fn = "extern" "fn" IDENT "(" [ params ] ")" [ "->" type ] ";" ;Curlee supports a deterministic bounded vector baseline in MVP-run via Vec and stdlib wrappers.
Type form:
let v: Vec = vec.new_int(8);
MVP element support is currently integer-only (Vec<Int> semantics behind Vec).
stdlib v1 API (import std.vec as vec;):
fn new_int(max_len: Int) -> Vec
fn len_int(v: Vec) -> Int
fn push_int(v: Vec, value: Int) -> Unit
fn get_int(v: Vec, index: Int) -> Int
fn set_int(v: Vec, index: Int, value: Int) -> Unit
Deterministic runtime failures:
vec capacity exceededvec index out of bounds
Note: capability names must be written without whitespace around . (e.g. cap io.stdout).
The freestanding target (curlee build --target freestanding-c, see Running Programs)
extends the surface with physical-memory access, x86 port I/O, and extern functions. All
three are freestanding-only: the VM (curlee run) rejects them.
Phys<T> is an opaque physical-memory pointer type. Element kinds are limited to the
fixed-width unsigned integers U8 | U16 | U32 | U64.
fn main(pm: cap phys.mem) -> U32 {
unsafe {
let fb: Phys<U32> = phys<U32>(0xFD00_0000); // framebuffer base
fb.write(0xFF8800); // pixel write
let v: U32 = fb.read(); // register/framebuffer read
return v;
}
}
Rules (constant-address rule):
-
phys<U>(literal)requires a compile-time literal address (decimal or hex, e.g.0xFD00_0000). Non-constant addresses are rejected. -
read()/write(v)are allowed only insideunsafe { ... }blocks. - Every
read()/write()use requires acap phys.memcapability value in scope (typically amainparameter). -
Phys<T>cannot be stored (struct field / enum payload), returned, or held in aletoutsideunsafe; it is an opaque pointer used only forread()/write(). - Writes accept
Intliterals for unsigned element kinds (the compiler range-checks at the freestanding target).
The x86 port I/O instructions (in/out) live in a separate I/O address space from MMIO.
Six builtins expose them for freestanding drivers:
| Builtin | Width | Direction |
|---|---|---|
port_inb(port) -> U8 |
8-bit | read |
port_outb(port, value) |
8-bit | write |
port_inw(port) -> U16 |
16-bit | read |
port_outw(port, value) |
16-bit | write |
port_inl(port) -> U32 |
32-bit | read |
port_outl(port, value) |
32-bit | write |
fn main(pm: cap phys.mem) -> Unit {
unsafe {
port_outb(0x3FB, 0x03); // COM1 LCR: 8N1
let lsr: U8 = port_inb(0x3FD); // COM1 LSR (opaque read)
port_outb(0x3F8, lsr); // COM1 THR
}
return;
}
Rules (mirror the Phys<T> gate):
- The port is a general
Intexpression: a compile-time literal (decimal or hex, e.g.0x3F8), or alet-bound base plus a constant offset (e.g.io_base + 0x0E, the virtio_net.c legacy-virtio pattern, issue #276). Non-Intport expressions are rejected. - Port I/O is allowed only inside
unsafe { ... }blocks and requires acap phys.memcapability value in scope. - Verifier semantics are trusted/opaque, exactly like
Phys<T>reads: port reads are uninterpreted (a contract cannot see through them), and there is no address-range fault model, so no out-of-bounds obligation is introduced. -
port_out*values must match the builtin's unsigned width (Int literals are accepted, mirroringPhyswrites). - Codegen emits inline
in/outassembly (asm volatile). Constant ports use an 8-bit immediate where possible; runtime ports (alet-bound base, with or without an offset) use the DX-register form (in (%dx),%ax/out %ax,(%dx)).
Extern functions declare a foreign symbol (no body) that the freestanding runtime must provide:
extern fn curlee_putc(c: Int) -> Unit;
extern fn curlee_halt() -> Unit;
Rules:
-
extern fnmust appear at the top level and has no body. - The contract on an extern is assumed, not verified (see Verification Scope): the extern boundary is a documented exception — the caller trusts the declared signature.
- The VM rejects calls to extern functions (freestanding-only). Only
curlee build(and the linked kernel, which provides the symbols) can call them.
Unsigned element kinds (U8 | U16 | U32 | U64) are produced by opaque reads
(Phys<T>.read(), port_in*). Since issue #277 a U64 can also be constructed from an
Int or a literal; the other unsigned kinds widen implicitly to Int (issue #274).
Rules:
-
U8/U16/U32widen implicitly toIntin expressions (issue #274): mixed arithmetic/comparisons between an unsigned read and anIntare typed asInt. -
U64is constructed from anIntor a literal in binding positions —letinitializers,ghostsnapshots, and struct-literal fields — when the value is value-preserving: a compile-time-known literal must satisfy0 <= value < 2^32(all joeos physical addresses are 32-bit); an out-of-range or negative literal is a hard error, never a silent truncation. -
U64 == U64comparisons are legal;U64arithmetic and mixedU64/Intcomparisons remain out of scope.
let base: Int = 1048576; // 0x100000: a physical address
let addr: U64 = base; // Int -> U64 widening (issue #277)
let lit: U64 = 1048576; // U64 literal adaptation
// let bad: U64 = 4294967296; // error: 0 <= value < 2^32 required
Codegen stores a U64 as C uint64_t (see Running Programs).
Reserved keywords:
-
fn,let,if,else,while,return -
requires,ensures,where -
import,as
Planned/reserved for later (do not assign other meanings):
-
unsafe,cap -
phys(currently usable as thephys<U>(addr)constructor /Phys<T>type name)