A project
A single file is a complete program. When you want more than one, a project is a
directory with a midad.toml in it:
[project]
name = "ledger"
entry = "main.md"
midad run . and midad check . read the manifest and start from the entry
file. A file path still works and needs no manifest; that file's imports are
resolved either way.
The layout
midad.toml the manifest: name and entry file
main.md the entry file: imports, routes, the exec block
lib/ your own modules
routes/ handler functions, mounted by the entry file
vendor/ copies of other people's libraries
tests/ programs that check the project rather than serve it
README.md what the project is
midad new NAME writes this shape. The folders are a convention the checker
enforces rather than a mechanism the runtime needs: a module imported from
outside them still checks and runs, and gets a warning (W007) saying where it
belongs.
A project with two files
The entry file imports a module and calls it through the module's name:
[project]
name = "ledger"
entry = "main.md"
---
midad: "0.1"
module: money
---
# money
Money arithmetic, in cents.
```fn
function total(cents: list): number {
let sum = 0;
for c in cents {
sum = sum + int(c);
}
return sum;
}
function format(cents: number): string {
return str(idiv(cents, 100)) + "." + str(cents % 100);
}
```
---
midad: "0.1"
module: ledger
imports:
- ./lib/money.md
---
# ledger
The entry file. It imports one module and prints a total.
```exec
const prices = [1250, 399, 25];
print(money.format(money.total(prices)));
```
okThe same project runs as a program:
---
midad: "0.1"
module: ledger
imports:
- ./lib/money.md
---
# ledger
```exec
const prices = [1250, 399, 25];
print(money.format(money.total(prices)));
```
16.74Imports
The imports: key sits in the frontmatter next to the permissions, because
that is where a program says what it is allowed to do. Each path is relative to
the importing file and must end in .md.
An imported file contributes its declarations: functions, state, types,
native: bridges, ai:intent directives and html: templates. It never
contributes its exec block, its routes, its commands or its before blocks.
Importing a library cannot run code or serve a route, and each of those blocks
in an imported file is reported rather than dropped silently (E0315 to
E0319).
Permissions do not flow either. The importing program's frontmatter is the only grant, and imported code is checked against it, so a library that reads a file is legal only where the program that imports it granted that path. A library cannot widen the program that uses it.
Module names
Each file's module: is the name its declarations are reached through:
money.total(...), store.count, html("view.page"). Ask for another name
with as:
imports:
- ./lib/money.md
- ./lib/format.md as fmt
Then money.total(...) and fmt.pad(...) both work. Two files may declare the
same function name: store.add and cart.add are different names. What cannot
collide is the alias: two imports may not take the same one (E0312), and an
alias may not be a builtin's name, such as int or time (E0318).
Inside its own file a module writes its declarations unqualified. Qualifying is for the other side of the module boundary.
A file reached twice is loaded once, so diamond imports are fine. An import
cycle is E0310 and the message names the chain.
Routes in a project
A route block belongs to the file that serves it. In a project, main.md is
that file: it mounts the routes, and the handler functions behind them live in
routes/, imported like any other module.
routes/health.md function check(req: map): map { ... }
main.md imports: ./routes/health.md
route:GET /health health.check
The layout rule says a file under routes/ has to be mounted (E0403), and a
file that mounts a route outside routes/ is refused (E0402). A handler file
holds functions; the entry file mounts each one by name with a route block. A
routes/ file that no route names is E0403, and the fix is to mount it or
move it to lib/. midad new writes exactly this shape, and midad check .
answers ok on it.
Using a library
There is no package manager. A library is a directory of .md files that import
each other, and using it means copying it into your project:
$ midad vendor ~/src/midad-web --into lib/web
4 files vendored into lib/web from ~/src/midad-web
That copies the .md files and writes lib/web/.vendor, a fingerprint of each
file and the path it came from. midad check reads it back and warns (W003)
when a vendored file has changed since it was copied:
W003: lib/web/view.md has changed since it was vendored from ~/src/midad-web:
run `midad vendor ~/src/midad-web --into lib/web` to refresh it, or delete
lib/web/.vendor if you meant to fork it
A fingerprint, not a signature: it catches accidental drift, and it will not stop anyone who wants to forge it. Vendoring is flat, because a nested copy would change the relative paths the library's files import each other by.
What the checker enforces
| rule | code |
|---|---|
a file that mounts a route lives in routes/ or is the entry |
E0402 |
a file under routes/ mounts a route |
E0403 |
one module: per file, unique across the project |
E0404 |
an imported module outside lib/, routes/, vendor/, tests/ |
W007 |
| an import that reaches outside the project directory | W002 |
The first three are errors and stop the program; the last two are warnings and do not. Project layout explains what each is for, and the generated layout reference is the same list read out of the binary.
Next: the language tour.