Start Here

Introducing Topaz

See Topaz code through examples and choose where to start.

Topaz is a general-purpose programming language with typesetting built in. Code that does the same job looks the same no matter who writes it.

With Topaz you write programs that typeset books and documents, as well as command-line tools, data transformations, and application logic. The same source runs on your computer or builds into a package for the browser. See working products and their source code in Built with Topaz.

To try it now, install Topaz and run your First Program, or open the Playground without installing anything. To learn step by step, follow the Learning Path, and keep Syntax at a Glance open when you need to look up a form.

Who it fits and where to start

In typesetting, Topaz decides where lines and pages fall, and the output tool you already use draws the page. The same language also builds web tools and command-line tools.

A book typesetting engine

The typical project is an engine that takes a manuscript and decides where its lines and pages fall. Atlas is one, and part of its line-breaking source is on Built with Topaz. Paragraph breaking, the Korean, Japanese and Chinese line-break rules, character widths and spacing come from std.typeset, and you build page breaking on top. To start, open the std.typeset example in the playground, replace its sentence with a paragraph from your manuscript, and change the width to see which words end each line. The same line-breaking code can run in your browser preview and in your print composition.

Rules that differ by publisher and by book

Decisions that differ by publisher and by book, such as where a chapter starts or how many orphan lines to allow, go into Lispex rule files. Topaz takes each decision and carries on with the layout, and the line-breaking code is shared across books. Follow Call a Lispex rule to run a small rule, then change its condition, rebuild the lock file and see how the result changes. Lispex rules run in the interpreter and in native executables.

Data handling inside a web app

Write a function that handles input, such as checking JSON, in Topaz, build it as a browser package and call it from JavaScript in your existing app. The browser tools on jeni.kr work this way, and its JSON formatter reports the line and column of a syntax error. Start with Call it from JavaScript and call one function in the browser. Building an executable also needs Rust with Cargo, and Backends lists what it needs.

Topaz improves its compiler's performance with each release. The plan for the next one is on the roadmap.

Use names from your work

The same program can use Korean, Russian, and English names.

TOPAZ
import std.io

function greet(name: string, language: string) -> string {
    return match language {
        case "한국어" => "안녕하세요, {name}님!"
        case "Русский" => "Привет, {name}!"
        case _ => "Hello, {name}!"
    }
}

let 사용자 = "김토파즈"
io.writeLine(greet(사용자, "한국어"))
io.writeLine(greet("Topaz", "Русский"))
Output
안녕하세요, 김토파즈님!
Привет, Topaz!

사용자 is a variable name, just like name. Topaz keeps the characters you enter. Names match when their characters match, and the compiler rejects names that could be confused during module lookup.

{name} inside a string inserts a value. For paths, regular expressions, shell commands, and SQL queries, the p, r, sh, and sql templates keep the text and the inserted values apart. Strings & Templates covers the details.

Handle errors and clean up

This example returns either a port number or an error, then runs cleanup in both cases.

TOPAZ
import std.io

function parsePort(raw: string) -> Result<int, string> {
    let n = raw.toInt() ?? -1
    if n < 1 {
        return Err("not a port: {raw}")
    }
    return Ok(n)
}

function startup(raw: string) -> Result<string, string> {
    defer io.writeLine("config closed")
    let port = parsePort(raw)?
    return Ok("listening on {port}")
}

io.writeLine("{startup("8080")}")
io.writeLine("{startup("http")}")
Output
config closed
Ok(listening on 8080)
config closed
Err(not a port: http)

Result represents success as Ok and a recoverable error as Err. ? returns an Err to the caller. defer runs cleanup when the scope ends, including an early return, so both calls print config closed. Use Option with Some or None when a value may be absent.

Find errors before running

This function accepts three traffic-light values and returns the next one.

TOPAZ
type TrafficLight = "red" | "yellow" | "green"

function next(light: TrafficLight) -> TrafficLight {
    return match light {
        case "red" => "green"
        case "green" => "yellow"
        case _ => "red"
    }
}

The compiler checks types, resolves names, and reports missing match cases before execution. These are static checks. It cannot decide whether the program implements the logic you intended. Here, the types allow any of the three colours, so tests must check that the transitions are right.

Choose how to run your program

topaz run checks a file and executes it. topaz build creates a native executable or a package for the browser from the same source. Native builds need the Rust toolchain, including Cargo. Follow First Application for the files, setup, and expected result.

The core of the Topaz compiler is written in Topaz too. Built with Topaz explains what that compiler does. See Toolchain Status for the compilation stages, supported targets, and installation details.

Read the language philosophy for the design reasons. From 5.21, each release publishes the compiler source as one seed archive at topaz.ooo/dl.