# The dsc command line

The `dsc` command-line surface: the subcommands, what
each produces, the exit codes, and the flags whose effect on the artefact a
grammar cannot carry.

**The shape of the command line is a grammar and lives in a file.**
`compiler/dsc_cli.ebnf` is the normative statement of what invocations are
admissible ([STYL.2.15.6.2]); this document is the residue ([STYL.2.15.4]) —
what the admissible invocations *do*, which no production can say.

**This is the product, not one implementation of it.** Any conforming compiler
owes this surface, so it sits in `runtime/` beside the clauses describing
the artefact a compile produces, on the same split `[STYL.2.13.6]` draws for
ahead-of-time compilation: the product commitment here, each implementation's
account of meeting it in `impl/`.

Prefix `RCLI`.

Paired with:
- `compiler/dsc_cli.ebnf` — the invocation grammar this document is the
  residue of.
- [RDSC] — the artefact `compile` produces.
- [RLOW] — the Rete network it carries.

## What this is not

- Not an account of any compiler's internals. How a subcommand is dispatched,
  and what modules implement it, is `impl/` material ([STYL.2.13.5]).
- Not the invocation grammar. That is `compiler/dsc_cli.ebnf`, and restating a
  production here would be the duplication [STYL.2.15.1] refuses.

---

(RCLI-1)=
## Subcommands [RCLI.1]

(RCLI-1-1)=
[RCLI.1.1] **The command line is subcommand-based.** `dsc` dispatches on a
leading subcommand; the admissible set and the shape of each invocation are the
`subcommand` and `dsc_invocation` productions of `compiler/dsc_cli.ebnf`.

The surface admits `compile`, `compile-project`, `dump-schemas`, `dump-ast`
([RCLI.1.10]) and `check` ([RCLI.1.12]), and `--help` ([RCLI.1.13]). A further
subcommand attaches without restructuring the surface, so adding one is a grammar
change and a clause here, not a redesign.

*see also:* [RCLI.1.10](#RCLI-1-10), [RCLI.1.12](#RCLI-1-12), [RCLI.1.13](#RCLI-1-13)

(RCLI-1-2)=
[RCLI.1.2] **What each subcommand does.** The grammar states which invocations
parse; this states what they produce.

- `compile` compiles one module, resolving its imports against the `-L` search
  path ([LIMP.3.1]), to one `.dsc` at the path `-o` names ([RCLI.2.1]).
- `compile-project` compiles every module under a sketch directory's `rules/`,
  each to its own `.dsc` ([RCLI.1.4]).
- `dump-schemas` emits proto3 source for the schemas the input declares
  ([RCLI.1.5]), or with `--json` a JSON document describing them, their units
  included ([RCLI.1.14]). It produces no `.dsc`.
- `dump-ast` writes the input's syntax tree ([RCLI.1.10]). It produces no `.dsc`.
- `check` gives `compile`'s verdict ([RCLI.1.12]). It produces no `.dsc`.

*see also:* [RCLI.1.10](#RCLI-1-10), [RCLI.1.12](#RCLI-1-12), [RCLI.1.14](#RCLI-1-14), [LIMP.3.1](#LIMP-3-1), [RCLI.1.4](#RCLI-1-4), [RCLI.1.5](#RCLI-1-5), [RCLI.2.1](#RCLI-2-1)

(RCLI-1-3)=
[RCLI.1.3] **Which options repeat, which are required, and which belong to an
implementation.** The grammar lets any option recur; this states what a
recurrence means.

`-L` and `-d` accumulate. Each occurrence adds one value, and the
values apply in the order given: search directories are searched in that order
and dialect files are registered in that order. Every other option that takes a
value takes one, and given more than once, the last occurrence wins.

`-o` is required by `compile`. `compile-project` writes
beside its sources ([RCLI.1.4]), and `dump-schemas` writes to standard output
unless `-o` names a file ([RCLI.1.5]).

A compiler may accept options and subcommands the grammar does not list. An
option it adds may only configure how it meets this surface — which toolchain
an emission step invokes, for instance — and must not change what an admitted
invocation produces. A subcommand it adds is outside this surface: no
obligation of this document binds it, and no other compiler owes it. Either
belongs to the implementation that adds it ([STYL.2.13.6]).

*ratified*  —  *see also:* [RCLI.1.4](#RCLI-1-4), [RCLI.1.5](#RCLI-1-5)

(RCLI-1-4)=
[RCLI.1.4] **`compile-project` builds a sketch.** Its argument names a sketch
directory, which must contain a `rules/` directory holding at least one `.ds`
file; if it does not, the invocation fails with exit `2` ([RCLI.3.1]).

Every `.ds` file under `rules/`, at any depth, is compiled as `compile` would
compile it — its imports resolved against the `-L` search path, its dialect
selected by its own `use` directive — and its `.dsc` is written beside it under
the same basename. Unless `--no-header` is given, each artefact is also written
beside it as `<basename>_dsc.h`, in the embedded form [NGST.4.3] states, for a
target that cannot read files.

A module that cannot be read or does not compile does not stop the others.
Every module is attempted, and the exit code is `1` if any failed.
An internal compiler error ends the invocation at once with `3`
([RCLI.3.1]); the artefacts already written for earlier modules stay.

*ratified*  —  *see also:* [NGST.4.3](#NGST-4-3), [RCLI.3.1](#RCLI-3-1)

(RCLI-1-5)=
[RCLI.1.5] **`dump-schemas` emits one proto3 file**, unless `--json` asks for
the schemas as data ([RCLI.1.14]). It declares
`syntax = "proto3"` and `package ds.dsc.user.v1`, followed by one message per
`schema` the input declares, named for its `NAME`, its fields numbered from `1`
in declaration order. Each message is the proto source the artefact carries for
that schema ([RDSC.5.8.3]), so a descriptor built from this output matches the
one an engine reads from the `.dsc`. The compiler knows no import path for its
input, so the package is this one for a library's input too, where the artefact's
text adds the path; the message is the same.

Only schemas **declared** in the input are emitted. A schema reached by
`import` is emitted by running `dump-schemas` on the module that declares it,
so a module's output does not change when the import graph around it is
reorganised.

The output is one combined file, so one `protoc` run generates bindings for
the whole set. It goes to standard output, or to the file `-o` names; `-o -`
is standard output.

*ratified*  —  *see also:* [RCLI.1.14](#RCLI-1-14), [RDSC.5.8.3](#RDSC-5-8-3)

(RCLI-1-14)=
[RCLI.1.14] **`dump-schemas --json` writes the schemas as data.** With
`--json`, `dump-schemas` must write one JSON document, and nothing else, that
validates against `compiler/dsc_schemas.schema.json`, in place of the proto3
file ([RCLI.1.5]), to standard output or to the file `-o` names. It covers the
schemas [RCLI.1.5] covers, those the input declares, in declaration order, and
each schema's slots in declaration order. What the schema cannot carry:

- **A slot is unit-typed** when its declared type is a unit symbol, or a
  dimension or scalar-group name ([LUNI.7.1]). Its `unit` names its dimension
  or scalar group, that group's base unit ([LUNI.4.3]) and its storage type
  ([LUNI.4.1.1]). Any other slot's `unit` is `null`.
- **`declared` is the unit the slot names**, which is the unit's canonical
  symbol, since a slot type names no alias ([LUNI.7.5]). It is `null`
  for a slot typed by its dimension or scalar group, which holds base-unit
  values ([LUNI.7.1]). `label` is the declared unit's `label`, and `null` when
  it declares none or the slot names no unit.
- **`conversion` is the one the compiler bakes** ([LUNI.2.7.1]). For a base or
  tier-1 unit it is `affine`, with the constants `a` and `b` of
  base = `a` × declared + `b`, each written as the Float's canonical text
  ([LDET.3.2.7]), so a reader that parses each as an IEEE-754 double has the
  baked constant bit for bit. A base unit's are `1.0` and `0.0`. For a tier-2
  unit it is `tier2` and carries no constants, since its conversion is not
  affine ([LUNI.4.3]). It is `null` when `declared` is.
- **An input `dump-schemas` refuses writes no document.** It is refused as
  the proto3 form refuses it.

*ratified*  —  *see also:* [LDET.3.2.7](#LDET-3-2-7), [LUNI.2.7.1](#LUNI-2-7-1), [LUNI.4.1.1](#LUNI-4-1-1), [LUNI.4.3](#LUNI-4-3), [LUNI.7.1](#LUNI-7-1), [LUNI.7.5](#LUNI-7-5), [RCLI.1.5](#RCLI-1-5)

(RCLI-1-6)=
[RCLI.1.6] **`strip-tests` removes an artefact's test content.** It reads a
`.dsc` and writes to the path `-o` names the same artefact without:

- every atom of the `Test*` scenario and `Oracle*` oracle families ([RDSC.7.1]); and
- the rules the compiler synthesized to realize a `produces` assertion's `where`
  predicate ([RLOW.17.2]), with every network node that serves only them.

That is the whole of what a compiled test adds ([RTIM.14.12]), so an engine runs
the stripped artefact's own rules exactly as it runs the original's. An artefact
with no test content is written unchanged.

*Deferred: not binding in this edition.*

*see also:* [RDSC.7.1](#RDSC-7-1), [RLOW.17.2](#RLOW-17-2), [RTIM.14.12](#RTIM-14-12)

(RCLI-1-7)=
[RCLI.1.7] **`strip-debug` removes an artefact's debug metadata.** It reads a
`.dsc` and writes to the path `-o` names the same artefact without its `DsSource`,
`LineSpan`, `DocString`, `RuleSource` and `RuleDebugName` atoms — source text, source spans, docstrings,
rule source locations and debug display names ([RDSC.7.1]). An engine executes
the stripped artefact exactly as it executes the original; what is lost is what
a debugger or a documentation tool reads.

A compiler must not strip an atom whose absence changes execution. A rule with
no `RuleCooldown` atom has no cooldown ([RDSC.6.5.4]), so `RuleCooldown` is kept, although it
is per-rule metadata in form.

*Deferred: not binding in this edition.*

*see also:* [RDSC.6.5.4](#RDSC-6-5-4), [RDSC.7.1](#RDSC-7-1)

(RCLI-1-8)=
[RCLI.1.8] **What a strip must preserve.** `strip-tests` and `strip-debug`
each write a well-formed `.dsc`. Every atom they keep is written byte-for-byte as
read, with one exception: where the header carries `dsc_sha256`, the stripper
must recompute it over the bytes it writes ([RDSC.3.6]), because a hash of the
original would describe an artefact that is no longer there.

The result is therefore a **distinct artefact**. Its bytes and its hash differ
from the original's, and anything bound to the original by that hash — a fact
stream's binding tuple among them ([RBND.1.3]) — does not bind to it.

The two strips are independent and compose in either order. An input that is
not a well-formed `.dsc` is refused with exit `2` ([RCLI.3.1]), and nothing is
written.

*Deferred: not binding in this edition.*

*see also:* [RBND.1.3](#RBND-1-3), [RCLI.3.1](#RCLI-3-1), [RDSC.3.6](#RDSC-3-6)

(RCLI-1-9)=
[RCLI.1.9] **Standard streams, the input's name, and the contract file.**
Each of these belongs to `compile`.

- An input of `-` is the source read from standard input, and `-o -` writes
  the artefact to standard output. A `-` is never a filename.
- `--filename` names the input wherever the compiler names it: in every
  diagnostic, and as the source path the `DsSource` atom records ([RDSC.14.2.3]).
  Without it, the name is the input path as given, or `<stdin>` for standard
  input.
- A diagnostic in any other file the compile reads names that file. A library
  module is named as its `DsSource` names it ([RDSC.14.2.3]). A module's
  companion `.dstest` ([LTSL.2.4]) is named as its module is, with the `.ds`
  extension replaced by `.dstest`. In the JSON form, that name is the span's
  `file` ([RCLI.3.2]).
- `--dsrt`, which names a realtime contract file, is refused in this edition
  with `DS_VALIDATE_REALTIME_DEFERRED` ([LCAP.2.1.5]). The grammar keeps the
  option so the compiler can name what the invocation passed; the compiler
  reads no file it names.

*ratified*  —  *see also:* [LCAP.2.1.5](#LCAP-2-1-5), [LTSL.2.4](#LTSL-2-4), [RCLI.3.2](#RCLI-3-2), [RDSC.14.2.3](#RDSC-14-2-3)

(RCLI-1-10)=
[RCLI.1.10] **`dump-ast` writes the input's syntax tree.** The compiler must
lex, lay out and parse the input and do nothing more: it resolves no name and
validates nothing, so a program that does not compile still dumps when it
parses. It writes one JSON document, the `Module` node of `tools/ds_ast/ds.asdl`
and everything under it, to standard output, or to the file `-o` names. The
input is a path or `-`, and `--filename` names it, as [RCLI.1.9] says.

What the tree's definition cannot carry:

- **A node is an object.** Its `_type` member is its constructor's name, and
  its other members are its fields and attributes, by name. A sequence is an
  array, and an absent optional is `null`.
- **Positions count as the definition's attributes do.** `lineno` is a 1-based
  line and `col_offset` a 0-based column, counted in code points, so a node's
  `col_offset` is one less than the `column` a diagnostic gives the same point
  ([RCLI.3.2]). `end_lineno` and `end_col_offset` may be `null`.
- **An input that does not parse writes no tree.** It is refused: the compiler
  exits `1` and reports the diagnostic as `compile` reports one, under
  `--diagnostics` ([RCLI.3.2]).

*ratified*  —  *see also:* [RCLI.1.9](#RCLI-1-9), [RCLI.3.2](#RCLI-3-2)

(RCLI-1-11)=
[RCLI.1.11] **The bundled library sits beside the compiler.** A compiler finds the library it ships with ([LIMP.3.3.1]) at
`../share/ds/stdlib`, resolved against the directory that holds its own
executable, and searches it after every `-L` directory, in the order
[RCLI.1.3] gives them. No option, environment variable or configuration names
that directory or removes it from the search path.

**A symbolic link to the compiler is followed.** The directory that holds the
executable is the one that holds the file itself, with every link on the way to
it resolved, so a compiler invoked through a link finds the library beside the
link's target and not beside the link. A chain of links that ends nowhere, or
that does not end, is a compiler that does not find the library (below).

Every build of a compiler **must** lay the library out there, whatever the build
is for. A compiler that runs with no file system of its own, such as one
compiled to WebAssembly, **must** present the library at that path in the file
system it reads.

**A compiler that does not find it resolves nothing.** `units.core` is read from
the library on every compilation ([LUNI.4.1.2]), so without it the compiler
would accept a different language. The compiler **must** refuse every
invocation that resolves a name with exit `2` ([RCLI.3.1]), naming the directory
it looked in. `dump-ast`, which resolves none ([RCLI.1.10]), is unaffected.

*ratified*  —  *see also:* [LIMP.3.3.1](#LIMP-3-3-1), [LUNI.4.1.2](#LUNI-4-1-2), [RCLI.1.10](#RCLI-1-10), [RCLI.1.3](#RCLI-1-3), [RCLI.3.1](#RCLI-3-1)

(RCLI-1-12)=
[RCLI.1.12] **`check` gives `compile`'s verdict and writes no artefact.** The
compiler must do everything `compile` does to the input short of writing the
artefact, resolving its imports and dialects as `compile` does. Its exit code,
and what it writes to standard error, are what `compile` gives for the same
input under the same `--diagnostics` ([RCLI.3.2]). The input is a path or `-`,
and `--filename` names it, as [RCLI.1.9] says. It takes no `-o`, and an
invocation naming one is a usage error ([RCLI.3.1]).

With `--dump-ast`, the compiler must write one JSON document to standard output
that validates against `compiler/dsc_check.schema.json`, and nothing to standard
error. The exit code does not move. What the schema cannot carry:

- **`tree` does not wait for the verdict.** It is the tree `dump-ast` writes for
  the input ([RCLI.1.10]) whenever the input parses, a program a later phase
  refuses included, and `null` only when the input does not parse.
- **`diagnostics` is what `compile` would have written to standard error**, in
  the form `--diagnostics` chooses.

*see also:* [RCLI.1.10](#RCLI-1-10), [RCLI.1.9](#RCLI-1-9), [RCLI.3.1](#RCLI-3-1), [RCLI.3.2](#RCLI-3-2)

(RCLI-1-13)=
[RCLI.1.13] **`--help` writes the usage.** At the head of an invocation, or
directly after a subcommand, `--help` or `-h` must write the usage to standard
output and exit `0`, and the compiler reads no input. The usage's wording is not
a contract. It must not list an option this edition refuses ([RCLI.1.9]).

An invocation with no arguments parses against nothing, and is a usage error
([RCLI.3.1]).

*see also:* [RCLI.1.9](#RCLI-1-9), [RCLI.3.1](#RCLI-3-1)

(RCLI-2)=
## What a compile produces [RCLI.2]

(RCLI-2-1)=
[RCLI.2.1] **A plain `compile` emits the base `.dsc`:** the schema and rule
atoms, plus the Rete network — `AlphaNode`, `BetaNode`, `TerminalNode` — which
is the program ([RDSC.4a]).

The Rete atoms are emitted whenever the artefact is to be interpretable, which
is the default. Nothing an author writes suppresses them; only a flag does, and
in v1 no flag that suppresses them is admitted.

*see also:* [RDSC.4a](#RDSC-4a)

(RCLI-3)=
## Exit codes [RCLI.3]

(RCLI-3-1)=
[RCLI.3.1] **Four exit codes, and they are a contract.**

| Code | Meaning |
|---|---|
| `0` | success |
| `1` | compile error — the program was refused, and one or more diagnostics rendered to stderr |
| `2` | usage error — the invocation did not parse against `compiler/dsc_cli.ebnf` and used none of this compiler's own options or subcommands ([RCLI.1.3]), named an option this edition does not admit ([RCLI.2.1]) or this compiler does not implement, named a path that could not be read or written, or found no bundled library ([RCLI.1.11]) |
| `3` | internal compiler error — the compiler failed while carrying out an invocation it had accepted: a fault in the compiler, or its memory exhausted |

The distinction between `1` and `2` is what a build script branches on: a
compile error means the program is wrong, a usage error means the command was.
Collapsing them would make a mistyped flag indistinguishable from a rejected
program.

A `3` is the compiler's fault, never the program's or the command's. A compiler that finds it cannot finish what an invocation
asks **must** exit `3`, write the reason to stderr, and write none of the
artefact it could not finish: no byte of it to standard output, and no file at
the path `-o` names. [RCLI.1.4] states which artefacts `compile-project` keeps
when it stops. It renders no diagnostic, since it reaches no verdict on the
program.

A compiler must refuse an option it does not implement, with `2`, rather than
accept and ignore it: an ignored option hands back an artefact the author
believes carries something it does not.

*ratified*  —  *see also:* [RCLI.1.11](#RCLI-1-11), [RCLI.1.3](#RCLI-1-3), [RCLI.1.4](#RCLI-1-4), [RCLI.2.1](#RCLI-2-1)

(RCLI-3-2)=
[RCLI.3.2] **Diagnostics as data.** `--diagnostics` chooses how `compile`, `check`
([RCLI.1.12]) and `dump-ast` ([RCLI.1.10]) write their diagnostics to stderr: `text`, the
default, renders them for a person; `json` writes them as one document, and
nothing else, that validates against `compiler/dsc_diagnostics.schema.json`.
Under `json` the compiler must write that document whenever it reports a
diagnostic, a refused program's errors and an accepted program's warnings
alike, holding every diagnostic the text form would render, in the order the
text form renders them; when it reports none, it writes no document.

What the schema cannot carry:

- **The format does not move the exit code.** A refused program exits `1`
  and an accepted one `0`, under either format ([RCLI.3.1]). A usage error exits `2` and an internal
  compiler error `3`. Neither is a diagnostic of the program, so the document
  never carries one, and a compiler that exits `3` writes no document.
- **A reader keys on `code` and `span`.** A diagnostic's `code` is its
  registered name ([RCLI.5.1]); its `message` and `hint` are wording, which a
  compiler may change without notice.
- **`file` is the name the compiler gives the source** ([RCLI.1.9]):
  `--filename` when given, else the input path as given, else `<stdin>`.

*ratified*  —  *see also:* [RCLI.1.10](#RCLI-1-10), [RCLI.1.12](#RCLI-1-12), [RCLI.1.9](#RCLI-1-9), [RCLI.3.1](#RCLI-3-1), [RCLI.5.1](#RCLI-5-1)

(RCLI-4)=
## The grammar is authoritative [RCLI.4]

(RCLI-4-1)=
[RCLI.4.1] **[LGRA] is the authoritative statement of the DS language
surface**, and an implementation's parser answers to it rather than the
reverse. Where a compiler's grammar and the `LGRA` productions disagree, the
spec is right and the parser is defective.

No parser is normative over the grammar: every implementation's parser answers
to `[LGRA]`, whatever other parsers exist ([STYL.2.15.5]). An implementation that treats its own grammar as
the definition has inverted the relationship [STYL.2.13] describes — a spec
that sends the reader to the code to learn what it requires.

(RCLI-5)=
## Diagnostics [RCLI.5]

(RCLI-5-1)=
[RCLI.5.1] **The compile-diagnostic registry is `specs/registry/validate.ebnf`.**
Every `DS_VALIDATE_*` code a compiler emits is allocated there and nowhere
else, and each implementation's view of the set is generated from it — a code
absent from the registry is not one a compiler may emit.

The registry is an enumeration under [STYL.2.15.6], so its alternation is the
closure and each terminal production is an allocation. Retirement keeps the
production and drops the membership, which leaves the number allocated and
still naming what held it ([STYL.2.8.7]).

**The number is a registry key, not a wire value.** A validate code never
leaves a compiler as a number: it is rendered, matched by name, and never
serialised — which is why it is an EBNF registry rather than a protobuf
([STYL.5.9.7]).

(RCLI-5-2)=
[RCLI.5.2] **Diagnostic codes are stable, and renaming one is a breaking
change.** Editors and CI wrap a compiler and match codes against their own
surfaces, so a code is a published identifier rather than an internal label.

This is [STYL.2.8.7]'s discipline reaching the surface a tool consumes: a
code out of use stays allocated and is never reused, and a live code never
changes its name to mean the same thing.

(RCLI-5-3)=
[RCLI.5.3] **A compile reports every error it finds, not the first.** Within one
stage of analysis a compiler must not stop at its first error; it reports each
one, as a separate diagnostic. It may stop before a later stage once an earlier
stage has failed, because the later stage's input is then not well-formed and
what it would report would be noise.

An author fixing a program should see the whole of what is wrong with it at the
point the compiler got to, not discover it one rebuild at a time.

