Guides · 17
Tooling
Vyi comes as two programs: vyi, the compiler, which you run from the shell,
and vyi-language-server, which gives editors errors, hover, completion, and
the rest. This guide shows how to install them, the commands you use day to
day, how to pick a target, and how to set up an editor. The
CLI reference lists every flag and exit code.
Install
Vyi runs on Linux, on x86-64 (amd64) and arm64 machines. The install script
downloads vyi, vyi-language-server, and the standard packages, and puts
them under ~/.local:
curl -fsSL https://forgejo.roberthurst.ca/robert/Vyi/raw/branch/master/scripts/install.sh | bash
export PATH="$HOME/.local/bin:$PATH"
vyi helpTo build from a checkout of the repository instead, you need Go. Build the compiler and tell it where the standard packages are:
cd bootstrap-compiler
go build -o vyi ./cmd/vyi
export VYI_STDLIB_PATH="$PWD/../packages"
./vyi run path/to/main.vyiVYI_STDLIB_PATH names the folder that holds @fmt, @io, and the other @
packages. The --packages <dir> flag does the same for one command. The
install reference has the script's
options and the other places vyi looks for packages.
Running a program
vyi run compiles a program and runs it straight away. Give it the entry file:
import { println } from "@fmt"
fn main() !u8 {
println("hello") // prints hello
return Ok(0)
}vyi run hello.vyiThe exit code of vyi run is your program's exit code:
mainreturningOk(n)exits withn.mainreturning anErrprintserror:and the error's message, and exits with1.- A panic prints
panic:, its message, and where it happened, and exits with101.
Everything after the file name goes to your program as its arguments, which it
reads with args() from @io. A -- before them is optional and is dropped:
vyi run hello.vyi -- --verbose orders.txtIn a package with a package.vyipac manifest (guide
14), you can name a program from the manifest's
entries instead of a file. vyi run shop runs the entry called shop, and
vyi run with no name runs every entry in turn.
Flags go before the file
Flags for vyi go between the command and the file or entry name:
vyi run --target x86-64-linux hello.vyiNothing after the file name is read as a vyi flag. For vyi run it belongs
to your program; the other commands reject it with a usage error that says the
flag goes first.
The commands
vyi help lists the commands:
vyi runcompiles and runs a program.vyi buildcompiles a program and writes the result to a file.vyi checkreports errors without producing anything.vyi testruns your tests (guide 16).vyi astprints the syntax tree of a program as JSON.
vyi build
vyi build writes the compiled program to out/<target>/<name>. For the
default target that is a WebAssembly module:
vyi build hello.vyiwrote out/wasm/hello (10843 bytes)-o picks the output path instead. When you build every entry of a package
at once, -o names a folder and each program goes under it:
vyi build -o hello.wasm hello.vyi--emit picks what to write. --emit=wat prints the WebAssembly as text, and
--emit=asm prints the assembly for the target you build for. Both print to
the terminal unless -o names a file:
vyi build --emit=wat hello.vyi
vyi build --emit=asm --target x86-64-linux hello.vyivyi check
vyi check runs every check the compiler makes and stops before producing
code. It exits with 0 when the program is clean and 1 when it found an
error. Use it in an editor's save hook or a CI job. --json prints the errors
in a form other tools can read, described under
Errors.
vyi test
vyi test finds every file whose name ends in .tests.vyi, in the current
folder and the folders below it, and runs each one. It exits with the number
of test files that failed. Guide 16 shows how to write tests.
vyi ast
vyi ast prints the program's syntax tree as JSON, for tools and for looking
at how the compiler reads your code. --pre-elaborate prints the tree as it
was parsed, before the compiler rewrites shorthand forms, and --spans adds
the source position of every node.
Targets
--target picks the machine the program is compiled for. There are three:
wasm, the default: a WebAssembly module.vyi runruns it with a runtime built intovyi, which passes your terminal's input and output through and lets the program open files in the current folder.x86-64-linux: a native Linux program for x86-64.arm64-linux: a native Linux program for arm64.
vyi run hello.vyi
vyi run --target x86-64-linux hello.vyi
vyi run --target arm64-linux hello.vyiThe two native targets need a C compiler to link the program: cc, gcc, or
clang for x86-64. For arm64 on an x86-64 machine, vyi uses
aarch64-linux-gnu-gcc to link, and vyi run runs the program with
qemu-aarch64.
The compiler's own test programs run on all three targets and must print the
same output on each, so you can develop on wasm and ship a native build.
Build modes
--mode picks between two build modes:
debug, the default: integer overflow stops the program withpanic: integer overflow.release: integer overflow wraps around, and on x86-64 the compiler optimizes the code.
vyi build --mode=release --target x86-64-linux hello.vyiGuide 02 shows what overflow does in each mode.
Errors
The compiler doesn't stop at the first error. It reports every error it can
find in one run, even after a syntax error, so one vyi check gives you the
whole list. Each error shows the line it's on and points at the problem:
fn main() !u8 {
con quantity: i32 = "three" // error: this value has type [*con]u8, but the binding is declared i32
con paid: bool = 1 // error: this value has type i32, but the binding is declared bool
return Ok(0)
}error: this value has type [*con]u8, but the binding is declared i32
--> order.vyi:2:25
|
2 | con quantity: i32 = "three"
| ^^^^^^^
note: expected type declared here
--> order.vyi:2:19
|
2 | con quantity: i32 = "three"
| ^^^
error: this value has type i32, but the binding is declared bool
--> order.vyi:3:22
|
3 | con paid: bool = 1
| ^
note: expected type declared here
--> order.vyi:3:15
|
3 | con paid: bool = 1
| ^^^^
error: aborting due to 2 previous errorsvyi check --json prints the same errors as a JSON array, with a file path
and a range for each. op names the kind of error and stays the same from
release to release, so a tool can match on it. message is the text a person
reads. Spread over several lines, one entry looks like this:
[
{
"path": "typo.vyi",
"op": "undefinedName",
"severity": "error",
"message": "undefined name \"tru\"",
"range": {
"start": { "line": 2, "col": 16 },
"end": { "line": 2, "col": 19 }
}
}
]Editors
vyi-language-server speaks the Language Server Protocol over standard input
and output. It uses the same checks as vyi check, so the editor and the
command line report the same errors. Any editor with an LSP client can use
it: point the client at vyi-language-server for .vyi files. The install
script installs it; from a checkout, build it with Go:
cd bootstrap-language-server
go build -o vyi-language-server .Its --target flag (wasm, x86-64-linux, or arm64-linux) checks code for
that target, and --packages works as it does for vyi.
VS Code
The Vyi extension for Visual Studio Code is on the Visual Studio Marketplace
as Vyi.vyi:
code --install-extension Vyi.vyiThe extension highlights .vyi files on any system. Everything else comes
from vyi-language-server, which it looks for on your PATH; if it can't find
it, it asks you to install Vyi. The source is in the repository's
vscode-extension/ folder, and npm ci followed by npm run package there
builds a .vsix file you can install with code --install-extension.
The language server looks at your whole project, not only the open file. A
project is a folder with a package.vyipac, and one server handles every
project under the folders you open. It gives you:
- errors as you type, across every file of the project
- hover with a declaration's signature, its type, and its doc comment
- completion of fields, methods, and enum variants
- go to definition, including into the standard packages, find references, and rename
- quick fixes: fill in a struct literal's missing fields, add a
switch's missing arms, correct a misspelled name, add a missing import - refactors: extract a variable or a function, inline a variable
- signature help, inferred types shown inline, semantic highlighting, symbol search, and folding
A doc comment is a run of /// lines right above a declaration. Hover and
completion show it:
import { println } from "@fmt"
/// Returns the price with tax added, in cents.
fn withTax(cents: i32) i32 {
return cents + cents / 10
}
fn main() !u8 {
println(withTax(200)) // prints 220
return Ok(0)
}The extension's settings start with vyi.. Set vyi.languageServer.target
to the target you build for, so the editor checks the same code vyi does.
vyi.languageServer.path points at a vyi-language-server you built
yourself. The command Vyi: Restart Language Server(s) restarts the server.
The playground
The playground at vyilang.org/playground runs Vyi in the browser. Its editor gets errors, hover, and go to definition from the same language server. You can work with several files, pick a target and a build mode, and run the program. Tabs show the program's output, its syntax tree, and the generated assembly or WebAssembly text. Share copies a link to what you wrote.
The cheat sheet covers the language on three pages.
See also: the CLI reference, the install reference, and the targets reference.