Previous: , Up: Scheme reference   [Contents][Index]


6.17 REPL

The (hoot repl) module provides a procedural API for read-eval-print loops. Additionally, it comes with a built-in driver for a purely text-based REPL interface. New drivers can be created by integrating the procedural API with the desired interface, such as the web’s document object model.

REPLs contain a set of meta-commands and a stack of environments that can be pushed and popped.

Procedure: make-repl [#:environment] [#:meta-commands]

Return a fresh REPL for environment with meta-commands.

Procedure: repl? obj

Return #t if obj is a REPL.

Procedure: repl-environment repl

Return the current environment for repl.

Procedure: repl-stack repl

Return the stack of previous environments for repl.

Procedure: repl-depth

Return the current depth of repl.

Procedure: repl-welcome repl

Return the welcome message for the current environment in repl.

Procedure: repl-prompt repl

Return the current prompt message for repl.

Procedure: repl-meta-commands repl

Return a list of meta-commands associated with repl.

Procedure: repl-meta-command-ref repl

Return the meta-command in repl associated with name, or #f if there is no such command.

Procedure: repl-add-meta-command! repl cmd

Add meta-command cmd to repl.

Procedure: repl-push! repl env

Enter a sub-REPL within repl using env, saving the current environment on the environment stack to be restored later.

Procedure: repl-pop! repl

Leave the current environment in repl, restoring the previous environment if there is one. Otherwise, raise an exception.

Procedure: repl-read repl port

Read an expression from port using the current reader for repl. The returned expression may be a meta-command invocation.

Procedure: repl-eval repl exp

Eval exp in the current environment of repl.

Procedure: repl-eval-with-error-handling repl exp

Eval exp, either a language or meta-command expression, in the current environment of repl. If an exception is thrown while evaluating a language expression, a debugging sub-REPL will be entered.

Procedure: repl-meta-eval repl cmd [args]

Invoke the meta-command cmd in repl with args.

Parameter: current-repl

Parameter containing the current REPL being used for evaluation. Can be used in evaluated code to manipulate the host REPL.

The procedures below provide a classic textual REPL driver.

Procedure: run-repl repl [#:reader]

Run repl using reader to prompt the user and read input. A default reader is used if none is specified.

Reader procedures take a REPL object as their only argument. Readers should display the current prompt and parse an input expression with the repl-read procedure.

Procedure: spawn-repl [#:environment] [#:meta-commands] [#:reader]

Create and run a REPL for environment with meta-commands.

Environments are the context in which evaluation occurs. They contain a module (see Modules) and a language for parsing and evaluating REPL input.

Procedure: make-repl-environment

Return a fresh REPL environment for evaluating language in module. data can be any arbitrary object to associate with this environment, perhaps including debug information. welcome is a procedure that displays a welcome message when the enviroment is entered. prompt is a procedure that returns an input prompt to display to the user. Both welcome and prompt take just one argument: a REPL object.

Procedure: repl-environment? obj

Return #t if obj is a REPL environment.

Procedure: repl-environment-language env

Return the language for env.

Procedure: repl-environment-module env

Return the module for env.

Procedure: repl-environment-data env

Return the custom data for env.

Procedure: repl-environment-welcome env

Return the welcome procedure for env.

Procedure: repl-environment-prompt env

Return the prompt procedure for env.

Languages encapsulate a title string, a reader procedure, and an evaluator procedure. So, in theory, the Hoot interpreter could host many languages, like the Guile VM does. In practice, at least for now, the language is always Scheme.

Procedure: make-repl-language [#:title] [#:reader] [#:evaluator]

Return a new language named title that parses input text with reader and evaluates expressions with evaluator. reader is a procedure of one argument: an input port. evaluator is a procedure of two arguments: an expression to evaluate and a module in which to perform the evaluation.

Procedure: repl-language? obj

Return #t if obj is a REPL language.

Procedure: repl-language-title lang

Return the title of lang.

Procedure: repl-language-reader lang

Return the read procedure for lang.

Procedure: repl-language-evaluator lang

Return the evaluation procedure for lang.

Meta-commands are special REPL tools that can be invoked using a comma at the beginning of the expression, such as ,help. New meta-commands can be defined using define-meta-command.

Syntax: define-meta-command (name repl args ...) kwargs ... body ...

Define a meta-command named name with an associated procedure containing body that accepts repl and additionally any other args. The arguments list may include optional and keyword arguments as in define*.

Additionally, there are several meta-command properties that may be specified via kwargs:

#:name

The symbol used to invoke this meta-command at the REPL prompt.

#:aliases

A list of symbols, each of which is an alternative name for this meta-command.

#:group

The name of the meta-command group as a symbol. Related meta-commands should belong to the same group to ease user discovery.

#:usage

A string that describes how to invoke this meta-command. A meta-command with a required argument foo and an optional argument bar might have the usage string "FOO [BAR]".

#:summary

A brief documentation string.

#:documentation

A detailed documentation string.

#:reader

A procedure that reads meta-command arguments. It takes two arguments: a repl and a port. This is an advanced configuration option for meta-commands that need special argument parsing logic. The default reader is usually sufficient.

This macro is best explained by example:

(define-meta-command (meta:say repl #:optional message)
  ;; Keyword arguments:
  #:name 'say
  #:aliases '(s)
  #:group 'example
  #:usage "[MESSAGE]"
  #:summary "Say something."
  #:documentation "Say a few words, or maybe don't say anything at all."
  ;; Procedure body:
  (if message
      (format #t "You said: ~a\n" message)
      (display "You didn't say anything.")))

Once added to a REPL with (repl-add-meta-command! repl meta:say), it can be invoked with ,say or its alias ,s:

(hoot user)> ,say "Hello!"
You said: Hello!
(hoot user)> ,s
You didn't say anything.
Procedure: meta-command? obj

Return #t if obj is a meta-command.

Procedure: meta-command-name cmd

Return the name of cmd.

Procedure: meta-command-aliases cmd

Return the aliases of cmd.

Procedure: meta-command-usage cmd

Return the documentation for cmd

Procedure: meta-command-summary cmd

Return the documentation for cmd

Procedure: meta-command-documentation cmd

Return the documentation for cmd

Procedure: meta-command-reader cmd

Return the procedure used to read arguments for cmd.

Procedure: meta-command-procedure cmd

Return the procedure used to perform cmd.


Previous: Modules, Up: Scheme reference   [Contents][Index]