gsh
gsh is the core entry point for the Interactive Gleam Shell.
It acts as a development orchestrator, providing three main capabilities:
- Zero-Config Bootloader: Intercepts CLI arguments to dynamically boot host
applications in the background (e.g.,
gleam run -m gsh -- my_app). - Hot Code Swapping: Provides a
compilecommand to manually rebuild the host project and trigger Erlangcode:purgeandcode:load_file, hot-swapping live module updates without restarting the shell. - Persistent REPL: A live-node interactive shell that maintains VM state,
memoizes side effects, and safely handles runtime exceptions while toggling
terminal raw mode to ensure clean I/O. It supports standard expression evaluation,
lexical variable shadowing, and convenient top-level module syntax (
fn,type) for rapid prototyping.
Types
Holds the persistent state of the shell session across evaluations. This state is passed recursively through the REPL loop to seamlessly inject historical context into each dynamically generated module.
pub type ShellState {
ShellState(
prompt_count: Int,
bindings: List(binding.Binding),
imports: List(String),
types: List(#(String, String)),
history: List(String),
functions: List(#(String, String)),
debug: Bool,
)
}
Constructors
-
ShellState( prompt_count: Int, bindings: List(binding.Binding), imports: List(String), types: List(#(String, String)), history: List(String), functions: List(#(String, String)), debug: Bool, )Arguments
- prompt_count
-
Increments on every REPL execution to guarantee uniquely named Erlang modules (e.g.,
gsh_eval_1,gsh_eval_2), preventing VM cache collisions. - bindings
-
Active
letbindings. The shell intelligently drops older bindings only when all of their extracted variables have been fully shadowed. - imports
-
Active
importstatements. Checked sequentially to discard duplicates. - types
-
Custom
typedeclarations. Stored as a tuple of#(Type_Name, Source_String). Redefining a type automatically prunes the old source to prevent compiler crashes. - history
-
The raw input strings of previously executed commands, used by the raw-mode editor for Arrow Up/Arrow Down traversal.
- functions
-
Top-level
fndefinitions. Stored as a tuple of#(Function_Name, Source_String). When redefined, the old source string is strictly purged from the active state to satisfy the Gleam compiler’s unique-name constraints. - debug
-
Toggles verbose output for debugging the internal AST parsing and evaluation pipeline.
Values
pub fn main() -> Nil
The main entry point.
- Cleans up any orphaned evaluation files from previous crashed sessions.
- Intercepts trailing CLI arguments to boot background host applications.
- Injects a custom logger to prevent staircasing in background logs.
- Places the terminal into raw mode and starts the recursive REPL loop.