Skip to the page
Start here / Getting started

Start here01

The language02

Programs and the world03

Kiln04

01 Start here / 01

Getting started

Build Hematite, run a first program, and meet the commands you'll use every day.

This page takes you from a copy of the source to a running program in a few minutes. On the way you'll see how Hematite answers a mistake, and you'll meet the handful of commands you'll use from then on.

Everything goes through one program, hem: it runs, checks, formats and explains Hematite code. Packages come through lode, which comes with it.

Install

Hematite comes as a release for Linux on x86_64, a file called hematite-0.1.0-x86_64-linux.tar.gz. It holds bin/hem, bin/lode and, in lib/hematite, the runtime hem build links programs with. Unpack it into ~/.local, where bin is usually on your PATH already:

terminal
tar -xzf hematite-0.1.0-x86_64-linux.tar.gz --strip-components=1 -C ~/.local --wildcards '*/bin' '*/lib'
hem --version

Or unpack it anywhere and put its bin folder on your PATH. There are no releases to download yet: for now, ./release.sh in a copy of the source, with Rust installed, builds one in dist/. When hem --version answers, it worked, and every command after this assumes hem is on your PATH.

A first program

Save this as hello.hem, and run it with hem run hello.hem:

hello.hem
const names = ["Ada", "Grace", "Linus"]

names.each do (name)
  print("Hello, #{name}!")
end

Prints

Hello, Ada!
Hello, Grace!
Hello, Linus!

There's no main to write and nothing to set up first: a file is a program, read from the top. const names a value that never changes, each runs the block between do and end once for every name, and #{...} puts a value into a string.

Mistakes, before anything runs

Hematite checks every name and every type before the program starts, even in code that would never run, and a program with one of these mistakes doesn't start at all. Here, name might be nil, and that has to be dealt with before it's used:

greet-unchecked.hem
def greet(name: String?): String
  return "Hello, #{name.upcase()}!"
end

print(greet("Ada"))

hem run says

greet-unchecked.hem:2:20: error: name might be nil here; check it first, like 'if name != nil'

A message names the mistake and, wherever there's a right way to write it, says what to write instead. Messages are written for whoever fixes the code, a person or an agent, so following one leads to working code:

greet-checked.hem
def greet(name: String?): String
  if name == nil
    return "Hello, whoever you are!"
  end

  return "Hello, #{name.upcase()}!"
end

print(greet("Ada"))
print(greet(nil))

Prints

Hello, ADA!
Hello, whoever you are!

After if name == nil returns, Hematite knows name is a String, so name.upcase() is allowed. There is no way to skip the check: a value that might be nil can only be used once the code has checked it. Checking for nil has the details.

Where Hematite is today

Hematite is in early development. Here's what it has today; each part has its page in the guide.

Values
Numbers, strings, booleans and nil, with arithmetic, comparisons, and, or and not, and conversions (Float(x), Int(x), String(x)).
Flow
Variables with const, var and static; conditions with if, else if and else; loops with while; and choosing a value with if ... then ... else.
Structure
Methods (def), arrays and hashes, classes and records, traits, enums and when, and blocks.
Programs
Files (import and export), failures (!, try and rescue) and the built-in methods. Hematite is only a language: serving and making HTTP requests are the http package's, and web applications are built with Kiln, a package too.

There are no symbols, inheritance or modules. Names and types are checked before anything runs, so most mistakes are found even in code that never runs: checked before running says more.

The commands

These are all of hem's commands. You'll use run and fmt every day; the others are there when you need them.

terminal
hem run app.hem a b      # run app.hem, with the arguments a and b
hem build app.hem        # make app: the program as one executable of machine code
hem foreign app.hem      # list the C the program talks to (see "Foreign files")
hem parse app.hem        # show how each statement is understood
hem disassemble app.hem  # show the bytecode the program compiles to
hem fmt app.hem          # rewrite app.hem in the standard layout
hem fmt --check app.hem  # exit status 1 if app.hem isn't formatted
hem repl                 # type code and see results, entry by entry
hem lsp                  # the language server, for editors
hem guide                # print the guide
hem --version

Running and building

hem run app.hem runs a program, and anything after the file name goes to the program as its arguments. hem repl lets you type code and see the results, entry by entry.

hem build app.hem makes app (or the name after -o): the whole program compiled to machine code, in one executable that runs without hem. It does exactly what hem run does, with the same output, mistakes and exit statuses. It runs on any machine with the same system and kind of processor, not only the one it was built on: on x86-64, any since about 2009 (x86-64-v2). Building needs the system's linker, cc, on the machine where it builds; the program it makes doesn't. It also needs the runtime, libhematite.a, from the same sources as hem: one from others is refused, with what to do, rather than linked into a program it doesn't fit.

Seeing how code is understood

hem parse shows how each statement is understood, as a tree in prefix form, one per line. It only needs valid syntax: names and types aren't checked. Use it whenever you're unsure how an expression groups:

parse.hem
const total = 1 + 2 * 3

print(1 - 2 - 3)

hem parse shows

(const total (+ 1 (* 2 3)))
(call print (- (- 1 2) 3))

(+ 1 (* 2 3)) means "add 1 to the result of 2 times 3", and (- (- 1 2) 3) shows that 1 - 2 - 3 subtracts from the left.

hem disassemble shows each method's bytecode. When the program starts, the methods that gain from it also become machine code, and are marked (native code): methods of Ints, Floats and Booleans alone, methods and scripts with a loop, and the methods those call. Native code does exactly what the bytecode does, mistakes and their messages included, only faster.

Editors and C

hem lsp is the language server, for editors: it shows mistakes as you type, types on hover, where a name is defined, and what can come next. It works in templates as in .hem files, and in a template it also leads from an imported template's name, its render and the names of its arguments to that template's takes. hem foreign app.hem lists the C a program talks to, which foreign files explains. hem guide prints the guide; more on that below.

Formatting

Run hem fmt on every file you write or change. It lays the code out one way, so nobody has to decide on layout and every file reads the same. It never changes what the code means, and it only formats code without syntax errors. hem fmt --check app.hem leaves the file alone and exits with status 1 if it isn't formatted.

Here is what it does:

  • Indentation: 2 spaces per block (if, while, def, class, do), and one more for a line that continues the line before it.
  • Spacing: one space around operators and after commas; none inside (), [] and {}; name: Type; do (item).
  • Long lines: a line longer than 100 characters (counting its indentation, not a comment after it) is broken at its outermost brackets: a call's, a new's, a definition's parameters, a list's or a hash's. Each item goes on its own line, one level in, with a comma after it, and an item that's still too long is broken the same way. Brackets spread over lines that would fit on one are joined back up, unless that would lose a comment between them.
  • Comments after code, on lines one after another, are lined up.

Blank lines

Blank lines show the shape of the code. A block (if, while, when, def, class, a call with do, and so on) gets one before it and one after its end, wherever there's code at its level on that side. So does a return, unless it's the first line of its block.

Declarations (const, var, static, a class's fields) stay together in a group, and so do statements that are calls (print(x), try bar.start()), with a blank line between a group and whatever sits next to it. Comments right above a line stay with it. There's one blank line after the foreign line and one after the imports. Otherwise there's at most one in a row, and none at the start or end of a block.

Here's a file as hem fmt leaves it. The new Book(...) line was too long for one line, so each argument has a line of its own:

layout.hem
record Book
  title: String
  author: String
  year: Int
end

const book = new Book(
  title: "The Left Hand of Darkness, in the edition with the long introduction",
  author: "Ursula K. Le Guin",
  year: 1969,
)

if book.year < 1970
  print(book.author) # prints Ursula K. Le Guin
end

print(book.year) # prints 1969

Templates

hem fmt page.och lays out an Ochre template: its imports together, then takes and a component's client (laid out as a .hem file), each set apart by a blank line; then the markup, two spaces for each level in. An element's attributes are a line each under it, then a blank line, then what's in it, and end.

A block (an element with an end, an if, an each, a when) has a blank line before it and after its end, unless it starts or ends the block around it, as in Hematite; else lines up with its if, and a when's is lines go one level in. Lines of text, and elements whole on their line, sit together, and so does an element with its text on its line inside a sentence. Values are laid out as Hematite lays out code.

layout.och
ul
  items.each do (item)
    if item.done
      li item.name
        class: "done"
      end
    end
  end
end

if items.length == 0
  p "Nothing yet."
end

p "#{items.length} items"

The page it makes is the same: nothing a template's layout changes goes into it.

For agents

hem guide prints the complete guide, written for AI agents: every rule, and an example for each, all run as tests. This site says the same things for people, and a test keeps the two in step. Give an agent hem guide before it writes Hematite: it hasn't seen the language in training.