07 REPL

The REPL reads an entry, evaluates each of its top-level forms on its own, and prints each result as one canonical DUST form. A list keeps its brackets, so [1 2 (+ 3 4)] prints [1 2 7]. It keeps history and highlights input as you type. A form that spans lines continues until its brackets balance.

> (+ 1 2)
3  eval 57.9µs cpu · 130.5KiB peak · +4.6KiB net · 323.5KiB live
> (+ 1 2)
3  eval 17.3µs cpu · 128.5KiB peak · +2.5KiB net · 326.0KiB live

A def form defines a top-level local that later entries read:

> (def rate 3)
rate  eval 10.6µs cpu · 5.7KiB peak · +3.3KiB net · 580.8KiB live
> [rate (* rate 2)]
[3 6]  eval 8.1µs cpu · 8.4KiB peak · +5.5KiB net · 586.3KiB live

7.1. Importing files

An import in the REPL names a .dust file relative to the working directory, which is also the import root. The REPL reads no file outside it and the imports roots of its configuration. With a database (With a database), the newer of the file and a saved namespace of the path is used. The file's export forms name the members that the alias makes available, and its other locals stay private. Each evaluation of the import form reads the file again, so an edit takes effect (Files). An alias cannot be a builtin namespace such as math: use (import {m "lib/math"}). A line that starts with ! is a REPL command. Every other line is DUST.

Given lib/shapes.dust:

(def double (fn [x] (+ x x)))
(def square (fn [x] (* x x)))
(export square double)
> (import {shapes "lib/shapes"})
null  eval 132.2µs cpu · 76.6KiB peak · +71.6KiB net · 1.5MiB live
> (shapes/square 7)
49  eval 16.6µs cpu · 5.5KiB peak · +5.5KiB net · 1.6MiB live
> (import {s "lib/shapes"})
null  eval 8.9µs cpu · 2.6KiB peak · +2.6KiB net · 1.6MiB live
> (s/double 4.5)
9.0  eval 15.0µs cpu · 5.2KiB peak · +5.2KiB net · 1.6MiB live

!reset clears top-level locals, aliases and imported files. The new session contains only the core libraries:

> !reset
reset: locals, namespaces, and compiled programs cleared
> (shapes/square 7)
#err {<name unbound>
      message "unbound name: shapes/square"}  eval 25.4µs cpu · 9.2KiB peak · -208.4KiB net · 1.3MiB live

7.2. With a database

When the --config file has database storage, a path or temporary true (Configuration files), the REPL opens that database, as eval does, and evaluates each form in one database session. Named queries and mutations work, and a database file keeps its data after the REPL stops:

{plugins {stdlib true database {path "app.db"}}}
$ stardust repl --config app.dust
> (defmutation seed [] {patch {_u {email "a@x"}}})
seed  eval 301.4µs
> (seed)
{ids {_u #link 1}
 tx 3}  eval 3.6ms
> (defquery q [] {<tuples> find [?e] where [[?e email ?m]]})
q  eval 424.8µs
> (q)
[[1]]  eval 842.2µs

Each result shows the time of its request, measured by the REPL; the database reports no compile or memory cost. The database's limits apply. !reset starts the database session again: it clears top-level locals and imported files, and the data stays. An imported file loads as an unsaved namespace of the session and saves nothing (Files). Without storage settings, the REPL evaluates without a database, as described above.