Skip to content
bizzehdee edited this page Jun 23, 2026 · 4 revisions

REPL

The DScript.Repl project is an interactive read-eval-print loop backed by a persistent ScriptEngine. Variables, functions, and classes you declare in one line are available in all subsequent lines.

Running

dotnet run --project DScript.Repl

Or build and run the binary directly:

dotnet build DScript.Repl --configuration Release
./DScript.Repl/bin/Release/net10.0/DScript.Repl

Session example

DScript REPL  (type .exit to quit, .help for commands)

> var x = 10
> x + 5
15
> function square(n) { return n * n; }
> square(7)
49
> var nums = [1, 2, 3, 4, 5]
> nums.map(n => n * 2)
[ 2, 4, 6, 8, 10 ]
> .exit
bye.

How input is handled

The REPL distinguishes between statements and expressions:

  • Lines that begin with var, let, const, function, or class, or that end with ;, are treated as statements and executed with engine.Execute(). No value is printed.
  • Everything else is treated as an expression, evaluated with engine.EvalComplex(), and its result printed (unless the result is undefined).

This means you can omit the trailing semicolon when typing expressions you want to see the value of.

Built-in commands

Command Description
.help Show the list of REPL commands
.exit or .quit End the session
Ctrl-C or Ctrl-D End the session (EOF)

Built-in functions

The REPL registers a print(arg) global in addition to any standard library. It converts the argument to a display string using GetParsableString() — useful when the extras library is not loaded.

Persistent state

All globals survive across inputs within a single session. You can build up a script incrementally:

> function fib(n) { return n <= 1 ? n : fib(n-1) + fib(n-2); }
> fib(10)
55
> var result = fib(15)
> result
610

Error handling

Errors are caught and printed to stderr without ending the session:

> undeclaredVariable
Error: 'undeclaredVariable' is not defined
> 1 / 0
Infinity

Extending the REPL

The REPL source (DScript.Repl/Program.cs) is intentionally minimal. To add the full DScript.Extras standard library, register it before the loop:

using DScript.Extras;

var engine = new ScriptEngine();
new EngineFunctionLoader().RegisterFunctions(engine);
// ... rest of REPL loop

To pre-load a script file, compile and run it on the engine before entering the loop:

engine.Run(ScriptEngine.Compile(File.ReadAllText("prelude.ds")));

Clone this wiki locally