A language with typesetting built in
LinkTopaz is a general purpose language with typesetting built in. This nonnormative tour connects its main ideas through a complete program. The linked reference chapters define the rules.
Bindings
LinkBindings name values and introduce names into lexical scopes. Use let for immutable bindings, let mut for reassignment, and const for constant definitions.
Types
LinkTypes describe values and guide inference from annotations and surrounding expressions. Records, enums and newtypes give data a named shape, alternatives, or a distinct identity.
Functions and abstraction
LinkFunctions and generics define reusable calculations with parameter and result types. Functions are values, and lambdas provide short function expressions. Protocols and implementations name capabilities that generic code can require.
Execution and control flow
LinkExpressions and operators compute values, while blocks and control flow sequence work and repeat effects. Pattern matching selects code by the shape of a value. Collections and comprehensions collect values from iteration.
Determinism
LinkLanguage determinism concerns the order of evaluation and the values a program computes from its inputs. Typesetting determinism concerns the layout that program produces. For the same program and text, the same font data, measurements, Unicode version, policies, grid, and product release produce the same typesetting result. Font data and measurements are program inputs.
Failure handling
LinkFailure and propagation separates expected errors in Result from runtime faults. Ok(value) carries success, Err(error) carries failure, and ? forwards an error from the enclosing function.
Concurrency
LinkConcurrency runs named arms as a group and collects their values in a record. A Duration deadline gives the group a timeout and an else path.
Modules
LinkModules split a program into files and expose selected declarations through export and import. Imported declarations retain their defining identity, whether selected by name or reached through a module namespace.
Libraries
LinkThe library provides bytes, data formats, files, and standard input and output. Typesetting supplies measured lengths, text break classification, and paragraph plans. Lispex evaluates packaged rules with explicit inputs and limits.
A complete program
LinkSave the program as language-. It prints each valid job and reports an error for a job with no copies.
Print jobs and handle an error
Linkimport std.iorecord Job { text: string, copies: int }function checkedCopies(job: Job) -> Result<int, string> {if (job.copies > 0) {Ok(job.copies)} else {Err("copies must be positive")}}function printJob(job: Job) -> Result<int, string> {let copies: int = checkedCopies(job)?for copy in (0 ..< copies) {io.writeLine(job.text)}Ok(copies)}let jobs: Array<Job> = [Job { text: "Topaz", copies: 2 },Job { text: "Draft", copies: 0 },]for job in (jobs) {match (printJob(job)) {case Ok(copies) => io.writeLine("printed {copies} copies")case Err(message) => io.writeLine(message)}}
Topaz Topaz printed 2 copies copies must be positive
Expected result
Expected result
Execution completes. Exit code 0.
TopazTopazprinted 2 copiescopies must be positive
Source evidence
- parseEnum; parseRecord at
b48527d0a174· 294-411 - Point construction at
b48527d0a174· 1-4 - iteration, iterationBinding, and emitFor at
b48527d0a174· 1840-1893 - emitTry at
af88afbc1d99· 1927-1933 - inferTryExpression at
af88afbc1d99· 1123-1166 - emitMatch at
b48527d0a174· 2274-2308
The first job reaches the loop and prints two lines. The second job returns an error through ?, so the final match prints its message.