📚 Encyclopedia

Programming Languages · updated 2026-11-30

Designing a programming language's syntax and semantics (lessons from H)

language-designsyntaxsemanticspipelinesstatic-typingh-language

Designing a language is mostly a long series of small decisions, each of which looks harmless alone and interacts with all the others. This article collects the reasoning behind the decisions made for H, Justin's hobby language (imperative, braces, | pipelines, compiled to native x86-64 by hc, repo ~/dev/h), and generalizes the ones that apply to any language. The compiler side is in Writing a native compiler in C that emits x86-64 assembly.

Running the design process

Grammar: keep it unambiguous on purpose

Things declined, and why

Semantics worth deciding early

Variables without a value: no silent defaults

H first gave unassigned variables Java-style defaults (0, "", false, []). When the designer noticed, it turned out he had never chosen that; it had ridden along in a proposal. The replacement is definite assignment, checked at compile time when the answer is certain and at run time when it isn't: - int x; echo x; doesn't compile. int x; if c { x = 1; } echo x; compiles and fails with "'x' is used before it has a value" only when c was false. - The checker follows the program in order: both branches of an if must assign (a branch that ends in return/break doesn't count), the right side of &&/|| may not run, a loop body may run zero times (what it assigns is "maybe set" afterward), and a call may assign any global. - Proven reads cost nothing; "maybe" reads compare against a hidden "no value" marker. - x = e doesn't need a value; x += e and x++ do.

Java requires definite assignment everywhere and rejects the "maybe" case; C leaves it undefined. The hybrid keeps simple programs compiling while never inventing a value. Lesson for any language design: list every default explicitly and get each one approved; implicit ones slip through.

Closures: share, don't copy

A function literal (const f = (int x) int { x * 2 };) shares the variables around it, the JavaScript/Python rule: count++ inside changes the real count, and later outside changes show inside. C++-style copies made at creation were rejected because count++ would silently change only the copy. This matches the choice to share lists. Until function types have a name, a literal may only be the value of an untyped const and can't be passed, returned or stored, which also means it can never outlive its frame (see Writing a native compiler in C that emits x86-64 assembly). No self-call through the const's own name; use a named fn for recursion.

Mutable collections: sharing, freezing, identity

Records first, methods second, inheritance when a program asks

A record is a class minus methods, constructors, private members and inheritance, so starting with records closes nothing off. - Definition and value: type Room { string name; int visits = 0; } and Room { name: "Hall" }. Fields are named at the use site, in any order; a field without a default is required. Defaults must be plain values, and each record gets a fresh copy. - Records follow the list rules: shared on assignment, deeply frozen in a const, == field by field, and printing shows the literal form. - Methods arrived as a second step once records had been used: fn enter() { visits++; } inside the type, called as hall.enter(). - Explicit this. first, bare names later. Requiring this.name can be relaxed afterward without breaking code; the reverse cannot. It was relaxed within a day (too much typing), using Java's lookup order: local, then field, then top level. - Adding methods exposed a rule that was too strict: a loop parameter was constant, so rooms | each (r) { r.visits++; } failed to compile. The rule was narrowed to "the parameter cannot be reassigned, but a record it holds can be changed". - An error that lists what exists ("Room has no method 'x' (it has enter, describe)") costs one line and saves a lookup.

Modules

Error handling

Pipelines as a core operator

H's | streams values left to right, lazily, one item at a time: open("notes.txt") | each (line) { echo line[0]; }; - One job per stage. each is pass-through (like tee): its function's result is discarded and the item flows on. Transforming is a separate map. Splitting (each<sep>) never joins; join<sep> is its own stage. - Layered splitting: every each cuts its input at its separator, so file | each | each<" "> (w) { } walks every word of every line. - When does a lazy pipeline run? A pipeline used as a statement drains; one bound to a name stays a lazy stream until consumed. Streams are read once. - break and return in each bodies act as in the while the pipeline stands for: break stops the whole pipeline (items already passed on are still joined), return leaves the enclosing function. There is no skip-one form, as there is no continue. - Lists as sources are walked, not cut: each stage's input decides (text is split, a list is iterated). each (x) takes the items' type, and an optional written type (each (int x)) is checked. x | join<sep> and x | echo may follow the source directly (an implicit each). - Streams get a type name (stream) so they can be variables, parameters and return values. - Inline literal stages: when the stage function is a literal, the compiler can turn the whole pipeline into nested loops, so pipelines work before closures exist.

Functions and globals

See also

Linked from

Sources: h repo DESIGN.md, h memory, tldr posts #1819 #1846 #1863 #1873 #1876, 2026-10-22..10-31, h memory (records, modules, error-handling), tldr post #1920