Janet 1.4.0-655d4b3 Documentation
(Other Versions:
1.4.0
1.3.1)
Introduction
Installation
Install a stable version of janet from the releases page. 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
On Windows, the easiest way to install is via the scoop package manager. You will first need to add the janet-lang bucket, and then you can install janet normally.
scoop bucket add janet-lang https://github.com/janet-lang/scoop
scoop install janet
AUR
You can install either the latest git verion or the latest stable version for Arch Linux from the Arch user repositories with janet-lang-git or janet-lang.
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.
macOS and Unix-like
On most platforms, use Make to build janet. The resulting binary will be in build/janet
.
cd somewhere/my/projects/janet
make
make test
After building, run make install
to install the janet binary and libs.
Will install in /usr/local
by default, see the Makefile to customize.
FreeBSD
FreeBSD build instructions are the same as the unix-like build instuctions,
but you need gmake
and gcc
to compile.
cd somewhere/my/projects/janet
gmake CC=gcc
gmake test CC=gcc
Windows
- Install Visual Studio or Visual Studio Build Tools
- Run a Visual Studio Command Prompt (cl.exe and link.exe need to be on the PATH) and cd to the directory with janet.
- Run
build_win
to compile janet. - Run
build_win test
to make sure everything is working.
To install from source, the best way to is use NSIS to generate an installer and run that. You will need NSVC (or the Microsoft Build Tools) to build everything, and NSIS to build the installer. From a Developer Command Prompt, run build_win all
to build, test, and install Janet on your local machine.
Emscripten
To build janet for the web via Emscripten, make sure you
have emcc
installed and on your path. On a linux or macOS system, use make emscripten
to build
janet.js
and janet.wasm
- both are needed to run janet in a browser or in node.
The JavaScript build is what runs the repl on the main website,
but really serves mainly as a proof of concept. Janet will run slower in a browser.
Building with emscripten on windows is currently unsupported.
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!".
(print "Hello, world!")
Put the following code in a file named hello.janet
, and run janet hello.janet
.
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. This 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 brackets are interchangeable,
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"
.
Like all lisps, 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.
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.