Skip to content

Macros RFC: a read-eval-print loop and completion on the page - #209

Open
jfrolich wants to merge 11 commits into
comptime-macrosfrom
macros-repl
Open

jfrolich wants to merge 11 commits into
comptime-macrosfrom
macros-repl

Conversation

@jfrolich

@jfrolich jfrolich commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Adds a read-eval-print loop to the RFC page of the compile-time macros, and completion to every editor of the site (stacked on #90).

What it is

A session in the first section of the page, "Try it", with a link "REPL" in the header. Each input is compiled as a file of its own with the earlier inputs opened, and run once, so names, types, modules and macros of one input are there for the next. it is the value of the last expression. The macro modules of the examples and the whole standard library are loaded.

> let price = 12.5 and quantity = 3
let price: float = 12.5
let quantity: int = 3

> %Debug.log(price *. Int.toFloat(quantity))
price *. Int.toFloat(quantity) = 37.5
- : float = 37.5

> type user = {name: string, age: int, email?: string}
type user = {name: string, age: int, email?: string}

> Json.encode({name: "Ada", age: 36})
- : JSON.t =
  Object(dict{"name": String("Ada"), "age": Number(36.), "email": Null})
  • Enter runs an input, or continues one whose brackets are open; Shift+Enter is a new line; the arrows walk the earlier inputs.
  • Every answer can show the expansion and the JavaScript of its input.
  • Values are printed as the ReScript that makes them (Some(None), Blue(3), list{1, 2}, records with optional fields, unboxed variants), not as JavaScript.
  • A macro written in a module in one input can be used in the next one.
  • A top-level await works, an open stays open, output of timers appears when it is printed.
  • An input that does not end is stopped after 20 seconds; the session then starts again.

Completion

The input of the loop, the editors of the examples and the playground's editor offer what can be written, as the compiler sees it:

  • after Json. the members of the module, with their types; after %Debug. and @Enum. its macros;
  • after user. the fields of the record, with their types;
  • after users-> the functions that take an array first (Array.map), and functions in scope that take the type;
  • for a name, what is in scope: earlier inputs or the file up to the item being written, opened modules, the standard library.

The compiler has no completion of its own. playground/src/complete.js compiles small files: include Path for the members of a module, and the expression before a . or -> with a type it cannot have, in its place, for its type. That way a function parameter completes like a top-level name (users->Array.map(u => u. offers the fields of user). No change to the compiler was needed.

Changes

Commit What
macros: a macro runs from the file its name leads to The engine was asked for the first part of the macro's name. That is wrong through an alias (module D = Debug, %D.dbg(x)) and for a module of an opened file.
compiler: what a read-eval-print loop needs of a compilation repl::prepare binds a last expression to it and lists the opens. With capture_intermediates, a compilation also describes what the file defines: each item as an interface declares it, and the run-time shape of each value's type.
playground: a read-eval-print loop in the engine engine.replStart / engine.replEval, compile_input and prepare_input in the wasm crate, and the value printer.
site: a read-eval-print loop on the RFC page The component, its worker, and the opening session, which is built with the site.
docs: the session of the RFC, checked against the loop The RFC shows the opening session as text; npm run smoke fails when it is not what the loop prints.
site: the read-eval-print loop is the first section of the RFC The loop moves from the tour to a section "Try it" before "Why"; a header link and a chip under the title go there.
site: a query against the schema among the inputs the loop offers %Sql.query of the database example, in a session.
playground: completion, asked of the compiler src/complete.js, engine.complete, and the checks in npm run smoke.
site: completion in the loop, the examples and the playground CodeMirror and Monaco ask the worker; in the loop Enter and the arrows choose from the list when it is open.
docs: completion in the RFC and the playground's README

Checked

  • cargo test --test repl --test macros --test macro_errors and the repl:: unit tests of compiler-rust.
  • npm run smoke in playground (all examples, the session, and the transcript in the RFC).
  • Completion in Chrome on the deployed page: a pipe, a module member, fields of a top-level and of a local name, macros after %, in the loop, in an example (main file and macro file) and in the playground.
  • In Chrome at 1440 and 390 px wide, light and dark: the opening session, typed inputs, the offered inputs, a type error, the expansion of an input, history with the arrows, a continued input, late output of a timer, and an endless loop that is stopped.

Not in this change: completion of labelled arguments, of the fields of an inline record, and of names inside a string or a quote; JSX in an input (the loop has no JSX configuration), and Belt (not part of the page's standard library bundle).

Started by Jaap Frolich in this Assistant session

@jfrolich jfrolich changed the title Macros RFC: a read-eval-print loop on the page Macros RFC: a read-eval-print loop and completion on the page Nov 12, 2026
open-session-submodule Bot and others added 11 commits November 13, 2026 10:34
Why:
    The engine was asked for the module that is the first part of the
    name at the use. That is not the file of the macro when the name goes
    through an alias (`module D = Debug`, then `%D.dbg(x)`) or names a
    module of an opened file: the macro was reported as needing a module
    that is not available at compile time.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
A loop compiles each input as a file of its own with the earlier inputs
opened (`CompilerOptions::open_modules`), and runs its JavaScript once.
The module `repl` adds the two things a compilation did not give:

- `repl::prepare` binds a last item that is an expression to `it`, and
  lists the top-level opens, which the loop keeps open.
- With `capture_intermediates`, the result has
  `signature_description`: every item the file defines as an interface
  declares it, and for a value the run-time shape of its type. A
  JavaScript value does not say what it is in ReScript (`Some(3)` is
  `3`, a constructor with a payload an object tagged `TAG`); the shape
  does.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
`engine.replStart` and `engine.replEval` keep a session: an input is
the file `Repl<n>.res`, compiled by the new `compile_input` with the
files of the earlier inputs opened, and run once. What it defined is
printed with its type and its value, written as the ReScript that makes
it (`src/repl-format.js`, by the shape the compiler gives). An input can
await at the top level, an open stays open, a diagnostic is shown for the
input as it was written, and an input that raises defines nothing.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
The block between `<!-- repl -->` and `<!-- /repl -->` of the RFC is a
session: Enter runs an input (or continues one that is not finished),
the arrows walk the earlier inputs, and each answer can show the
expansion and the JavaScript of its input. The macro modules of the
examples and the standard library are loaded.

It opens with a session that was run when the site was built
(`examples/repl.json`), so the page reads without the compiler; the
compiler runs that session again when it is loaded. The loop has a
worker of its own: an input that does not end is stopped after 20
seconds without stopping the examples.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
The RFC shows the opening session of the read-eval-print loop as text.
`npm run smoke` runs that session and the inputs the page offers, and
fails when the text is not what the loop says.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Why:
    The loop was the first part of the tour, three screens down, and was
    missed. It is now the section "Try it" before "Why", the header has a
    link "REPL" to it, and the title has a chip that goes there. The
    section that says how to build the site is "Building it".

A link to a heading from another page is followed once the page has
rendered: the heading does not exist before that.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
`%Sql.query` of the database example reads schema.sql in a session too:
the last input the loop offers defines a module with the type of the rows
and a function with a labelled argument per parameter.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
The compiler has no completion of its own. `src/complete.js` gets what
can be written at a place by compiling small files:

- the members of a module are what a file `include Path` defines;
- the names in scope are what the file up to the item being written
  defines, with the members of what it opens;
- the type of the expression before a `.` or a `->` is what the
  compiler says it has when the expression is given a type it cannot
  have, in its place. Function parameters and other local names have
  their types that way. The type gives the fields of a record, and for a
  pipe the functions that take it first.

After `%` and `@` a module offers what returns code, a type, a pattern
or declarations. `engine.complete` answers for an input of a session and
for a file of a project; `npm run smoke` asks for the completions listed
in `examples/repl.json`.

The file up to the item being written is compiled as a module with a new
name each time it changes: the compiler keeps what it knows of a module
given in `open_modules` by its name.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
The input of the read-eval-print loop, the editors of the examples and
the editor of the playground offer what the compiler says can be written
(`engine.complete`): members of a module, fields of a record, functions
for a pipe, macros after `%` and `@`, and the names in scope.

In the loop Enter and the arrows stay the loop's: with the list open they
choose from it, and Enter runs the input when the chosen completion is
what is written already. Tab takes a completion everywhere.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Why:
    Every input had a console of its own, which stopped collecting when
    the input finished. A function of an earlier input kept that console,
    so what it printed when a later input called it was shown as late
    output, below the answer of the input, or not with it at all.

Before:
    > let shout = text => Console.log(text ++ "!")
    let shout: string => unit = <fun>

    > shout("hi")
    - : unit = ()
    hi!

After:
    > shout("hi")
    hi!
    - : unit = ()

A session has one console. What is printed while an input runs is that
input's output; what is printed between inputs (a timer, a promise) is
still shown on a line of its own.

The names of the input modules are not used a second time by a later
session of the same compiler instance: the compiler keeps what it knows
of an opened module by its name, and the second session saw the first
one's `Repl1`. `npm run smoke` runs a second session to check both.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant