1. Getting started
This chapter gets Tern running on your machine. You will install the two things the compiler needs, run a first program, meet the four commands you will use every day, and learn how to read the compiler's error messages, which are designed to tell you what to write instead.
What Tern is
Tern is a small, statically typed, compiled language. It reads like Python: blocks are made by indentation, there are no semicolons or braces, and most programs need very few type annotations. Underneath, the compiler (tern, itself written in Tern) translates your program to C, and clang turns the C into a fast native binary. There is no garbage collector and no runtime to install.
The idea that sets Tern apart is that it has no references. Every value, including arrays, maps, and structs, behaves like a copy. A function can only change a variable of yours if its parameter says mut and you pass the variable with &. Because of that, whole classes of bugs (one part of a program changing data that another part is holding, or two threads writing the same variable) are compile errors instead of surprises. You will see how this works in chapter 9; until then you can write code as you would in Python and it will simply do what it looks like.
Installing
Tern runs on macOS and Linux (arm64 and x86_64). Install it with:
curl -fsSL https://ternlang.dev/install.sh | shThat downloads the build for your machine, checks its SHA-256 sum, unpacks it into ~/.tern, and links the tern command into ~/.local/bin (it tells you if that directory isn't on your PATH yet). Running it again updates Tern in place. tern finds the standard library and runtime next to its own binary, so it works from any directory.
You also need clang, which builds the C that Tern produces. On macOS, install the command line developer tools, which include clang:
xcode-select --installOn Linux, install clang with your package manager (for example sudo apt install clang, or sudo dnf install clang). Programs that import sqlite (and the web template) also need SQLite's headers there: sudo apt install libsqlite3-dev or sudo dnf install sqlite-devel.
From a checkout of the Tern source, ./install.sh builds bin/tern instead and links it into ~/.local/bin. The first time, it starts from bootstrap/ternc.c, the compiler as one C file; afterwards tern rebuilds itself.
Hello, world
Save this as hello.tn:
fn main():
print("Hello, world!")Hello, world!and run it:
tern run hello.tnA few things to notice already:
- A program starts at
fn main():. There is no code at the top level of a file other than imports and definitions (functions, structs, enums). - The body of a function is indented, like Python. Use 4 spaces.
printtakes any number of values and prints them separated by spaces, followed by a newline.
Here is a slightly bigger program. You don't need to understand every line yet:
fn greeting(name: str, times: int) str:
return f"hello, {name}! " * times
fn main():
names = ["ada", "grace", "linus"]
for name in names:
print(greeting(name, 2))
print(len(names), "people greeted")hello, ada! hello, ada!
hello, grace! hello, grace!
hello, linus! hello, linus!
3 people greetedgreeting takes a str and an int and returns a str: the return type goes after the ), with no arrow. f"..." is an f-string, which puts values into text, and "..." * 2 repeats a string. The next chapters cover all of this.
The commands
You will mostly use four commands:
tern run prog.tn compile and run
tern check prog.tn only check the program for errors; prints "prog.tn: ok"
tern build prog.tn -o prog make a native binary you can copy and run anywhere
tern guide print the language referenceruncompiles to a temporary binary and runs it. Anything after--is passed to the program:tern run prog.tn -- input.txt.tern prog.tn input.txtis the short form.checkruns every compile-time check (types,mutrules, data races) without calling clang. It is the fastest way to find out whether a program is correct, and it is what your editor or an AI agent should run after every change.buildproduces a real executable. A hello world is a few dozen kilobytes and starts instantly.guideprints the language reference.guide stringsprints only the sections about one topic, which is handy when you forget the name of a function.
For tools and agents, check --json reports errors in a machine-readable form:
tern check --json prog.tn{
"ok": false,
"errors": [
{
"file": "prog.tn",
"line": 1,
"message": "expected `fn`, `struct`, `enum`, `type`, `const`, or `extern` at the top level",
"hint": "functions are written `fn name(x: int, y: str) int:` (return type after the `)`)",
"source": "def greet(name: str):"
}
]
}The command line page lists every command and flag.
Reading an error message
Tern tries hard to make its errors useful. Suppose you write Python out of habit:
fn main():
count = 3
print("count: " + count)
items = [1, 2]
items.append(3)3: error: `+` with mismatched types str and int (use float()/int() to convert)
3: hint: convert with `str(n)` ("n=" + str(n)) or use an f-string: f"n={n}"
5: error: unknown function `append`
5: hint: arrays grow with `xs.push(value)`check prints each problem as file:line: error: message. When the mistake looks like something from another language, a hint: line follows with the Tern way to write it. (In this tour, the file name is left off, so the lines start with the line number.) All the errors in a file are reported at once, so you can fix them in one pass.
Here the fixes are exactly what the hints say:
fn main():
count = 3
print("count: " + str(count))
print(f"count: {count}")
items = [1, 2]
items.push(3)
print(items)count: 3
count: 3
[1, 2, 3]Some mistakes can only be found while the program runs, such as reading past the end of an array. These stop the program with a message on stderr and exit status 2:
runtime error in main (hello.tn:4): index 5 out of bounds (length 3)The message names the function and the line; when the error happened in a function that others called, a second line shows the way it came, such as called from report <- main.
There are no exceptions to catch; a runtime error means the program has a bug. The compiler errors page explains the most common messages.
Try it
Write a program that prints a small multiplication table: the lines 1 x 7 = 7 through 5 x 7 = 35. You will need a loop; for i in 1..6: counts from 1 to 5 (the end is not included). Use an f-string for each line. Then make a deliberate mistake, such as writing "x" + i instead of an f-string, and read what check tells you.
Solution
fn main():
for i in 1..6:
print(f"{i} x 7 = {i * 7}")1 x 7 = 7
2 x 7 = 14
3 x 7 = 21
4 x 7 = 28
5 x 7 = 35Next: 2. Values and types. Reference: §1 Running programs.