Getting started

Install Avin, run a program, and run the tests.

Avin is early in its roadmap: the compiler builds whole programs to native executables, but the language it accepts is still a small part of the specification.

Install

On an Apple silicon Mac, with Homebrew:

brew install avin-lang/tap/avin
avin --version

The release is also a tarball, avin-<version>-darwin-arm64.tar.gz, laid out as a prefix: put its bin/ on your PATH, and avin finds the standard library and runtime in share/avin beside itself, through any symbolic link to it. It never writes there: the runtime archives and binding shims it builds are kept in ~/Library/Caches/avin/<version> ($XDG_CACHE_HOME/avin or ~/.cache/avin elsewhere; AVIN_CACHE names another directory), and so are a program’s executables and what its builds keep for the next, in a directory of the program’s own under build/ there. Nothing is written beside the program.

What an installed avin runs:

  • clang, which links every program: Xcode’s command-line tools (xcode-select --install) provide it.
  • sysl, which builds the shims of the Sysl bindings a program reaches, and the runtime archive when the shipped one does not match the runtime’s sources. A program that reaches no binding needs no Sysl at all, since the runtime ships prebuilt; the standard library’s string case mapping, files, processes and threads are bindings. Homebrew installs it with Avin.

A missing tool is reported by name, with what installs it.

Build from source

Working on the compiler, the library or the runtime needs:

  • Sysl 0.13.5, with a checkout of Sysl’s parsing package at ../../sysl-lang/parsing relative to the repository: the repository is cloned into a directory named for its organization (avin-lang/avin), beside a sysl-lang directory holding sysl-lang/parsing
  • clang on PATH

From the repository root:

sysl build compiler -o compiler/avin
./compiler/avin build examples/hello.avin
./hello

avin build file.avin writes ./file, and -o chooses another path. avin check checks a program without building it, and avin emit-llvm file.avin -o file.ll writes its LLVM IR. A checkout’s compiler/avin finds the standard library and runtime in the checkout; AVIN_ROOT names another location, and is only for development: an installed avin needs no setting.

avin run builds a program and runs it in one step:

./compiler/avin run examples/hello.avin
./compiler/avin run my-program -O0 -- first second

The program writes to the terminal as it runs, and avin run exits with the program’s own status; when a trap stops the program, the status is 128 and the signal’s number, as a shell reports it. The program, and whatever it starts, ends with avin run: a signal that stops avin run is passed on to it, and even an avin run killed outright takes it along. Everything after -- is passed to the program as its arguments. The executable is kept in the program’s own directory in the cache (running and testing), and the next run of an unchanged program starts it without building it again. Any change to the program’s sources, the standard library, the runtime or the compiler builds it again, and every build, avin build and avin test included, keeps what it made of each function there too, so a rebuild after an edit makes again only what the edit can change (incremental builds; AVIN_INCREMENTAL=off turns it off). avin clean removes the program’s cache and avin clean --all every program’s; either is always safe.

The cache looks after its own size: builds keep it within 5G, removing what was used longest ago first, and clear away what interrupted builds and older versions of Avin left. AVIN_CACHE_LIMIT sets another limit (20G, 500M, or off for none). avin cache info shows what it holds, avin cache trim trims it now, and avin cache clean empties it (--older-than 7d for only what has not been used for a week, --packages for the fetched packages too).

A program is a source file or a directory: avin run my-program builds every .avin and .lavin file under my-program/. build, run and test all take -O0 or -O2, and build at -O2 by default. build and emit-llvm also take -g, which builds the program with debug information for a debugger such as lldb: Debugging shows a session.

run is the default command and the current directory the default program, so inside a package (or a directory of .avin files) these are all the same:

avin
avin run
avin run .

avin hello.avin -O0 -- first second is avin run hello.avin -O0 -- first second: a first argument that is not a command name is the program to run. Options with no program before them, as in avin -O0 or avin -- first, are for the current directory’s program. parse, check, build, emit-llvm, run and test likewise take the current directory when no program is named (avin build, avin check, avin test); tangle and weave always name their file. A directory with no avin.hocon above it and no .avin or .lavin files is not a program, and avin there says so.

A command name is always the command. A file or directory named like one is reached by a path that is not the bare word: avin ./test, or avin run test. A first argument that is neither a command nor an existing path is refused with the list of commands.

Literate programs

A .lavin file is a Markdown document whose lines indented four columns are the program (Literate source has the rules). Every command that takes a source file takes one, and a directory program may mix .avin and .lavin files freely. examples/digits.lavin is one. Two commands run each half of a literate system on its own:

./compiler/avin tangle examples/digits.lavin
./compiler/avin tangle examples/digits.lavin -o digits.avin
./compiler/avin weave examples/digits.lavin -o digits.html
  • avin tangle prints the program with the prose stripped and the four columns taken off, line for line with the document, so line 30 of what it prints is line 30 of the file. It shows exactly what the compiler reads, which is the first thing to check when a literate file misbehaves: a block indented that should not have been, or a fence that swallowed a function.
  • avin weave renders the document as one HTML page, with its stylesheet inside it: the prose as Markdown with GitHub’s extensions, and every program block, and every fenced block marked avin, coloured by the compiler’s own lexer. Without -o the page goes to standard output.

Both read the file as a build does, so a file the compiler refuses (a tab in an indentation, a fence never closed) is refused with the same diagnostic, and neither needs the library or the runtime.

Testing a program

A test is a function marked @test that takes nothing and yields nothing. It passes by returning, and fails by stopping, which a failed assertion does:

clamp(n: int, low: int, high: int) -> int = if n < low then low elif n > high then high else n

@test("clamps below the low bound")
below()
  assertEq(clamp(0, 1, 3), 1)

@test
leavesInRange()
  println("clamping 4")
  assert(clamp(4, 1, 3) == 4, "four is left as it is")

assert(condition) and assert(condition, message) stop the program when the condition is false; assertEq(actual, expected) stops it when the two differ, showing both in their Debug form. Each reports the place of the assertion itself.

avin test builds the program’s tests into one executable and runs each test in a process of its own, so a test that traps stops only itself. A test’s process, and whatever it starts, ends with the test — passed, failed, trapped or timed out — and with avin test itself, however that is stopped:

./compiler/avin test my-program
./compiler/avin test my-program "low bound"

With no program named, avin test tests the current directory. Its first argument is the program when it names an existing file or directory, and the filter otherwise: avin test "low bound" filters the current directory’s tests, and avin test . low filters when a path called low also exists.

The report is written as the tests finish, at most twice a second, so a log read while the run is alive is current. SIGTERM or SIGINT writes out the rest at once, lists every test and build still in progress with the time it has taken, and exits with 143 or 130. --prefix puts each file’s path and a colon before every result line, for output that shares a log with other runs.

running 2 tests

clamp.avin
  ok    clamps below the low bound  2ms
  FAIL  leavesInRange               1ms
        assertion failed: four is left as it is at clamp.avin:10:3
        > clamping 4

1 passed, 1 failed — 4ms

Tests are grouped by the file they are written in, in the order they are declared. A failing test shows why it failed, then everything it printed, each line after >; a passing test’s output is not shown. The exit status is 1 when any test failed. A second argument keeps only the tests whose name or file contains it. A program built for its tests need not have main, and avin run and avin build never run its tests.

A test may answer with a Result[unit, E] instead, failing on an Err, and @test may say more about running it: should_trap for a test that passes by trapping, timeout: 5 for a limit of its own, ignore: "why" for one that is checked and not run. @setup and @teardown mark functions run around each of a module’s tests, and @setup_all and @teardown_all once around them all. A file whose header says @tests holds fixtures and helpers only the tests may use, and only avin test builds. The declarations chapter has the details.

Packages

avin new writes a package: a directory with an avin.hocon manifest, a module, a test and a .gitignore. --lib makes a library, which has no main:

./compiler/avin new hello
./compiler/avin run hello
./compiler/avin test hello
./compiler/avin new geometry --lib
package {
  name = "hello"
  version = "0.1.0"
}

dependencies {
  geometry { path = "../geometry" }
  json { git = "github.com/owner/json", version = "1.4.0" }
}

Every command takes a path anywhere in a package and compiles the whole package, with the packages it depends on: avin run hello/main.avin and avin test hello/tests both mean hello. A dependency’s modules are imported by their own names. avin build names the executable after the package. Fetched packages are kept in the machine’s cache, and avin.sum beside the manifest records what each one hashed to; commit it. avin add github.com/owner/json@1.4.0 writes the dependency into the manifest and fetches it, and without @1.4.0 takes the highest release. avin tidy rewrites it to exactly what the package uses, and avin tidy --check fails when it is not. The packages chapter has the rest.

A program of one file uses packages too, with no manifest: its first lines name them, after a // avin.hocon line, in the comment lines under it, written exactly as a manifest’s dependencies block is. A #! line may come first, so the file runs as a command:

#!/usr/bin/env avin
// avin.hocon
// dependencies {
//   json { git = "github.com/avin-lang/json", version = "0.1.0" }
// }

import dev.avinlang.json

main(args: Vector[string])
    ...
chmod +x tool.avin
./tool.avin data.json

avin run, build, check and test fetch what the header names into the same cache, and the hashes go in tool.avin.sum beside the file. A blank line ends the header. Run through its #! line, ./tool.avin data.json is avin ./tool.avin data.json, and every argument after the file is the program’s own; avin run tool.avin -O0 -- data.json keeps the usual rule, with -O0 avin’s. The packages chapter has the rules.

The interactive session

avin repl starts a session: each declaration, import, statement or expression entered is checked, built and run at once. An expression’s value is shown with its type and kept under the next name res0, res1, …; a val or var is shown by its own name, and a function by its header:

avin> 1 + 2
res0: int = 3
avin> val greeting = "hello"
greeting: string = "hello"
avin> twice(n: int) = n * 2
twice(n: int) -> int
avin> twice(res0)
res1: int = 6
avin> import avin.threads.spawn
avin> val worker = spawn(() -> twice(21))
worker: Thread[int] = Thread[int]
avin> worker.join()
res2: int = 42

A session is a program that grows. Imports go to its top, and everything else into main, each input a block nested in the one before it, so a name entered again hides the earlier one from the inputs after it. A declaration of types is a module of its own, and so is a function or a val that uses no var or other local of the session’s code, so the classes and functions entered after it can call or read it, and keep the one they were declared after when it is entered again:

avin> twice(n: int) = n * 2
twice(n: int) -> int
avin> class Doubler
    |   run(n: int) -> int = twice(n)
    |
class Doubler
avin> twice(n: int) = n * 3
twice(n: int) -> int
avin> Doubler().run(4)
res0: int = 8

A function or val that uses a var of the session’s stays in its code, where declarations cannot reach it; and the session’s code sees a type declared again in place of the earlier one, so a new declaration is refused where earlier code no longer checks against it. After each input the whole program is built, through the same cache avin run uses, and run; what the earlier inputs write is not shown again. That costs something worth knowing: an earlier input runs again each time, so its side effects repeat, unseen, and a value it computes afresh (a random number, the time) may differ from the one shown when it was entered. An input that does not check, traps, or ends the program is left out, and the session goes on without it; a diagnostic or a trap names its place in the input as it was typed.

An input that opens a block (an if, a for, a function’s body, a type’s declaration with its members) goes on over the lines after it until an empty line; so does one with a bracket left open or ending with an operator. At a terminal the input is edited whole: Enter on an unfinished input begins a new line under a | prompt, indented for what the line above opened, and the arrow keys move through its lines and, past the first or the last, through the inputs entered before. Tab completes the name before the cursor (a local, a declaration, a member after a ., a module after import), listing the choices when there are several, and indents at a line’s start. Ctrl-C abandons an input or stops a running one, and Ctrl-D on an empty input ends the session. Read from a file or a pipe, the session prints no prompts.

Command
:type <expression>the expression’s type, without running it
:importsthe session’s imports
:load <file>each import and top-level declaration of the file, entered in turn; a main in it becomes a function the session can call
:resetforget every input
:helpthe commands
:quitend the session

In a package, or a directory holding a program, the session may import the program’s modules, and reads its dependencies; the program’s own root module, with its main, is left out, the session taking its place.

Formatting

avin fmt rewrites every .avin and .lavin file under the paths it is given (the current directory when there are none) in Avin’s one style, and avin fmt --check lists the files that are not in it and fails, for a build or a commit hook:

./compiler/avin fmt hello
./compiler/avin fmt --check hello

Each block level is two spaces in, and a line is broken only past 120 characters, one item of a list to a line; a sequence or set literal of short numbers, characters, strings, booleans or names is instead filled, as many items to a line as fit, so a table stays a table. Comments and blank lines stay where they are, at most one blank line in a row, except that a function’s signature and its clauses stay together with no blank line among them. In a literate file only the program is formatted, inside its four-column margin. A file that does not parse is left alone and its errors are reported. The decisions list the style in full; the language server formats with the same rules.

API documentation

avin doc writes a program’s API documentation as a static site: a page per module, listing every public declaration with its header and its doc comment, and an index of the modules with a box that searches every name. A type named in a signature links to its declaration, in another module, a dependency or the standard library, whose pages the program reads are written beside its own. The pages are plain HTML with one stylesheet, light or dark as the reader’s system is, and fetch nothing from anywhere.

./compiler/avin doc hello
./compiler/avin doc hello -o site
./compiler/avin doc hello --check
./compiler/avin doc --std -o std
  • The program is checked first, as avin check checks it, with every feature on; one that does not check is refused with its diagnostics. A package, a directory of modules and one file are each documented; a test module and a private declaration are not.
  • The pages go to -o, or to .avin/doc beside the program, which a package’s .gitignore already names. A directory is written only when it is new, empty, or one avin doc wrote before, whose old pages it replaces.
  • --check writes nothing, and fails on a doc comment’s link that leads nowhere and on an Avin block in one that does not parse, for a build or a commit hook. Without it the same problems are warnings.
  • --std-url <url> links the standard library’s names to its pages published at that URL instead of writing them beside the program’s. avin doc --std documents the whole standard library.

The compiler’s tests

sysl test compiler

This runs the compiler’s unit tests and every program in tests/, which are built and run at -O0 and -O2.

Editors

avin lsp is a language server for any editor with a Language Server Protocol client: Editors says what it answers.

Documentation

juicer build -s docs -d docs/public
juicer serve -s docs -L

Pages

  • Editors — The language server, `avin lsp`, and what it answers.
  • Debugging — `avin build -g`, and stopping a program at a line of its Avin source in lldb.

Search

Esc
↑↓ to navigate ↵ to open Esc to close