REPL - a more easily configurable REPL
use REPL;
# Start a Read, Evaluate, Print Loop
repl;
The REPL module is a re-imagining of the REPL (Read, Evaluate, Print Loop) functionality as offered by Rakudo before the 2024.11 release. It provides both a programmable interface, as well as a ready made "repl"
CLI.
# Simplest invocation
$ repl
# Invocation with some custom settings
$ repl --the-prompt='[:index:] :HHMM:' --symbols=🦋,🔥
[0] 20:51 🦋 if 42 {
[0] 20:51 🔥 say "foo"
[0] 20:51 🔥 }
foo
[0] 20:52 🦋
The REPL command-line interface can be invoked with named arguments that have the same name as the named arguments to the REPL.new
call. They are:
-
--editor - the editor to use (default: Any)
-
--header - whether to show the full header (default: yes)
-
--multi-line-ok - can expression span more than one line (default: yes)
-
--output-method - the output method to be used (default: gist)
-
--symbols - symbols for prompt state (default: ">,*")
-
--the-prompt - string for prompt (default: "[:index:] :symbol: ")
Any other command-line arguments will be ignored.
repl;
The repl
subroutine creates a new REPL
object for that context and returns that. When called in sink context, it will activate the interactive Read, Evaluate, Print Loop. This is the most common usage when debugging your code.
#line 666 foo.raku
if 42 -> $answer {
repl "Looking for clues", :$answer;
}
Looking for clues
answer: 42
[0] >
Note that when the repl
subroutine is used in a debugging mode, it will not show any header, other than the one that is specified with the positional argument (in this case "Looking for clues").
If no explicit heafer is specified, it will show the location of the callframe
that the code is currently in (in this case that would have been: line 666 in the file "foo.raku").
Finally it will show the value of any named arguments to help you debug your code (in this case "answer: 32").
It is also possible to save the REPL
object at one place in the code, and actually run the REPL at a later time in another scope.
If you just press ENTER when using the REPL as a debugging aid in this manner, then it will be interpreted the same as "=quit" for convenience (especially if the repl
sub is called in a loop).
my $repl = do {
my $a = 42;
repl
}
# ...
# activate REPL later, allowing access to $a even though that
# variable is no longer in scope
$repl.run;
The standard-completions
subroutine provides the standard completion logic if no :additional-completions
argument has been specified with either REPL.new
or the repl
subroutine.
It is provided to allow it to be added with the <:additional-completions> argument, and to allow testing of the standard completion logic.
The uniname-words
subroutine provides the same functionality as the uniname-words
subroutine provided by the uniname-words
distribution if that distribution is installed.
Otherwise it will always return Nil
.
The REPL
role is what usually gets punned into a class.
my $repl = REPL.new;
$repl.run;
Same as above, but with all named arguments spelled out:
my $repl = REPL.new:
:editor(Any), # or "Readline", "LineEditor", "Linenoise"
:output-method<gist>, # or "Str", "raku"
:out = $*OUT,
:err = $*ERR,
:val = $*OUT,
:header,
:multi-line-ok,
:the-prompt("[:index:] :symbol: "),
:symbols(">", "*"),
:is-win($*DISTRO.is-win),
:@completions,
:@additional-completions,
:compiler<Raku>,
;
The REPL
role embodies the information needed to run a Read, Evaluate, Print Loop. It allows for these named arguments:
Optional. String indicating which editor logic to be used.
If the INSIDE_EMACS
environment variable is set with a true value, then the Fallback
editor will be used.
If it can be determined from the environment that the process is running inside the "rlwrap" wrapper, then the Fallback
editor will be used.
If the RAKUDO_LINE_EDITOR
environment variable is set, then its contents will be assumed as an indication of preference and will first be tried. If that fails, an error message will be shown.
Whatever is then the value, that value will be used to create a Prompt
object.
String. The name of the method to be called to display the value of an expression. This defaults to the value of the RAKU_REPL_OUTPUT_METHOD
environment variable, or to "gist" if that has not been specified.
Used value available with the .output-method
method.
Boolean. Indicate whether to show the REPL header upon entry. Defaults to True
.
Used value available with the .header
method.
Optional. The :out
named argument specifies the value of $*OUT
whenever a command is executed. If not specified, or specified with an undefined value, will assume the value of $*OUT
at command execution time.
Optional. The :err
named argument specifies the value of $*ERR
whenever a command is executed. If not specified, or specified with an undefined value, will assume the value of $*ERR
at command execution time.
Optional, Boolean. Indicate whether it is ok to interprete multiple lines of input as a single statement to evaluate. Defaults to True
unless the RAKUDO_DISABLE_MULTILINE
environment variable has been specified with a true value.
Used value available with the .multi-line-ok
method.
Optional. Specifies what will be shown to the user before the user can enter characters. Defaults to what has been specified with the RAKUDO_REPL_PROMPT
environment variable, or "[:index:] :symbol: "
.
Supports all of the expansions offered by the Prompt::Expand
distribution.
Note that if there is a prompt (implicitely) specified, the string ":symbol: " will be added if there is no ":symbol:" specified, to make sure the user actually sees a prompting symbol, and to make specifying a user prompt a bit easier.
The expanded prompt is also available with the .the-prompt
method.
Optional. Specifies the symbols that should be used for the ":symbol:"
placeholder in the different REPL states. Defaults to what has been specified as a comma-separated list with the RAKUDO_REPL_SYMBOLS
environment variable. Defaults to ">", "*"
if that is not specified.
Currently the following states are recognized:
-
0 - accepting expression to be evaluated
-
1 - previous expression not complete, accepting continuation
Boolean. Indicate whether certain OS dependent checks should assume Windows semantics. Defaults to $*DISTRO.is-win
.
Used value available with the .is-win
method.
A List
of strings to be used tab-completions. If none are specified, then a default Raku set of completions will be used.
Used value available with the .completions
method.
# completion that uppercases whole line if ended with a !
sub shout($line, $pos) {
($line.chop.uc,) if $pos == $line.chars && $line.ends-with("!")
}
my $repl = REPL.new(:additional-completions(&shout));
A List
of Callables
to be called to produce tab-completions. If none are specified, the standard-completions
will be assumed.
Each Callable
is expected to accept two positional arguments: the line that has been entered so far, and the position of the cursor. It is expected to return a (potentially) empty List
with the new state of the line (so including everything before and after the completion).
The Prompt
object to be used. If specified, overrides anything related to the <C:editor> named argument. If not specified, will use whatever was (implicitely) specified with :editor
.
String. The HLL compiler to be used. This defaults to "Raku", which is the only compiler supported at this time.
Used value available with the .compiler
method.
Actually run the REPL.
The object to be used for error output. Defaults to $*ERR
.
Expected to take no arguments, and return an object that represents the (possibly persistent) history of the REPL's interactive sessions.
By default it will first look for a RAKUDO_HIST
environment variable and return an IO::Path
object for that. If that environment variable is not specified, will check the $*HOME
and $*TMPDIR
dynamic variables for the existence of a .raku
subdirectory in that. If found, will return an IO::Path
for the "rakudo-history" file in that subdirectory and try to create that if it didn't exist yet (and produce an error message if that failed).
The object to be used for standard output. Defaults to $*OUT
.
The Prompt
object to be used when obtaining input from the user. Also handles the read
, readline
, load-history
, add-history
, save-history
and editor-name
methods.
Returns a Bool
indicating whether the selected editor supports completions.
Expected to take no arguments. Will be called whenever the user indicates that they want to exit the REPL. Will call the save-history
method by default.
The object to be used for outputting values that weren't shown already. Defaults to $*OUT
.
The following REPL commands are currently supported: if a command is not recognized, then Raku
code will be assumed and executed if possible. Note that all REPL commands start with "=
" to prevent confusion with possibly legal Raku code.
Shows an introduction to completions. Can be shortened to "=com".
Allows creation of and switching between two or more contexts: "new" creates a new context and switches to it, "switch" switches to an already existing context, and "list" shows the available contexts. Can be shortened to "=con".
Edit the file given, or the last file that was saved with =write. Can be shortened to "=ed".
Leaves the REPL. Can be shortened all the way to "=ex".
Shows a list of available commands. Can be shortened all the way to "=h".
Shows the name of the editor logic being used. Can be shortened to to "=inf".
Shows a general introduction. Can be shortened to "=int".
Shows the current output method. If a second argument is specified, it indicates the name of the output method to be used from now on. Typical values are "raku", "Str", "gist". Can be shortened all the way to "=o".
Leaves the REPL. Can be shortened all the way to "=q", and is thus the shortest way to leave the REPL with a REPL command.
Read the code from the file with the indicated path and compiles and executes it. Remembers the path name so that subsequent =write commands need not have it specified.
Reset the status of the REPL as if it was freshly entered.
Show the caller stack from where the REPL has been called. Only makes sense if the REPL is being called from within a program, rather than from the command line. Can be shortened to "=s".
Write all lines entered that did not produce any output to the indicated path. Remembers the path name from =read and =write so that subsequent =write commands need not have it specified.
If the supports-completions
method returns True
, the standard tab-completion logic will provide:
-
all relevant items from the CORE:: namespace
-
any relevant items from the direct context (such as a REPL command)
-
\123 wil change integer value to superscript: ¹²³, subscript: ₁₂₃
-
foo! will tab-complete to FOO, foo, Foo
Additionally, if the uniname-words
module is installed:
-
any unclosed [**\c** sequence will tab-complete on the names of Unicode code-points
-
any \word will tab-complete to the actual codepoints
These environment variables will override default settings.
The prompt shown to the user. May contain escape sequences as supported by the Prompt.expand
method.
A comma separated list of symbols representing the states of the REPL. Defaults to >,*>
.
Path where the history file is / should be stored.
The name of the method with which to show results to the user.
Whether multi-line evaluations should be disabled or not.
Elizabeth Mattijsen liz@raku.rocks
Source can be located at: https://github.com/lizmat/REPL . Comments and Pull Requests are welcome.
If you like this module, or what I'm doing more generally, committing to a small sponsorship would mean a great deal to me!
Copyright 2024, 2025 Elizabeth Mattijsen
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.