Phase 4: commands in Bjolang

- A package of user commands in $XDG_CONFIG_HOME/bjosh/commands. Its
  module commands.bjo exports bjosh-extension: value commands, shell
  commands and a prompt function.
- (bjosh api): the types and functions a package imports.
- (bjosh user): builds the package when it is stale, and loads it with
  (std plugin) at each start. `reload` builds and loads it again.
- bjosh --new-commands writes a package with an example.
- No Native AOT, because it cannot load modules. bin/bjosh starts
  src/main.exe.
- tests/extension.bjo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 10:38:40 +01:00
parent f8c4ad64bc
commit 84bd2f3382
11 changed files with 686 additions and 29 deletions

View file

@ -364,10 +364,80 @@ the text above:*
~tests/line-terminal.bjo~ tests ~#:notices~; Bjolang's test 622 tests
~output-to-errors~.
** Phase 4: later
** Phase 4: commands in Bjolang
- Commands that users write in Bjolang, compiled, and loaded at start-up
from ~~/.config/bjosh/commands/~.
User commands, compiled, loaded into the shell at start-up. Needs
~(std plugin)~ in Bjolang (see plugin-plan.org there).
1. *No Native AOT.* A Native AOT program cannot load assemblies. bjosh
runs as ~dotnet src/main.exe~, started by a script ~bin/bjosh~. Measured
(2026-12-01): 115 ms to the first prompt, against 9 ms with AOT. If that
is too slow: ReadyToRun for the modules, a change in Bjolang.
2. *The package.* ~$XDG_CONFIG_HOME/bjosh/commands/~, or
~~/.config/bjosh/commands/~: a Bjolang package that depends on bjosh.
Its module ~commands.bjo~ exports ~bjosh-extension~, an ~Extension~.
~bjosh --new-commands~ writes the manifest, with the path of this
bjosh, and an example ~commands.bjo~.
3. *~(bjosh api)~*, the module a package imports. It has the types and the
functions a command needs:
- ~Extension~: a record of ~commands~ (value commands), ~shell-commands~
and ~prompt~ (~(Option (-> ShellView string))~).
- Value commands: ~ValueCommand~ as the built-in ones have, with their
long and short names, and the three kinds of body (bytes, values,
nothing). With ~emit!~, ~next!~, ~command-error~, ~record-of~ and the
~Json~ type.
- Shell commands: ~(-> ShellView (Vec string) int)~. They run in the
shell, alone on a line, as ~cd~ does.
- ~ShellView~: what a shell command and the prompt can see and do: the
status ~$?~, a variable, setting a variable, the jobs (number and
state), and running a line (~shell-run~, which returns its status). So
a shell command can do what a shell function does: ~(shell-run sh
(str "cd " dir))~.
4. *Loading.*
- At start-up of every bjosh (interactive, ~-c~ and a file), when the
directory is there.
- It is built with ~bjo build~ when a ~.bjo~ file or the manifest is
newer than ~commands.dll~: a build that does nothing takes 0.4 s, so
it is not run each time. The output of a build that fails is shown,
and the shell starts without the commands.
- ~reload~ builds and loads it again, into the same shell.
- An error of loading is one message; the shell goes on without the
commands.
5. *Names.* A user's value command takes the place of a built-in one with
the same name, and follows the rule of decision 4 against PATH. A
user's shell command takes the place of a built-in one, except ~exit~,
~reload~ and ~command~. ~type~ and ~help~ show them, marked as the
user's.
6. *The prompt.* The order: ~PROMPT~ when it is set, else the extension's
~prompt~, else the prompt of now. A prompt function that raises is
shown once, and the prompt of now is used until ~reload~.
7. *Errors.* A command that raises is as a built-in one that raises: its
message, and status 1.
8. *Tests.* ~tests/extension.bjo~ writes a package in a temporary
directory, with a value command, a shell command and a prompt, and runs
bjosh with ~XDG_CONFIG_HOME~ there: ~-c~ for the commands, ~script~ for
the prompt. Also: a package that does not compile, a rebuild after a
change, and ~reload~.
*Implemented (2026-12-01). Where the code differs from the text above:*
- ~JobInfo~ has the fields ~job-number~, ~job-state~ and ~job-command~, and
the fields of ~ShellView~ are ~get-status~, ~get-var~, ~set-var~,
~run-line~ and ~list-jobs~. With the names of ~JobEntry~ and ~Shell~, the
type checker could not tell the records apart in a lambda.
- A package that does not build is stale at each start, so each start
builds it again and shows the error, until it is fixed.
- Measured: 132 ms for ~bjosh -c true~ with the example package loaded.
- Tests: ~tests/extension.bjo~, 23 checks. The other tests: syntax 72,
expand 26, values 59, editor 24, run 60, interactive 16.
** Phase 5: later
- Control flow (~if~, ~for~). POSIX form, or a form of our own; to
decide then.
- Values in variables.