Setting Up Gerbil Scheme Development Environment

We covered the basic installation of Gerbil Scheme for macOS and Linux in the Preface. In this chapter, we verify your installation with a minimal first program, explain the Gerbil compilation and package workflow, configure editor support in Emacs and Visual Studio Code, and review the essential language features that Gerbil adds to standard Scheme.

Verifying Your Installation with Hello World

Before writing complex applications, verify that your Gerbil environment is working properly. The repository includes a minimal first example in source_code/hello/hello.ss.

Here is the complete source code of hello.ss:

1 (def (hello who)
2   (displayln "hello " who))
3 
4 (hello 'Brady)

In standard Scheme, one writes define and calls both display and newline. Gerbil provides def as a concise shorthand and includes displayln in its standard library.

Interpreted Execution

You can run this program immediately using the Gerbil interpreter, gxi:

1 $ cd source_code/hello
2 $ gxi hello.ss
3 hello Brady

Interactive REPL Session

You can also load the file into an interactive REPL session:

1 $ gxi
2 Gerbil v0.18.1 on Gambit v4.9.5
3 > (import "hello")
4 > (hello 'World)
5 hello World
6 > ,q

Compiling to a Standalone Executable

Gerbil uses Gambit Scheme as its backend compiler. You can compile Scheme source directly to an optimized native binary using gxc:

1 $ gxc -exe -o hello hello.ss
2 $ ./hello
3 hello Brady

This compilation step translates Gerbil Scheme into Gambit intermediate code, compiles to C, and links into a standalone executable.

The Gerbil Workflow: Modules, Packages, and Build Tools

As your projects grow beyond single scripts, Gerbil organizes code into packages and modules.

Packages and gerbil.pkg

A Gerbil package is a directory containing a gerbil.pkg manifest. The manifest declares the package symbol name and any dependencies:

1 (package: myproject)

When a package defines (package: myproject), a source file named utils.ss inside that directory is addressed by other modules as :myproject/utils.

Build Scripts: build.ss and gerbil build

Standard packages include a build.ss script specifying the compilation targets:

1 #!/usr/bin/env gxi
2 (import :std/build-script)
3 
4 (defbuild-script
5   '("utils"
6     (exe: "main" bin: "mytool")))

Running gerbil build compiles library modules into compiled bytecode archives (.ssi interface files and .o1 object files) and builds any declared executables into bin/.

Where Compiled Modules Live: GERBIL_PATH

By default, gerbil build and gxc install compiled modules into ~/.gerbil/lib. During development, you can isolate project dependencies by pointing the GERBIL_PATH environment variable to a local directory:

1 export GERBIL_PATH=$(pwd)/.gerbil

When GERBIL_PATH is set, Gerbil searches for libraries and writes compilation artifacts within that local directory rather than altering your user-wide environment.

Editor Setup

Emacs Configuration

Emacs provides a mature development environment for Gerbil through gerbil-mode and Gambit integration.

Download gambit.el and gerbil-mode.el into ~/.emacs.d/:

Then add the following configuration to your ~/.emacs or ~/.emacs.d/init.el:

1 (load "~/.emacs.d/gambit.el")
2 (load "~/.emacs.d/gerbil-mode.el")
3 
4 (autoload 'gerbil-mode "gerbil-mode" "Gerbil editing mode." t)
5 (require 'gambit)
6 (add-hook 'inferior-scheme-mode-hook 'gambit-inferior-mode)
7 (defvar gerbil-program-name
8   (expand-file-name "/opt/gerbil/bin/gxi")) ; adjust to match which gxi
9 (setq scheme-program-name gerbil-program-name)

With this configuration, opening any .ss file activates gerbil-mode, providing syntax highlighting, automatic indentation, and direct expression evaluation with C-x C-e.

Visual Studio Code

If you prefer Visual Studio Code:

  1. Install the vscode-scheme or Gerbil extension from the VS Code Marketplace.
  2. In your VS Code settings, set the Scheme interpreter path to your gxi executable:
    1 {
    2   "scheme.interpreterPath": "/usr/local/bin/gxi"
    3 }
    
  3. Use the integrated terminal to run gxi REPL sessions and gerbil build tasks.

Gerbil for Schemers: Quick Language Primer

If you are already familiar with standard Scheme (R5RS or R7RS), Gerbil introduces several powerful syntactic extensions and library conveniences that appear throughout this book.

Concise Definitions with def

Gerbil uses def for definitions:

1 (def x 42)
2 (def (add a b) (+ a b))

Keyword and Optional Arguments

Functions support optional arguments with default values and keyword arguments:

1 (def (greet name (greeting "Hello") punctuation: (punct "!"))
2   (string-append greeting ", " name punct))
3 
4 (greet "Alice")                           ; => "Hello, Alice!"
5 (greet "Bob" "Hi")                        ; => "Hi, Bob!"
6 (greet "Carol" punctuation: "?")          ; => "Hello, Carol?"

Records and Data Structures with defstruct

Gerbil provides typed structs through defstruct:

1 (defstruct point (x y))
2 
3 (def p (make-point 10 20))
4 (point-x p) ; => 10
5 (point-y p) ; => 20
6 (point? p)  ; => #t

Pattern Matching with match

The :std/sugar module provides expressive pattern matching:

1 (import :std/sugar)
2 
3 (def (eval-expr expr)
4   (match expr
5     (['+ a b] (+ (eval-expr a) (eval-expr b)))
6     (['* a b] (* (eval-expr a) (eval-expr b)))
7     ((? number? n) n)
8     (else (error "Unknown expression" expr))))

Exception Handling with try and with-catch

Gerbil provides structured exception handling:

1 (try
2   (open-input-file "missing.txt")
3   (catch (e)
4     (displayln "Handled error: " (error-message e))))

Hash Tables

Hash tables are first-class with convenient constructors:

1 (def h (make-hash-table))
2 (hash-put! h 'key "value")
3 (hash-ref h 'key)          ; => "value"
4 (hash-ref h 'missing "default") ; => "default"

Iteration with for

The :std/iter module provides comprehension and looping constructs:

1 (import :std/iter)
2 
3 (for (x '(1 2 3 4 5))
4   (displayln (* x x)))

With these fundamentals established, you are ready to begin building applications with Gerbil Scheme.