Language Reference
Lex is one typed language served by one parser and one bytecode evaluator in two closed evaluation modes. Pure expressions produce the exact same value in both modes, and every refusal reports a line, column, and failure kind.
Evaluation modes
| Property | Console mode | Declaration mode |
|---|---|---|
| Strategy | Forces each line whole, left to right, including every let binding | Lazy thunk graph; forces only the requested field path and its dependencies |
| Effects | Permitted through host-provided commands | Refused at the border with Effect_Refused before arguments run |
| Input form | Single line up to 4096 bytes; supports top-level name = expr | Multi-line .lex file up to 16 MiB; supports trailing commas; refuses top-level assignment |
| Cross-file loading | Host-defined | import("path.lex") and declaration(blake3:...) read from the caller-supplied loader |
Complete grammar
The parser reads |> and _ directly into compact 16-byte syntax nodes without text rewriting.
line = name "=" pipeline | pipeline
pipeline = logical_or { "|>" stage }
stage = name | name "(" [ arguments ] ")"
logical_or = logical_and { "||" logical_and }
logical_and = comparison { "&&" comparison }
comparison = coalesce [ ("=" | "!=" | "<" | "<=" | ">" | ">=") coalesce ]
coalesce = with_expr [ "??" coalesce ]
with_expr = sum { "with" sum }
sum = term { ("+" | "-" | "++") term }
term = set { ("*" | "/" | "%") set }
set = factor { "|" factor }
factor = ("-" | "!") factor
| "if" pipeline "then" pipeline "else" pipeline
| "let" binding { "," binding } "in" pipeline
| "match" pipeline "{" match_arm { "," match_arm } [ "," ] "}"
| postfix
postfix = primary { "." field_name }
primary = integer [ unit_name ]
| "true" | "false"
| text
| hash
| "." name
| name "(" [ arguments ] ")"
| name record_lit
| name
| "(" [ param { "," param } ] ")" "=>" pipeline
| "(" pipeline ")"
| record_comp
| record_lit
| "[" [ pipeline { "," pipeline } [ "," ] ] "]"
param = name [ ":" kind_annot ] [ "=" pipeline ] | record_pat
kind_annot = kind_name | "Integer" "[" unit_name "]"
binding = (name | record_pat) "=" pipeline
record_pat = [ name "@" ] "{" [ pat_field { "," pat_field } [ "," ] ] [ "..." [ "," ] ] "}"
pat_field = name [ ":" kind_annot ] [ "=" pipeline ]
record_lit = "{" [ field_init { "," field_init } [ "," ] ] "}"
field_init = field_path "=" pipeline | name
field_path = name { "." name }
record_comp = "{" "for" (name | record_pat) "in" pipeline [ "if" pipeline ] ":" coalesce "=" pipeline "}"
match_arm = match_pat [ "if" pipeline ] "=>" pipeline
match_pat = "_" | name | literal | "." name | match_rec_pat
match_rec_pat = [ name "@" ] "{" [ name [ "=" match_pat ] { "," name [ "=" match_pat ] } [ "," ] ] [ "..." [ "," ] ] "}"
arguments = argument { "," argument }
argument = [ name "=" ] (pipeline | "_")
hash = "blake3:" 64 * lower_hex_digit
comment = "//" { byte except line end }
Names, reserved words, and comments
- Names start with an ASCII letter or
_, followed by ASCII letters, digits, or_. - Reserved words (10):
_,let,in,with,if,then,else,true,false, andmatch. Longer identifiers containing_remain valid. - Comments:
//starts a line comment extending to the end of the line. Lex has no block comment.
Values and kinds
| Kind | Literal form | Rules and bounds |
|---|---|---|
Integer | 0, 42, -7, 64 MiB | Signed 64-bit integer with overflow checks on +, -, *, and /. No operation wraps. |
Boolean | true, false | Required by if, &&, ||, !, and predicates. Integers never coerce to booleans. |
Text | "hello ${name}" | Valid UTF-8 with \\, \", \$, \u{XXXXXX} escapes and ${expr} interpolation. |
Hash | blake3:<64 lower hex> | Exact 32-byte BLAKE3 digest. Uppercase hex or wrong length refuses with Hash_Not_Valid. |
Member | .french, .Read | Closed enum member literal. |
Flag_Set | .Read | .Write | Unordered set of unique enum members. Duplicate members refuse with Set_Member_Twice. |
Record | { x = 0, width = 10 } | Lazy key-value record with canonical sorted field shape. |
List | [1, 2, 3], [] | Ordered sequence of values; concatenated with ++. |
Function | (x, y = 1) => x + y | Up to 12 parameters; captures enclosing bindings by thunk reference; supports kind annotations and defaults. |
Bindings, destructuring, and lambdas
Bindings in one let block can reference earlier or later sibling bindings. Record destructuring is strict by default and refuses extra fields unless ... is present.
let
width = 6,
cfg = { host = "local", scale = 3, extra = 99 },
area = ({ w: Integer, h: Integer = 1 }) => w * h
in
let { scale, offset = 0, ... } = cfg in area({ w = width, h = scale }) + offset
18Strict overrides with with
base with override checks that every field path in override already exists in base before forcing field values. Nested dotted paths update deep fields lazily while preserving sibling thunks.
let
defaults = { net = { host = "localhost", port = 8080 }, retries = 3 }
in
defaults with { net.host = "cache.modus.systems", net.port = 5010 }
{ net = { host = "cache.modus.systems", port = 5010 }, retries = 3 }To add new fields that do not exist in base, call extend(base, extra) or merge_deep(base, extra).
Record comprehensions
Record comprehensions construct canonical sorted records in one pass with lazy values in declaration mode.
{ for item in [{ k = "b", v = 2 }, { k = "a", v = 1 }]: item.k = item.v * 10 }
{ a = 10, b = 20 }Pattern matching with match
match tests literals, enum members, nested record patterns with optional name @ and ..., variable bindings, wildcards _, and boolean if guards. Uninspected record fields stay lazy, and unmatched values refuse with No_Matching_Arm.
let
cfg = { mode = .Verify, net = { port = 5010 }, unused = 1 / 0 }
in
match cfg {
{ mode = .Verify, net = { port = p }, ... } if p > 1024 => p,
{ mode = .Fast, ... } => 80,
_ => 8080,
}
5010Safe field fallback with ??
Lex has no null value. Right-associative ?? evaluates its fallback lazily only when a field along the left selection chain is absent. Errors inside present fields propagate instead of being masked.
let
cfg = { net = { host = "localhost" } }
in
cfg.net.port ?? 8080
8080Pipelines |> and placeholder _
The pipe operator |> passes its left value as the first argument of the right call, or into the explicit _ placeholder position.
[1, 2, 3, 4]
|> filter((x) => x % 2 = 0)
|> map((x) => x * 4)
|> fold(0, (acc, x) => acc + x)
24Resource bounds and refusals
Every evaluation enforces bounded execution without ambient I/O:
- Cycle detection: thunks transition through
Unforced -> Computing -> Forced; re-entering a computing thunk stops withCycle_Detected. - Step budget: VM instructions, thunk forces, equality, and serialization charge against
EVALUATION_BUDGET_STEPS(33,554,432 steps). - Depth bound: non-tail frames stop at
EVALUATION_DEPTH_LIMIT(4096) while tail calls run in O(1) stack space. - Checked arithmetic: division by zero stops with
Division_By_Zero; signed 64-bit overflow stops withOverflow.