Janet 1.32.1-cc5beda Documentation
(Other Versions: 1.31.0 1.29.1 1.28.0 1.27.0 1.26.0 1.25.1 1.24.0 1.23.0 1.22.0 1.21.0 1.20.0 1.19.0 1.18.1 1.17.1 1.16.1 1.15.0 1.13.1 1.12.2 1.11.1 1.10.1 1.9.1 1.8.1 1.7.0 1.6.0 1.5.1 1.5.0 1.4.0 1.3.1 )

Introduction

Installation

Janet is prebuilt for a few systems, but if you want to develop Janet, run Janet on a non-x86 system, or get the latest, you must build Janet from source.

Windows

The recommended way to install on Windows is just to run the installer from the releases page. A Scoop package was maintained previously, but ongoing support is being dropped.

If you want to use jpm to install native extensions, you will also need to install Visual Studio, ideally a recent release (2019 or 2017 should work well). Then, running jpm from the x64 Native Tools Command Prompt should work for a 64-bit build, and from the Developer Command Prompt should work for 32-bit builds. Using these specific command prompts for jpm simply lets jpm use the Microsoft compiler to compile native modules on install.

Linux

Arch Linux

You can install either the latest git version or the latest stable version for Arch Linux from the Arch user repositories with janet-lang-git or janet-lang.

Other Linux

See instructions below for compiling and installing from source.

macOS

Janet is available for installation through the homebrew package manager as janet. The latest version from git can be installed by adding the --HEAD flag.

brew install janet

Compiling and running from source

If you would like the latest version of Janet, are trying to run Janet on a platform that is not macOS or Windows, or would like to help develop Janet, you can build Janet from source. Janet only uses Make and batch files to compile on POSIX and Windows respectively. To configure Janet, edit the header file src/conf/janetconf.h before compilation.

Non-root install (macOS and Unix-like)

To build and install Janet (and jpm) to a local or temporary directory tree instead of the default prefix of /usr/local, set the PREFIX environment variable while building and installing. You will like likely also want to update your path to point to PREFIX/bin to use janet, jpm, and any scripts installed via jpm.

export PREFIX="$HOME"/.local
make -j
make install
make install-jpm-git # you can also do this manually by reading the janet-lang/jpm.git readme
export PATH="$PREFIX"/bin:"$PATH"

macOS and Unix-like

On most platforms, use Make to build Janet. The resulting files (including the janet binary) will be in the build/ directory.

cd somewhere/my/projects/janet
make
make test

After building, run make install to install the janet binary and libraries. This will install in /usr/local by default, see the Makefile to customize.

Next, to install the Janet Project Manager tool (jpm), clone the jpm repository, and from within that directory, run:

[sudo] janet bootstrap.janet

FreeBSD

FreeBSD build instructions are the same as the Unix-like build instructions, but you need gmake and gcc to compile.

cd somewhere/my/projects/janet
gmake CC=gcc
gmake test CC=gcc

Windows

  1. Install Visual Studio or Visual Studio Build Tools
  2. Run a Visual Studio Command Prompt (cl.exe and link.exe need to be on the PATH)
  3. cd to the directory with Janet
  4. Run build_win to compile Janet.
  5. Run build_win test to make sure everything is working.

To install from source, first follow the steps above, then you will need to

  1. Install, or otherwise add to your PATH, the WiX 3.11 Toolset
  2. Run a Visual Studio Command Prompt (cl.exe and link.exe need to be on the PATH)
  3. cd to the directory with Janet
  4. Run build_win dist
  5. Then, lastly, execute the resulting .msi executable

Meson

Janet also has a build file for Meson, a cross-platform build system. This is not currently the main supported build system, but should work on any system that supports Meson. Meson also provides much better IDE integration than Make or batch files.

Small builds

If you want to cut down on the size of the final Janet binary or library, you need to omit features and build with -Os. With Meson, this can look something like below:

git clone https://github.com/janet-lang/janet.git
cd janet
meson setup SmallBuild
cd SmallBuild
meson configure -Dsingle_threaded=true -Dassembler=false -Ddocstrings=false \
    -Dreduced_os=true -Dtyped_array=false -Dsourcemaps=false -Dpeg=false \
    -Dint_types=false --optimization=s -Ddebug=false
ninja
# ./janet should be about 40% smaller than the default build as of 2019-10-13

You can also do this with the Makefile by editing CFLAGS, and uncommenting some lines in src/conf/janetconf.h.

First program

Following tradition, a simple Janet program will print "Hello, World!".

Put the following code in a file named hello.janet, and run janet hello.janet:

(print "Hello, World!")

The words "Hello, World!" should be printed to the console, and then the program should immediately exit. You now have a working Janet program!

Alternatively, run the program janet without any arguments to enter a REPL, or read-eval-print-loop. This is a mode where Janet works like a calculator, reading some input from the user, evaluating it, and printing out the result, all in an infinite loop. This is a useful mode for exploring or prototyping in Janet.

This hello world program is about the simplest program one can write, and consists of only a few pieces of syntax. The first element is the print symbol. This is a function that simply prints its arguments to the console. The second argument is the string literal "Hello, World!", which is the one and only argument to the print function. Lastly, the print symbol and the string literal are wrapped in parentheses, forming a tuple. In Janet, parentheses and also brackets form a tuple; brackets are used mostly when the resulting tuple is not a function call. The tuple above indicates that the function print is to be called with one argument, "Hello, World".

All operations in Janet are in prefix notation: the name of the operator is the first value in the tuple, and the arguments passed to it are in the rest of the tuple. While this may be familiar in function calls, in Janet this idea extends to arithmetic and all control forms.

Second program

If you create a "main" function, janet will automatically call that for you when you run your script. Here's an example that prints out the command line args tuple passed to your program, its length, and a greeting to the whomever's named first on the command line:

#!/usr/bin/env janet

(defn print-greeting
  [greetee]
  (print "Hello, " greetee "!"))

(defn main
  [& args]
  (pp args) # pretty-prints the args tuple
  (print (length args))
  (print-greeting (get args 1)))

Output:

$ ./foo.janet World Famous Fries
("./foo.janet" "World" "Famous" "Fries")
4
Hello, World!

The core library

Janet has a built in core library of over 300 functions and macros at the time of writing. For efficient programming in Janet, it is good to be able to make use of many of these functions.

If at any time you want to see short documentation on a binding, use the doc macro to view the documentation for it in the REPL.

(doc defn) # -> Prints the documentation for "defn"

To see a list of all global functions in the REPL, type the command

(all-bindings)

Which will print out every global binding in the Janet REPL. You can also browse the core library API on the website.